public
Description: (offically at github.com/sinatra/sinatra) Classy web-development dressed in a DSL
Homepage: http://sinatra.github.com
Clone URL: git://github.com/bmizerany/sinatra.git
sinatra / compat / app_test.rb
100644 283 lines (227 sloc) 5.922 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require File.dirname(__FILE__) + '/helper'
 
context "Sinatra" do
 
  setup do
    Sinatra.application = nil
  end
 
  specify "should put all DSL methods on (main)" do
    object = Object.new
    methods = %w[get put post head delete configure template helpers set]
    methods.each do |method|
      object.private_methods.map { |m| m.to_sym }.should.include(method.to_sym)
    end
  end
 
  specify "should handle result of nil" do
    get '/' do
      nil
    end
 
    get_it '/'
    should.be.ok
    body.should == ''
  end
 
  specify "handles events" do
    get '/:name' do
      'Hello ' + params["name"]
    end
 
    get_it '/Blake'
 
    should.be.ok
    body.should.equal 'Hello Blake'
  end
 
 
  specify "handles splats" do
    get '/hi/*' do
      params["splat"].kind_of?(Array).should.equal true
      params["splat"].first
    end
 
    get_it '/hi/Blake'
 
    should.be.ok
    body.should.equal 'Blake'
  end
 
  specify "handles multiple splats" do
    get '/say/*/to/*' do
      params["splat"].join(' ')
    end
 
    get_it '/say/hello/to/world'
 
    should.be.ok
    body.should.equal 'hello world'
  end
 
  specify "allow empty splats" do
    get '/say/*/to*/*' do
      params["splat"].join(' ')
    end
 
    get_it '/say/hello/to/world'
 
    should.be.ok
    body.should.equal 'hello world' # second splat is empty
 
    get_it '/say/hello/tomy/world'
 
    should.be.ok
    body.should.equal 'hello my world'
  end
 
  specify "gives access to underlying response header Hash" do
    get '/' do
      header['X-Test'] = 'Is this thing on?'
      headers 'X-Test2' => 'Foo', 'X-Test3' => 'Bar'
      ''
    end
 
    get_it '/'
    should.be.ok
    headers.should.include 'X-Test'
    headers['X-Test'].should.equal 'Is this thing on?'
    headers.should.include 'X-Test3'
    headers['X-Test3'].should.equal 'Bar'
  end
 
  specify "follows redirects" do
    get '/' do
      redirect '/blake'
    end
 
    get '/blake' do
      'Mizerany'
    end
 
    get_it '/'
    should.be.redirection
    body.should.equal ''
 
    follow!
    should.be.ok
    body.should.equal 'Mizerany'
  end
 
  specify "renders a body with a redirect" do
    helpers do
      def foo ; 'blah' ; end
    end
    get "/" do
      redirect 'foo', :foo
    end
    get_it '/'
    should.be.redirection
    headers['Location'].should.equal 'foo'
    body.should.equal 'blah'
  end
 
  specify "redirects permanently with 301 status code" do
    get "/" do
      redirect 'foo', 301
    end
    get_it '/'
    should.be.redirection
    headers['Location'].should.equal 'foo'
    status.should.equal 301
    body.should.be.empty
  end
 
  specify "stop sets content and ends event" do
    get '/set_body' do
      stop 'Hello!'
      stop 'World!'
      fail 'stop should have halted'
    end
 
    get_it '/set_body'
 
    should.be.ok
    body.should.equal 'Hello!'
 
  end
 
  specify "should easily set response Content-Type" do
    get '/foo.html' do
      content_type 'text/html', :charset => 'utf-8'
      "<h1>Hello, World</h1>"
    end
 
    get_it '/foo.html'
    should.be.ok
    headers['Content-Type'].should.equal 'text/html;charset=utf-8'
    body.should.equal '<h1>Hello, World</h1>'
 
    get '/foo_test.xml' do
      content_type :xml
      "<feed></feed>"
    end
 
    get_it '/foo_test.xml'
    should.be.ok
    headers['Content-Type'].should.equal 'application/xml'
    body.should.equal '<feed></feed>'
  end
 
  specify "supports conditional GETs with last_modified" do
    modified_at = Time.now
    get '/maybe' do
      last_modified modified_at
      'response body, maybe'
    end
 
    get_it '/maybe'
    should.be.ok
    body.should.equal 'response body, maybe'
 
    get_it '/maybe', :env => { 'HTTP_IF_MODIFIED_SINCE' => modified_at.httpdate }
    status.should.equal 304
    body.should.equal ''
  end
 
  specify "supports conditional GETs with entity_tag" do
    get '/strong' do
      entity_tag 'FOO'
      'foo response'
    end
 
    get_it '/strong'
    should.be.ok
    body.should.equal 'foo response'
 
    get_it '/strong', {},
      'HTTP_IF_NONE_MATCH' => '"BAR"'
    should.be.ok
    body.should.equal 'foo response'
 
    get_it '/strong', {},
      'HTTP_IF_NONE_MATCH' => '"FOO"'
    status.should.equal 304
    body.should.equal ''
 
    get_it '/strong', {},
      'HTTP_IF_NONE_MATCH' => '"BAR", *'
    status.should.equal 304
    body.should.equal ''
  end
 
  specify "delegates HEAD requests to GET handlers" do
    get '/invisible' do
      "I am invisible to the world"
    end
 
    head_it '/invisible'
    should.be.ok
    body.should.not.equal "I am invisible to the world"
    body.should.equal ''
  end
 
 
  specify "supports PUT" do
    put '/' do
      'puted'
    end
    put_it '/'
    assert_equal 'puted', body
  end
 
  specify "rewrites POSTs with _method param to PUT" do
    put '/' do
      'puted'
    end
    post_it '/', :_method => 'PUT'
    assert_equal 'puted', body
  end
 
  specify "rewrites POSTs with lowercase _method param to PUT" do
    put '/' do
      'puted'
    end
    post_it '/', :_method => 'put'
    body.should.equal 'puted'
  end
 
  specify "does not rewrite GETs with _method param to PUT" do
    get '/' do
      'getted'
    end
    get_it '/', :_method => 'put'
    should.be.ok
    body.should.equal 'getted'
  end
 
  specify "ignores _method query string parameter on non-POST requests" do
    post '/' do
      'posted'
    end
    put '/' do
      'booo'
    end
    post_it "/?_method=PUT"
    should.be.ok
    body.should.equal 'posted'
  end
 
  specify "does not read body if content type is not url encoded" do
    post '/foo.xml' do
      request.env['CONTENT_TYPE'].should.be == 'application/xml'
      request.content_type.should.be == 'application/xml'
      request.body.read
    end
 
    post_it '/foo.xml', '<foo></foo>', :content_type => 'application/xml'
    @response.should.be.ok
    @response.body.should.be == '<foo></foo>'
  end
 
end