Add #bypassed callback before calling forward

This commit is contained in:
Jon Crosby 2009-03-11 14:44:09 -07:00 committed by Ryan Tomayko
parent ce2c889f41
commit 0e6becf09d
2 changed files with 13 additions and 1 deletions

View File

@ -435,6 +435,9 @@ module Sinatra
# No matching route found or all routes passed -- forward downstream
# when running as middleware; 404 when running as normal app.
if @app
# Call bypassed method before forward to catch behavior that should
# happen even if no routes are hit.
bypassed if respond_to?(:bypassed)
forward
else
raise NotFound

View File

@ -42,7 +42,7 @@ end
describe "Sinatra::Base as Rack middleware" do
app = lambda { |env|
[210, {'X-Downstream' => 'true'}, ['Hello from downstream']] }
[210, {'X-Downstream' => 'true', 'X-Bypass-Test' => '1' || ''}, ['Hello from downstream']] }
class TestMiddleware < Sinatra::Base
end
@ -58,6 +58,10 @@ describe "Sinatra::Base as Rack middleware" do
end
class TestMiddleware < Sinatra::Base
def bypassed
env['X-Bypass-Test'] = '1'
end
get '/' do
'Hello from middleware'
end
@ -78,6 +82,11 @@ describe "Sinatra::Base as Rack middleware" do
assert_equal 'Hello from downstream', response.body
end
it 'calls #bypassed before forwarding downstream' do
response = request.get('/missing')
assert_equal '1', response['X-Bypass-Test']
end
class TestMiddleware < Sinatra::Base
get '/low-level-forward' do
app.call(env)