breaking out into separate files
[sinatra.git] / test / filter_test.rb
blobd6febca8175f555e0b6fc0d1e2bff16a52db96d9
1 require File.dirname(__FILE__) + '/helper'
3 class CustomResult
4   
5   def to_result(cx, *args)
6     cx.status 404
7     cx.body "Can't find this shit!"
8   end
9   
10 end
12 context "Filters" do
13   
14   setup do
15     Sinatra.reset!
16   end
18   specify "halts when told" do
19   
20     before do
21       throw :halt, 'fubar'
22     end
23   
24     get '/' do
25       'not this'
26     end
27     
28     get_it '/'
29     
30     should.be.ok
31     body.should.equal 'fubar'
32     
33   end
34   
35   specify "halts with status" do
36     
37     before do
38       throw :halt, [401, 'get out dude!']
39     end
40     
41     get '/auth' do
42       "you're in!"
43     end
44     
45     get_it '/auth'
46     
47     status.should.equal 401
48     body.should.equal 'get out dude!'
49     
50   end
51   
52   specify "halts with custom result" do
53     
54     before do
55       throw :halt, CustomResult.new
56     end
57     
58     get '/custom' do
59       'not this'
60     end
61     
62     get_it '/custom'
63     
64     should.be.not_found
65     body.should.equal "Can't find this shit!"
66     
67   end
68   
69 end
71 context "Filter grouping" do
72   
73   setup do
74     Sinatra.reset!
75   end
77   specify "befores only run for groups if specified" do
79     Sinatra::EventContext.any_instance.expects(:foo).times(4)
80     
81     before do
82       foo  # this should be called before all events
83     end
84     
85     after do
86       foo
87     end
88     
89     before :admins do
90       throw :halt, 'not authorized'
91     end
92     
93     get '/', :groups => :admins do
94       'asdf'
95     end
96     
97     get '/foo' do
98       'yeah!'
99     end
100             
101     get_it '/'
102   
103     should.be.ok
104     body.should.equal 'not authorized'
105     
106     get_it '/foo'
107     
108     should.be.ok
109     body.should.equal 'yeah!'
111   end
112