Skip to content

Commit

Permalink
Allow changing request.path_info in a before filter that has a pattern.
Browse files Browse the repository at this point in the history
Fixes #114.
  • Loading branch information
rkh committed Nov 4, 2010
1 parent aa06ed5 commit 4988126
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/sinatra/base.rb
Expand Up @@ -28,6 +28,18 @@ def secure?
else
alias secure? ssl?
end

def route
@route ||= begin
path = Rack::Utils.unescape(path_info)
path.empty? ? "/" : path
end
end

def path_info=(value)
@route = nil
super
end
end

# The response object. See Rack::Response and Rack::ResponseHelpers for
Expand Down Expand Up @@ -635,11 +647,7 @@ def route_eval(&block)
# Returns pass block.
def process_route(pattern, keys, conditions)
@original_params ||= @params
@path ||= begin
path = unescape(@request.path_info)
path.empty? ? "/" : path
end
if match = pattern.match(@path)
if match = pattern.match(@request.route)
values = match.captures.to_a
params =
if keys.any?
Expand Down
10 changes: 10 additions & 0 deletions test/filter_test.rb
Expand Up @@ -255,6 +255,16 @@ class AfterFilterTest < Test::Unit::TestCase
assert ran_filter
end

it 'changes to path_info from a pattern matching before filter are respoected when routing' do
mock_app do
before('/foo') { request.path_info = '/bar' }
get('/bar') { 'blah' }
end
get '/foo'
assert ok?
assert_equal 'blah', body
end

it 'generates block arguments from route pattern' do
subpath = nil
mock_app do
Expand Down

0 comments on commit 4988126

Please sign in to comment.