Set X-Cascade header when using pass

Setting X-Cascade: pass allows middleware outside the Sinatra stack
to continue trying to match the request.

Signed-off-by: Ryan Tomayko <rtomayko@gmail.com>
This commit is contained in:
Joshua Peek 2009-12-23 20:32:23 -06:00 committed by Ryan Tomayko
parent 3ef8eedef2
commit f9a792396c
3 changed files with 43 additions and 3 deletions

View File

@ -583,9 +583,10 @@ module Sinatra
end
def handle_not_found!(boom)
@env['sinatra.error'] = boom
@response.status = 404
@response.body = ['<h1>Not Found</h1>']
@env['sinatra.error'] = boom
@response.status = 404
@response.headers['X-Cascade'] = 'pass'
@response.body = ['<h1>Not Found</h1>']
error_block! boom.class, NotFound
end

View File

@ -142,6 +142,19 @@ class HelpersTest < Test::Unit::TestCase
assert_equal 404, status
assert_equal '', body
end
it 'does not set a X-Cascade header' do
mock_app {
get '/' do
not_found
fail 'not_found should halt'
end
}
get '/'
assert_equal 404, status
assert_equal nil, response.headers['X-Cascade']
end
end
describe 'headers' do

View File

@ -60,6 +60,15 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 404, status
end
it "404s and sets X-Cascade header when no route satisfies the request" do
mock_app {
get('/foo') { }
}
get '/bar'
assert_equal 404, status
assert_equal 'pass', response.headers['X-Cascade']
end
it "overrides the content-type in error handlers" do
mock_app {
before { content_type 'text/plain' }
@ -462,6 +471,23 @@ class RoutingTest < Test::Unit::TestCase
assert not_found?
end
it "transitions to 404 and sets X-Cascade header when passed and no subsequent route matches" do
mock_app {
get '/:foo' do
pass
'Hello Foo'
end
get '/bar' do
'Hello Bar'
end
}
get '/foo'
assert not_found?
assert_equal 'pass', response.headers['X-Cascade']
end
it "uses optional block passed to pass as route block if no other route is found" do
mock_app {
get "/" do