Errors should know and set their error code
[sinatra.git] / test / dispatching_test.rb
blobef490f668d36df8a65efc75b9a416e49c0483f9e
1 require File.dirname(__FILE__) + '/helper'
3 context "Dispatching" do
4     
5   include Sinatra::Test::Methods
6       
7   def dont_raise_errors
8     Sinatra.config[:raise_errors] = false
9     yield
10     Sinatra.config[:raise_errors] = true
11   end
12     
13   setup do
14     Sinatra.routes.clear
15     Sinatra.setup_default_events!
16   end
17     
18   specify "should return the correct block" do
19     r = get '/' do
20       'main'
21     end
22             
23     result = Sinatra.determine_route(:get, '/')
24     result.block.should.be r.block
25   end
26   
27   specify "should return custom 404" do
28     Sinatra.routes[404] = r = Proc.new { 'custom 404' }
29         
30     result = Sinatra.determine_route(:get, '/')
31     result.should.be r
32   end
33   
34   specify "should return standard 404" do
35     get_it '/'
36     status.should.equal 404
37     body.should.equal '<h1>Not Found</h1>'
38   end
39     
40   specify "should give custom 500 if error when called" do
41     Sinatra.routes[500] = r = Proc.new { 'custom 500' }
43     get '/' do
44       raise 'asdf'
45     end
46     
47     dont_raise_errors do
48       get_it '/'
49     end
51     body.should.equal 'custom 500'
52   end
53   
54   specify "should give standard 500 if error when called" do
55     get '/' do
56       raise 'asdf'
57     end
58     
59     dont_raise_errors do
60       get_it '/'
61     end
63     body.should.match(/^asdf/)
64   end
65   
66   specify "should run in a context" do
67     Sinatra::EventContext.any_instance.expects(:foo).returns 'in foo'
68     
69     get '/' do
70       foo
71     end
72     
73     get_it '/'
74     body.should.equal 'in foo'
75   end
76   
77   specify "has access to the request" do
78     
79     get '/blake' do
80       request.path_info
81     end
82     
83     get_it '/blake'
84     
85     body.should.equal '/blake'
86     
87   end
88   
89   specify "has DSLified methods for response" do
90     get '/' do
91       status 555
92       'uh oh'
93     end
94     
95     get_it '/'
97     body.should.equal "uh oh"
98     status.should.equal 555
99   end
100           
103 context "An Event in test mode" do
104   
105   include Sinatra::Test::Methods
107   setup do
108     Sinatra.routes.clear
109     Sinatra.setup_default_events!
110   end
112   specify "should raise errors to top" do
113     get '/' do
114       raise 'asdf'
115     end
116       
117     lambda { get_it '/' }.should.raise(RuntimeError)
118   end