throw :halt within requests

This commit is contained in:
Blake Mizerany 2007-11-24 17:12:07 -08:00
parent 2d4e589d68
commit ed19b2a1a2
3 changed files with 31 additions and 4 deletions

View File

@ -188,6 +188,12 @@ module Sinatra
filters[type] << b
end
def reset!
routes.clear
config = nil
setup_default_events!
end
protected
def handle_with_filters(cx, &b)
@ -195,7 +201,10 @@ module Sinatra
filters[:before].each { |x| cx.instance_eval(&x) }
[:complete, b]
end
result = caught.to_result(cx)
caught = catch(:halt) do
caught.to_result(cx)
end
result = caught.to_result(cx) if caught
filters[:after].each { |x| cx.instance_eval(&x) }
cx.body Array(result.to_s)
cx

View File

@ -2,7 +2,11 @@ require File.dirname(__FILE__) + '/helper'
context "Event's DSL" do
specify "Takes multiple routes" do
setup do
Sinatra.reset!
end
specify "takes multiple routes" do
get '/', '/foo' do
'hello from me'
@ -18,4 +22,18 @@ context "Event's DSL" do
end
specify "should be able to halt from within request" do
get '/halting' do
throw :halt, 'halted'
'not this'
end
get_it '/halting'
should.be.ok
body.should.equal 'halted'
end
end

View File

@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/helper'
class NotFound
class CustomResult
def to_result(cx, *args)
cx.status 404
@ -54,7 +54,7 @@ context "Filters" do
specify "halts with custom result" do
before do
throw :halt, NotFound.new
throw :halt, CustomResult.new
end
get '/custom' do