diff --git a/CHANGES b/CHANGES index 8668767d..bfa2a75b 100644 --- a/CHANGES +++ b/CHANGES @@ -40,6 +40,9 @@ `content_type` (example: `content_type "text/plain; charset=utf-16"`). (Konstantin Haase) + * If a route with an empty pattern is defined (`get("") { ... }`) requests with + an empty patch info match this route instead of "/". (Konstantin Haase) + = 1.2.1 / 2011-03-17 * Use a generated session secret when using `enable :sessions`. (Konstantin diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 3f82503e..abff2d1f 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -40,10 +40,7 @@ module Sinatra end def route - @route ||= begin - path = Rack::Utils.unescape(path_info) - path.empty? ? "/" : path - end + @route ||= Rack::Utils.unescape(path_info) end def path_info=(value) @@ -742,7 +739,9 @@ module Sinatra # Returns pass block. def process_route(pattern, keys, conditions) @original_params ||= @params - if match = pattern.match(@request.route) + route = @request.route + route = '/' if route.empty? and not settings.empty_path_info? + if match = pattern.match(route) values = match.captures.to_a params = if keys.any? @@ -1132,6 +1131,7 @@ module Sinatra def route(verb, path, options={}, &block) # Because of self.options.host host_name(options.delete(:host)) if options.key?(:host) + enable :empty_path_info if path == "" and empty_path_info.nil? block, pattern, keys, conditions = compile! verb, path, block, options invoke_hook(:route_added, verb, path, block) @@ -1411,6 +1411,7 @@ module Sinatra set :absolute_redirects, true set :prefixed_redirects, false + set :empty_path_info, nil set :app_file, nil set :root, Proc.new { app_file && File.expand_path(File.dirname(app_file)) } diff --git a/test/routing_test.rb b/test/routing_test.rb index 189881d3..4e6e9c98 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -93,12 +93,28 @@ class RoutingTest < Test::Unit::TestCase assert_equal "

Not Found

", response.body end - it 'matches empty PATH_INFO to "/"' do - mock_app { + it 'matches empty PATH_INFO to "/" if no route is defined for ""' do + mock_app do get '/' do 'worked' end - } + end + + get '/', {}, "PATH_INFO" => "" + assert ok? + assert_equal 'worked', body + end + + it 'matches empty PATH_INFO to "" if a route is defined for ""' do + mock_app do + get '/' do + 'did not work' + end + + get '' do + 'worked' + end + end get '/', {}, "PATH_INFO" => "" assert ok?