Skip to content

Commit

Permalink
handle empty route patterns as expected without breaking rails 3 comp…
Browse files Browse the repository at this point in the history
…atibility, fixes sinatra#231
  • Loading branch information
rkh committed Mar 30, 2011
1 parent e016efd commit f438be2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Expand Down
11 changes: 6 additions & 5 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ def forwarded?
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)
Expand Down Expand Up @@ -746,7 +743,9 @@ def route_eval(&block)
# 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?
Expand Down Expand Up @@ -1136,6 +1135,7 @@ def options(path, opts={}, &bk) route 'OPTIONS', path, opts, &bk end
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)
Expand Down Expand Up @@ -1400,6 +1400,7 @@ class << self

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)) }
Expand Down
22 changes: 19 additions & 3 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit f438be2

Please sign in to comment.