handle empty route patterns as expected without breaking rails 3 compatibility, fixes #231

This commit is contained in:
Konstantin Haase 2011-03-30 08:28:25 +02:00
parent dbb4301660
commit 49d470255d
3 changed files with 28 additions and 8 deletions

View File

@ -40,6 +40,9 @@
`content_type` (example: `content_type "text/plain; charset=utf-16"`). `content_type` (example: `content_type "text/plain; charset=utf-16"`).
(Konstantin Haase) (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 = 1.2.1 / 2011-03-17
* Use a generated session secret when using `enable :sessions`. (Konstantin * Use a generated session secret when using `enable :sessions`. (Konstantin

View File

@ -40,10 +40,7 @@ module Sinatra
end end
def route def route
@route ||= begin @route ||= Rack::Utils.unescape(path_info)
path = Rack::Utils.unescape(path_info)
path.empty? ? "/" : path
end
end end
def path_info=(value) def path_info=(value)
@ -742,7 +739,9 @@ module Sinatra
# Returns pass block. # Returns pass block.
def process_route(pattern, keys, conditions) def process_route(pattern, keys, conditions)
@original_params ||= @params @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 values = match.captures.to_a
params = params =
if keys.any? if keys.any?
@ -1132,6 +1131,7 @@ module Sinatra
def route(verb, path, options={}, &block) def route(verb, path, options={}, &block)
# Because of self.options.host # Because of self.options.host
host_name(options.delete(:host)) if options.key?(: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 block, pattern, keys, conditions = compile! verb, path, block, options
invoke_hook(:route_added, verb, path, block) invoke_hook(:route_added, verb, path, block)
@ -1411,6 +1411,7 @@ module Sinatra
set :absolute_redirects, true set :absolute_redirects, true
set :prefixed_redirects, false set :prefixed_redirects, false
set :empty_path_info, nil
set :app_file, nil set :app_file, nil
set :root, Proc.new { app_file && File.expand_path(File.dirname(app_file)) } set :root, Proc.new { app_file && File.expand_path(File.dirname(app_file)) }

View File

@ -93,12 +93,28 @@ class RoutingTest < Test::Unit::TestCase
assert_equal "<h1>Not Found</h1>", response.body assert_equal "<h1>Not Found</h1>", response.body
end end
it 'matches empty PATH_INFO to "/"' do it 'matches empty PATH_INFO to "/" if no route is defined for ""' do
mock_app { mock_app do
get '/' do get '/' do
'worked' 'worked'
end 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" => "" get '/', {}, "PATH_INFO" => ""
assert ok? assert ok?