Permalink
Browse files

sort routes for more specific results

This change sorts the routes to get the most specific routes first and
avoid hitting a route which is not the most specific match.
With this the regex to extract variables is made more strict to avoid
matching half of the URI.
  • Loading branch information...
1 parent c55441f commit defb7703c69d1e4f756254723d50997fb8dc63da @Gibheer Gibheer committed Sep 9, 2013
Showing with 33 additions and 2 deletions.
  1. +2 −2 lib/zero/router.rb
  2. +31 −0 spec/unit/zero/router/call_spec.rb
View
@@ -20,7 +20,7 @@ class Router
# match for variables in routes
VARIABLE_MATCH = %r{:(\w+)[^/]?}
# the replacement string to make it an regex
- VARIABLE_REGEX = '(?<\1>.+?)'
+ VARIABLE_REGEX = '(?<\1>[\w]+?)'
# regex part of the beginning of the line
REGEX_BEGINNING = '\A'
# regex part of the end of the line
@@ -41,7 +41,7 @@ class Router
# @param routes [Hash] a map of URLs to rack compatible applications
def initialize(routes)
@routes = {}
- routes.each do |route, target|
+ routes.to_a.sort_by(&:first).reverse.each do |route, target|
@routes[
Regexp.new(REGEX_BEGINNING +
route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) +
@@ -44,6 +44,22 @@
it_behaves_like "a sample app"
end
+ context 'with nested variable routes' do
+ let(:routes) do
+ { '/' => wrong_app, '/foo/:id' => app, '/foo/:id/bar' => wrong_app }
+ end
+ let(:env) { EnvGenerator.get('/foo/23') }
+ it_behaves_like "a sample app"
+ end
+
+ context 'with nested routes and variable in the middle' do
+ let(:routes) do
+ { '/' => wrong_app, '/foo/:id' => wrong_app, '/foo/:id/bar' => app }
+ end
+ let(:env) { EnvGenerator.get('/foo/23/bar') }
+ it_behaves_like "a sample app"
+ end
+
context 'with a route not found' do
let(:routes) {{ '/foo' => wrong_app, '/foo/bar/baz' => app }}
let(:env) { EnvGenerator.get('/foo/bar') }
@@ -72,4 +88,19 @@
it_behaves_like 'a sample app'
end
+
+ context 'with parameters and nested routes' do
+ let(:routes) do
+ { '/' => wrong_app, '/foo/:id' => app, '/foo/:id/bar' => wrong_app }
+ end
+ let(:env) { EnvGenerator.get('/foo/bar') }
+ let(:app) do
+ lambda do |env|
+ [200, content_type, [Zero::Request.new(env).params['id']]]
+ end
+ end
+ let(:result) { ['bar'] }
+
+ it_behaves_like "a sample app"
+ end
end

0 comments on commit defb770

Please sign in to comment.