tests refactored
[sinatra.git] / test / dispatching_test.rb
blob49566677cfd9546e7ff0468a9261a60fde679a4b
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   end
16     
17   specify "should return the correct block" do
18     r = get '/' do
19       'main'
20     end
21             
22     result = Sinatra.determine_route(:get, '/')
23     result.block.should.be r.block
24   end
25   
26   specify "should return custom 404" do
27     Sinatra.routes[404] = r = Proc.new { 'custom 404' }
28         
29     result = Sinatra.determine_route(:get, '/')
30     result.should.be r
31   end
32   
33   specify "should return standard 404" do
34     get_it '/'
35     body.should.equal '<h1>Not Found</h1>'
36   end
37   
38   specify "should give custom 500 if error when called" do
39     Sinatra.routes[500] = r = Proc.new { 'custom 500' }
41     get '/' do
42       raise 'asdf'
43     end
44     
45     dont_raise_errors do
46       get_it '/'
47     end
49     body.should.equal 'custom 500'
50   end
51   
52   specify "should give standard 500 if error when called" do
53     get '/' do
54       raise 'asdf'
55     end
56     
57     dont_raise_errors do
58       get_it '/'
59     end
61     body.should.match(/^asdf/)
62   end
63   
64   specify "should run in a context" do
65     Sinatra::EventContext.any_instance.expects(:foo).returns 'in foo'
66     
67     get '/' do
68       foo
69     end
70     
71     get_it '/'
72     body.should.equal 'in foo'
73   end
74   
75   specify "has access to the request" do
76     
77     get '/blake' do
78       request.path_info
79     end
80     
81     get_it '/blake'
82     
83     body.should.equal '/blake'
84     
85   end
86   
87   specify "has DSLified methods for response" do
88     get '/' do
89       status 555
90       'uh oh'
91     end
92     
93     get_it '/'
95     body.should.equal "uh oh"
96     status.should.equal 555
97   end
98           
99 end
101 context "An Event in test mode" do
102   
103   include Sinatra::Test::Methods
105   setup do
106     Sinatra.routes.clear
107   end
109   specify "should raise errors to top" do
110     get '/' do
111       raise 'asdf'
112     end
113       
114     lambda { get_it '/' }.should.raise(RuntimeError)
115   end