From ed19b2a1a27ebb52ee424146167b651ecbef9586 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Sat, 24 Nov 2007 17:12:07 -0800 Subject: [PATCH] throw :halt within requests --- lib/sinatra.rb | 11 ++++++++++- test/event_dsl_test.rb | 20 +++++++++++++++++++- test/filter_test.rb | 4 ++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 56f0079b..fb9ef759 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -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 diff --git a/test/event_dsl_test.rb b/test/event_dsl_test.rb index 9d269535..c88cfe6a 100644 --- a/test/event_dsl_test.rb +++ b/test/event_dsl_test.rb @@ -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 diff --git a/test/filter_test.rb b/test/filter_test.rb index 55fd1f0c..2b5bc72f 100644 --- a/test/filter_test.rb +++ b/test/filter_test.rb @@ -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