handle empty route patterns as expected without breaking rails 3 compatibility, fixes #231
This commit is contained in:
parent
dbb4301660
commit
49d470255d
3
CHANGES
3
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
|
||||
|
|
|
@ -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)) }
|
||||
|
|
|
@ -93,12 +93,28 @@ class RoutingTest < Test::Unit::TestCase
|
|||
assert_equal "<h1>Not Found</h1>", 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?
|
||||
|
|
Loading…
Reference in New Issue