From 335dbdd487cde05451078b18dbea6f87901d08e8 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 29 Jan 2013 15:28:59 +0100 Subject: [PATCH] catch empty routes This fixes a bug where an empty route would catch all requests resulting in all routes found with the empty route. --- lib/zero/router.rb | 2 +- spec/unit/zero/router/call_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/zero/router.rb b/lib/zero/router.rb index 8d1273f..e5b513c 100644 --- a/lib/zero/router.rb +++ b/lib/zero/router.rb @@ -40,7 +40,7 @@ module Zero routes.each do |route, target| @routes[ Regexp.new( - route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) + '$')] = target + '^' + route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) + '$')] = target end end diff --git a/spec/unit/zero/router/call_spec.rb b/spec/unit/zero/router/call_spec.rb index 6f01e3c..683eca8 100644 --- a/spec/unit/zero/router/call_spec.rb +++ b/spec/unit/zero/router/call_spec.rb @@ -25,6 +25,13 @@ describe Zero::Router, '#call' do it_behaves_like "a sample app" end + context 'with empty path' do + let(:routes) {{ '' => wrong_app, '/welcome' => app }} + let(:env) { EnvGenerator.get('/welcome') } + + it_behaves_like "a sample app" + end + context 'with multiple router' do let(:routes) {{ '/foo' => app, '/wrong' => wrong_app }} let(:env) { EnvGenerator.get('/foo') } @@ -45,6 +52,14 @@ describe Zero::Router, '#call' do it_behaves_like "a sample app" end + context 'with empty route' do + let(:routes) {{ '' => wrong_app }} + let(:env) { EnvGenerator.get('/foo') } + let(:result) { ['Not found!'] } + let(:status_code) { 404 } + it_behaves_like "a sample app" + end + context 'with parameters' do let(:routes) {{ '/foo/:id' => app }} let(:env) { EnvGenerator.get('/foo/bar') }