Merge pull request #1273 from namusyaka/support-trailing-slash

add :strict_paths option for managing trailing slashes
This commit is contained in:
Zachary Scott 2017-04-01 14:13:56 +09:00 committed by GitHub
commit 34521720b6
2 changed files with 18 additions and 0 deletions

View File

@ -1018,6 +1018,7 @@ module Sinatra
def process_route(pattern, conditions, block = nil, values = [])
route = @request.path_info
route = '/' if route.empty? and not settings.empty_path_info?
route = route[0..-2] if !settings.strict_paths? && route != '/' && route.end_with?('/')
return unless params = pattern.params(route)
params.delete("ignore") # TODO: better params handling, maybe turn it into "smart" object or detect changes
@ -1819,6 +1820,7 @@ module Sinatra
set :absolute_redirects, true
set :prefixed_redirects, false
set :empty_path_info, nil
set :strict_paths, true
set :app_file, nil
set :root, Proc.new { app_file && File.expand_path(File.dirname(app_file)) }

View File

@ -1499,4 +1499,20 @@ class RoutingTest < Minitest::Test
get '/foo/'
assert_equal 'Foo with a slash', body
end
it 'does not treat routes with and without trailing slashes differently if :strict_paths is disabled' do
mock_app do
disable :strict_paths
get '/foo' do
'foo'
end
end
get '/foo'
assert_equal 'foo', body
get '/foo/'
assert_equal 'foo', body
end
end