set status, headers and body before hitting after filters. fixes #402.

This commit is contained in:
Konstantin Haase 2012-01-04 00:33:35 +01:00
parent 6a91fa89b8
commit 301556abc3
3 changed files with 21 additions and 4 deletions

View File

@ -12,6 +12,9 @@
Moreover, it will be ignored if the value is not between 400 and 599. You Moreover, it will be ignored if the value is not between 400 and 599. You
should use Exception#http_status instead. (Konstantin Haase) should use Exception#http_status instead. (Konstantin Haase)
* Status, headers and body will be set correctly in an after filter when using
halt in a before filter or route. (Konstantin Haase)
= 1.3.2 / 2011-12-30 = 1.3.2 / 2011-12-30
* Don't automatically add `Rack::CommonLogger` if `Rack::Server` is adding it, * Don't automatically add `Rack::CommonLogger` if `Rack::Server` is adding it,

View File

@ -877,15 +877,18 @@ module Sinatra
elsif res.respond_to? :each elsif res.respond_to? :each
body res body res
end end
nil # avoid double setting the same response tuple twice
end end
# Dispatch a request with error handling. # Dispatch a request with error handling.
def dispatch! def dispatch!
invoke do
static! if settings.static? && (request.get? || request.head?) static! if settings.static? && (request.get? || request.head?)
filter! :before filter! :before
route! route!
end
rescue ::Exception => boom rescue ::Exception => boom
handle_exception!(boom) invoke { handle_exception!(boom) }
ensure ensure
filter! :after unless env['sinatra.static_file'] filter! :after unless env['sinatra.static_file']
end end

View File

@ -531,6 +531,17 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'HelloWorldHowAreYou', body assert_equal 'HelloWorldHowAreYou', body
end end
it 'sets response.status with halt' do
status_was = nil
mock_app do
after { status_was = status }
get('/') { halt 500, 'error' }
end
get '/'
assert_status 500
assert_equal 500, status_was
end
it "transitions to the next matching route on pass" do it "transitions to the next matching route on pass" do
mock_app { mock_app {
get '/:foo' do get '/:foo' do