Permalink
Browse files

handle empty route patterns as expected without breaking rails 3 comp…

…atibility, fixes #231
  • Loading branch information...
rkh committed Mar 30, 2011
1 parent e016efd commit f438be28a936d56e2ef701e42fd7247ca502484f
Showing with 28 additions and 8 deletions.
  1. +3 −0 CHANGES
  2. +6 −5 lib/sinatra/base.rb
  3. +19 −3 test/routing_test.rb
View
@@ -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
View
@@ -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)
@@ -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?
@@ -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)
@@ -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)) }
View
@@ -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?

0 comments on commit f438be2

Please sign in to comment.