helpers
[sinatra.git] / test / dispatching_test.rb
blobb71f288a07c7b637b306255d3b854810cfd0e4bb
1 require File.dirname(__FILE__) + '/helper'
3 context "Dispatching" do
4         
5   setup do
6     Sinatra.config = nil
7     Sinatra.routes.clear
8     Sinatra.setup_default_events!
9   end
10     
11   specify "should return the correct block" do
12     r = get '/' do
13       'main'
14     end
15         
16     result = Sinatra.determine_route(:get, '/')
17     result.path.should.equal r.first.path
18   end
19   
20   specify "should return custom 404" do
21     Sinatra.routes[404] = r = Proc.new { 'custom 404' }
22         
23     result = Sinatra.determine_route(:get, '/')
24     result.should.be r
25   end
26   
27   specify "should return standard 404" do
28     get_it '/'
29     status.should.equal 404
30     body.should.equal '<h1>Not Found</h1>'
31   end
32     
33   specify "should give custom 500 if error when called" do
34     Sinatra.routes[500] = r = Proc.new { 'custom 500' }
36     get '/' do
37       raise 'asdf'
38     end
39     
40     dont_raise_errors do
41       get_it '/'
42     end
44     body.should.equal 'custom 500'
45   end
46   
47   specify "should give standard 500 if error when called" do
48     get '/' do
49       raise 'asdf'
50     end
51     
52     dont_raise_errors do
53       get_it '/'
54     end
56     body.should.match(/^asdf/)
57   end
58   
59   specify "should run in a context" do
60     Sinatra::EventContext.any_instance.expects(:foo).returns 'in foo'
61     
62     get '/' do
63       foo
64     end
65     
66     get_it '/'
67     body.should.equal 'in foo'
68   end
69   
70   specify "has access to the request" do
71     
72     get '/blake' do
73       request.path_info
74     end
75     
76     get_it '/blake'
77     
78     body.should.equal '/blake'
79     
80   end
81   
82   specify "has DSLified methods for response" do
83     get '/' do
84       status 555
85       'uh oh'
86     end
87     
88     get_it '/'
90     body.should.equal "uh oh"
91     status.should.equal 555
92   end
93   
94   specify "should give format for free" do
95     get '/formatted' do
96       params[:format].should.equal 'xml'
97     end
98     
99     get_it '/formatted.xml'
100     
101     should.be.ok
102   end
103   
104   specify "should give format default html format for free" do
105     get '/formatted' do
106       params[:format].should.equal 'html'
107     end
108     
109     get_it '/formatted'
110     
111     should.be.ok
112   end
113           
116 context "An Event in test mode" do
117   
118   setup do
119     Sinatra.routes.clear
120     Sinatra.setup_default_events!
121   end
123   specify "should raise errors to top" do
124     get '/' do
125       raise 'asdf'
126     end
127       
128     lambda { get_it '/' }.should.raise(RuntimeError)
129   end