Skip to content

Commit

Permalink
Merge pull request #1273 from namusyaka/support-trailing-slash
Browse files Browse the repository at this point in the history
add :strict_paths option for managing trailing slashes
  • Loading branch information
Zachary Scott authored Apr 1, 2017
2 parents 4194577 + 2445a49 commit 3452172
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ def route_eval
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
Expand Down Expand Up @@ -1819,6 +1820,7 @@ class << self
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)) }
Expand Down
16 changes: 16 additions & 0 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1499,4 +1499,20 @@ def authorize(username, password)
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

0 comments on commit 3452172

Please sign in to comment.