sinatra 0.9.0.5 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/AUTHORS +8 -7
- data/CHANGES +70 -0
- data/README.rdoc +86 -155
- data/Rakefile +9 -59
- data/compat/app_test.rb +3 -21
- data/compat/application_test.rb +0 -72
- data/compat/pipeline_test.rb +0 -26
- data/compat/sessions_test.rb +3 -0
- data/compat/streaming_test.rb +15 -3
- data/lib/sinatra/base.rb +290 -142
- data/lib/sinatra/compat.rb +30 -30
- data/lib/sinatra/main.rb +4 -5
- data/lib/sinatra/test/bacon.rb +2 -0
- data/lib/sinatra/test/rspec.rb +2 -0
- data/lib/sinatra/test/spec.rb +2 -0
- data/lib/sinatra/test/unit.rb +2 -0
- data/lib/sinatra/test.rb +62 -50
- data/sinatra.gemspec +7 -3
- data/test/base_test.rb +108 -46
- data/test/erb_test.rb +31 -0
- data/test/extensions_test.rb +84 -0
- data/test/helper.rb +65 -9
- data/test/helpers_test.rb +469 -333
- data/test/mapped_error_test.rb +6 -6
- data/test/middleware_test.rb +13 -3
- data/test/options_test.rb +278 -1
- data/test/reload_test.rb +7 -0
- data/test/response_test.rb +42 -0
- data/test/result_test.rb +10 -0
- data/test/routing_test.rb +269 -2
- data/test/server_test.rb +41 -0
- data/test/static_test.rb +8 -25
- data/test/test_test.rb +144 -0
- metadata +13 -2
data/test/helpers_test.rb
CHANGED
@@ -1,361 +1,497 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/helper'
|
2
2
|
|
3
|
-
describe '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
3
|
+
describe 'Helpers#status' do
|
4
|
+
before do
|
5
|
+
mock_app {
|
6
|
+
get '/' do
|
7
|
+
status 207
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
}
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
13
|
+
it 'sets the response status code' do
|
14
|
+
get '/'
|
15
|
+
assert_equal 207, response.status
|
18
16
|
end
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
describe 'Helpers#body' do
|
20
|
+
it 'takes a block for defered body generation' do
|
21
|
+
mock_app {
|
22
|
+
get '/' do
|
23
|
+
body { 'Hello World' }
|
24
|
+
end
|
25
|
+
}
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
get '/'
|
28
|
+
assert_equal 'Hello World', body
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
it 'takes a String, Array, or other object responding to #each' do
|
32
|
+
mock_app {
|
33
|
+
get '/' do
|
34
|
+
body 'Hello World'
|
35
|
+
end
|
36
|
+
}
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
38
|
+
get '/'
|
39
|
+
assert_equal 'Hello World', body
|
42
40
|
end
|
41
|
+
end
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
get '/'
|
54
|
-
assert_equal 302, status
|
55
|
-
assert_equal '', body
|
56
|
-
assert_equal '/foo', response['Location']
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'uses the code given when specified' do
|
60
|
-
mock_app {
|
61
|
-
get '/' do
|
62
|
-
redirect '/foo', 301
|
63
|
-
fail 'redirect should halt'
|
64
|
-
end
|
65
|
-
}
|
43
|
+
describe 'Helpers#redirect' do
|
44
|
+
it 'uses a 302 when only a path is given' do
|
45
|
+
mock_app {
|
46
|
+
get '/' do
|
47
|
+
redirect '/foo'
|
48
|
+
fail 'redirect should halt'
|
49
|
+
end
|
50
|
+
}
|
66
51
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
52
|
+
get '/'
|
53
|
+
assert_equal 302, status
|
54
|
+
assert_equal '', body
|
55
|
+
assert_equal '/foo', response['Location']
|
72
56
|
end
|
73
57
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
get '/'
|
84
|
-
assert_equal 501, status
|
85
|
-
assert_equal '', body
|
86
|
-
end
|
87
|
-
|
88
|
-
it 'takes an optional body' do
|
89
|
-
mock_app {
|
90
|
-
get '/' do
|
91
|
-
error 501, 'FAIL'
|
92
|
-
fail 'error should halt'
|
93
|
-
end
|
94
|
-
}
|
95
|
-
|
96
|
-
get '/'
|
97
|
-
assert_equal 501, status
|
98
|
-
assert_equal 'FAIL', body
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'uses a 500 status code when first argument is a body' do
|
102
|
-
mock_app {
|
103
|
-
get '/' do
|
104
|
-
error 'FAIL'
|
105
|
-
fail 'error should halt'
|
106
|
-
end
|
107
|
-
}
|
58
|
+
it 'uses the code given when specified' do
|
59
|
+
mock_app {
|
60
|
+
get '/' do
|
61
|
+
redirect '/foo', 301
|
62
|
+
fail 'redirect should halt'
|
63
|
+
end
|
64
|
+
}
|
108
65
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
66
|
+
get '/'
|
67
|
+
assert_equal 301, status
|
68
|
+
assert_equal '', body
|
69
|
+
assert_equal '/foo', response['Location']
|
113
70
|
end
|
114
71
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
end
|
122
|
-
}
|
72
|
+
it 'redirects back to request.referer when passed back' do
|
73
|
+
mock_app {
|
74
|
+
get '/try_redirect' do
|
75
|
+
redirect back
|
76
|
+
end
|
77
|
+
}
|
123
78
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
79
|
+
request = Rack::MockRequest.new(@app)
|
80
|
+
response = request.get('/try_redirect', 'HTTP_REFERER' => '/foo')
|
81
|
+
assert_equal 302, response.status
|
82
|
+
assert_equal '/foo', response['Location']
|
128
83
|
end
|
129
84
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
get '/' do
|
145
|
-
assert session.empty?
|
146
|
-
session[:foo] = 'bar'
|
147
|
-
'Hi'
|
148
|
-
end
|
149
|
-
}
|
150
|
-
|
151
|
-
get '/'
|
152
|
-
assert_equal 'Hi', body
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
describe '#media_type' do
|
157
|
-
include Sinatra::Helpers
|
158
|
-
it "looks up media types in Rack's MIME registry" do
|
159
|
-
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
160
|
-
assert_equal 'application/foo', media_type('foo')
|
161
|
-
assert_equal 'application/foo', media_type('.foo')
|
162
|
-
assert_equal 'application/foo', media_type(:foo)
|
163
|
-
end
|
164
|
-
it 'returns nil when given nil' do
|
165
|
-
assert media_type(nil).nil?
|
166
|
-
end
|
167
|
-
it 'returns nil when media type not registered' do
|
168
|
-
assert media_type(:bizzle).nil?
|
169
|
-
end
|
170
|
-
it 'returns the argument when given a media type string' do
|
171
|
-
assert_equal 'text/plain', media_type('text/plain')
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe '#content_type' do
|
176
|
-
it 'sets the Content-Type header' do
|
177
|
-
mock_app {
|
178
|
-
get '/' do
|
179
|
-
content_type 'text/plain'
|
180
|
-
'Hello World'
|
181
|
-
end
|
182
|
-
}
|
183
|
-
|
184
|
-
get '/'
|
185
|
-
assert_equal 'text/plain', response['Content-Type']
|
186
|
-
assert_equal 'Hello World', body
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'takes media type parameters (like charset=)' do
|
190
|
-
mock_app {
|
191
|
-
get '/' do
|
192
|
-
content_type 'text/html', :charset => 'utf-8'
|
193
|
-
"<h1>Hello, World</h1>"
|
194
|
-
end
|
195
|
-
}
|
196
|
-
|
197
|
-
get '/'
|
198
|
-
assert ok?
|
199
|
-
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
200
|
-
assert_equal "<h1>Hello, World</h1>", body
|
201
|
-
end
|
202
|
-
|
203
|
-
it "looks up symbols in Rack's mime types dictionary" do
|
204
|
-
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
205
|
-
mock_app {
|
206
|
-
get '/foo.xml' do
|
207
|
-
content_type :foo
|
208
|
-
"I AM FOO"
|
209
|
-
end
|
210
|
-
}
|
211
|
-
|
212
|
-
get '/foo.xml'
|
213
|
-
assert ok?
|
214
|
-
assert_equal 'application/foo', response['Content-Type']
|
215
|
-
assert_equal 'I AM FOO', body
|
216
|
-
end
|
217
|
-
|
218
|
-
it 'fails when no mime type is registered for the argument provided' do
|
219
|
-
mock_app {
|
220
|
-
get '/foo.xml' do
|
221
|
-
content_type :bizzle
|
222
|
-
"I AM FOO"
|
223
|
-
end
|
224
|
-
}
|
225
|
-
assert_raise(RuntimeError) { get '/foo.xml' }
|
226
|
-
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe 'Helpers#error' do
|
88
|
+
it 'sets a status code and halts' do
|
89
|
+
mock_app {
|
90
|
+
get '/' do
|
91
|
+
error 501
|
92
|
+
fail 'error should halt'
|
93
|
+
end
|
94
|
+
}
|
95
|
+
|
96
|
+
get '/'
|
97
|
+
assert_equal 501, status
|
98
|
+
assert_equal '', body
|
227
99
|
end
|
228
100
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
101
|
+
it 'takes an optional body' do
|
102
|
+
mock_app {
|
103
|
+
get '/' do
|
104
|
+
error 501, 'FAIL'
|
105
|
+
fail 'error should halt'
|
106
|
+
end
|
233
107
|
}
|
234
|
-
|
235
|
-
|
236
|
-
|
108
|
+
|
109
|
+
get '/'
|
110
|
+
assert_equal 501, status
|
111
|
+
assert_equal 'FAIL', body
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'uses a 500 status code when first argument is a body' do
|
115
|
+
mock_app {
|
116
|
+
get '/' do
|
117
|
+
error 'FAIL'
|
118
|
+
fail 'error should halt'
|
119
|
+
end
|
237
120
|
}
|
238
121
|
|
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
|
-
get '/
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
end
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
122
|
+
get '/'
|
123
|
+
assert_equal 500, status
|
124
|
+
assert_equal 'FAIL', body
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe 'Helpers#not_found' do
|
129
|
+
it 'halts with a 404 status' do
|
130
|
+
mock_app {
|
131
|
+
get '/' do
|
132
|
+
not_found
|
133
|
+
fail 'not_found should halt'
|
134
|
+
end
|
135
|
+
}
|
136
|
+
|
137
|
+
get '/'
|
138
|
+
assert_equal 404, status
|
139
|
+
assert_equal '', body
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe 'Helpers#headers' do
|
144
|
+
it 'sets headers on the response object when given a Hash' do
|
145
|
+
mock_app {
|
146
|
+
get '/' do
|
147
|
+
headers 'X-Foo' => 'bar', 'X-Baz' => 'bling'
|
148
|
+
'kthx'
|
149
|
+
end
|
150
|
+
}
|
151
|
+
|
152
|
+
get '/'
|
153
|
+
assert ok?
|
154
|
+
assert_equal 'bar', response['X-Foo']
|
155
|
+
assert_equal 'bling', response['X-Baz']
|
156
|
+
assert_equal 'kthx', body
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'returns the response headers hash when no hash provided' do
|
160
|
+
mock_app {
|
161
|
+
get '/' do
|
162
|
+
headers['X-Foo'] = 'bar'
|
163
|
+
'kthx'
|
164
|
+
end
|
165
|
+
}
|
166
|
+
|
167
|
+
get '/'
|
168
|
+
assert ok?
|
169
|
+
assert_equal 'bar', response['X-Foo']
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe 'Helpers#session' do
|
174
|
+
it 'uses the existing rack.session' do
|
175
|
+
mock_app {
|
176
|
+
get '/' do
|
177
|
+
session[:foo]
|
178
|
+
end
|
179
|
+
}
|
180
|
+
|
181
|
+
get '/', :env => { 'rack.session' => { :foo => 'bar' } }
|
182
|
+
assert_equal 'bar', body
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'creates a new session when none provided' do
|
186
|
+
mock_app {
|
187
|
+
get '/' do
|
188
|
+
assert session.empty?
|
189
|
+
session[:foo] = 'bar'
|
190
|
+
'Hi'
|
191
|
+
end
|
192
|
+
}
|
193
|
+
|
194
|
+
get '/'
|
195
|
+
assert_equal 'Hi', body
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe 'Helpers#media_type' do
|
200
|
+
include Sinatra::Helpers
|
201
|
+
|
202
|
+
it "looks up media types in Rack's MIME registry" do
|
203
|
+
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
204
|
+
assert_equal 'application/foo', media_type('foo')
|
205
|
+
assert_equal 'application/foo', media_type('.foo')
|
206
|
+
assert_equal 'application/foo', media_type(:foo)
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'returns nil when given nil' do
|
210
|
+
assert media_type(nil).nil?
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'returns nil when media type not registered' do
|
214
|
+
assert media_type(:bizzle).nil?
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'returns the argument when given a media type string' do
|
218
|
+
assert_equal 'text/plain', media_type('text/plain')
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe 'Helpers#content_type' do
|
223
|
+
it 'sets the Content-Type header' do
|
224
|
+
mock_app {
|
225
|
+
get '/' do
|
226
|
+
content_type 'text/plain'
|
227
|
+
'Hello World'
|
228
|
+
end
|
229
|
+
}
|
230
|
+
|
231
|
+
get '/'
|
232
|
+
assert_equal 'text/plain', response['Content-Type']
|
233
|
+
assert_equal 'Hello World', body
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'takes media type parameters (like charset=)' do
|
237
|
+
mock_app {
|
238
|
+
get '/' do
|
239
|
+
content_type 'text/html', :charset => 'utf-8'
|
240
|
+
"<h1>Hello, World</h1>"
|
241
|
+
end
|
242
|
+
}
|
243
|
+
|
244
|
+
get '/'
|
245
|
+
assert ok?
|
246
|
+
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
247
|
+
assert_equal "<h1>Hello, World</h1>", body
|
248
|
+
end
|
249
|
+
|
250
|
+
it "looks up symbols in Rack's mime types dictionary" do
|
251
|
+
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
252
|
+
mock_app {
|
253
|
+
get '/foo.xml' do
|
254
|
+
content_type :foo
|
255
|
+
"I AM FOO"
|
256
|
+
end
|
257
|
+
}
|
258
|
+
|
259
|
+
get '/foo.xml'
|
260
|
+
assert ok?
|
261
|
+
assert_equal 'application/foo', response['Content-Type']
|
262
|
+
assert_equal 'I AM FOO', body
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'fails when no mime type is registered for the argument provided' do
|
266
|
+
mock_app {
|
267
|
+
get '/foo.xml' do
|
268
|
+
content_type :bizzle
|
269
|
+
"I AM FOO"
|
270
|
+
end
|
271
|
+
}
|
272
|
+
assert_raise(RuntimeError) { get '/foo.xml' }
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
describe 'Helpers#send_file' do
|
277
|
+
before do
|
278
|
+
@file = File.dirname(__FILE__) + '/file.txt'
|
279
|
+
File.open(@file, 'wb') { |io| io.write('Hello World') }
|
280
|
+
end
|
281
|
+
|
282
|
+
after do
|
283
|
+
File.unlink @file
|
284
|
+
@file = nil
|
285
|
+
end
|
286
|
+
|
287
|
+
def send_file_app(opts={})
|
288
|
+
path = @file
|
289
|
+
mock_app {
|
290
|
+
get '/file.txt' do
|
291
|
+
send_file path, opts
|
292
|
+
end
|
293
|
+
}
|
294
|
+
end
|
295
|
+
|
296
|
+
it "sends the contents of the file" do
|
297
|
+
send_file_app
|
298
|
+
get '/file.txt'
|
299
|
+
assert ok?
|
300
|
+
assert_equal 'Hello World', body
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'sets the Content-Type response header if a mime-type can be located' do
|
304
|
+
send_file_app
|
305
|
+
get '/file.txt'
|
306
|
+
assert_equal 'text/plain', response['Content-Type']
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'sets the Content-Length response header' do
|
310
|
+
send_file_app
|
311
|
+
get '/file.txt'
|
312
|
+
assert_equal 'Hello World'.length.to_s, response['Content-Length']
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'sets the Last-Modified response header' do
|
316
|
+
send_file_app
|
317
|
+
get '/file.txt'
|
318
|
+
assert_equal File.mtime(@file).httpdate, response['Last-Modified']
|
319
|
+
end
|
320
|
+
|
321
|
+
it "returns a 404 when not found" do
|
322
|
+
mock_app {
|
323
|
+
get '/' do
|
324
|
+
send_file 'this-file-does-not-exist.txt'
|
325
|
+
end
|
326
|
+
}
|
327
|
+
get '/'
|
328
|
+
assert not_found?
|
329
|
+
end
|
330
|
+
|
331
|
+
it "does not set the Content-Disposition header by default" do
|
332
|
+
send_file_app
|
333
|
+
get '/file.txt'
|
334
|
+
assert_nil response['Content-Disposition']
|
335
|
+
end
|
336
|
+
|
337
|
+
it "sets the Content-Disposition header when :disposition set to 'attachment'" do
|
338
|
+
send_file_app :disposition => 'attachment'
|
339
|
+
get '/file.txt'
|
340
|
+
assert_equal 'attachment; filename="file.txt"', response['Content-Disposition']
|
341
|
+
end
|
342
|
+
|
343
|
+
it "sets the Content-Disposition header when :filename provided" do
|
344
|
+
send_file_app :filename => 'foo.txt'
|
345
|
+
get '/file.txt'
|
346
|
+
assert_equal 'attachment; filename="foo.txt"', response['Content-Disposition']
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
describe 'Helpers#last_modified' do
|
351
|
+
before do
|
352
|
+
now = Time.now
|
353
|
+
mock_app {
|
354
|
+
get '/' do
|
355
|
+
body { 'Hello World' }
|
356
|
+
last_modified now
|
357
|
+
'Boo!'
|
358
|
+
end
|
359
|
+
}
|
360
|
+
@now = now
|
361
|
+
end
|
362
|
+
|
363
|
+
it 'sets the Last-Modified header to a valid RFC 2616 date value' do
|
364
|
+
get '/'
|
365
|
+
assert_equal @now.httpdate, response['Last-Modified']
|
366
|
+
end
|
367
|
+
|
368
|
+
it 'returns a body when conditional get misses' do
|
369
|
+
get '/'
|
370
|
+
assert_equal 200, status
|
371
|
+
assert_equal 'Boo!', body
|
372
|
+
end
|
373
|
+
|
374
|
+
it 'halts when a conditional GET matches' do
|
375
|
+
get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
|
376
|
+
assert_equal 304, status
|
377
|
+
assert_equal '', body
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
describe 'Helpers#etag' do
|
382
|
+
before do
|
383
|
+
mock_app {
|
384
|
+
get '/' do
|
385
|
+
body { 'Hello World' }
|
386
|
+
etag 'FOO'
|
387
|
+
'Boo!'
|
388
|
+
end
|
389
|
+
}
|
390
|
+
end
|
391
|
+
|
392
|
+
it 'sets the ETag header' do
|
393
|
+
get '/'
|
394
|
+
assert_equal '"FOO"', response['ETag']
|
395
|
+
end
|
396
|
+
|
397
|
+
it 'returns a body when conditional get misses' do
|
398
|
+
get '/'
|
399
|
+
assert_equal 200, status
|
400
|
+
assert_equal 'Boo!', body
|
401
|
+
end
|
402
|
+
|
403
|
+
it 'halts when a conditional GET matches' do
|
404
|
+
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
|
405
|
+
assert_equal 304, status
|
406
|
+
assert_equal '', body
|
407
|
+
end
|
408
|
+
|
409
|
+
it 'should handle multiple ETag values in If-None-Match header' do
|
410
|
+
get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
|
411
|
+
assert_equal 304, status
|
412
|
+
assert_equal '', body
|
413
|
+
end
|
414
|
+
|
415
|
+
it 'uses a weak etag with the :weak option' do
|
416
|
+
mock_app {
|
417
|
+
get '/' do
|
418
|
+
etag 'FOO', :weak
|
419
|
+
"that's weak, dude."
|
420
|
+
end
|
421
|
+
}
|
422
|
+
get '/'
|
423
|
+
assert_equal 'W/"FOO"', response['ETag']
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
describe 'Helpers#back' do
|
428
|
+
it "makes redirecting back pretty" do
|
429
|
+
mock_app {
|
430
|
+
get '/foo' do
|
431
|
+
redirect back
|
432
|
+
end
|
433
|
+
}
|
434
|
+
|
435
|
+
get '/foo', {}, 'HTTP_REFERER' => 'http://github.com'
|
436
|
+
assert redirect?
|
437
|
+
assert_equal "http://github.com", response.location
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
module HelperOne; def one; '1'; end; end
|
442
|
+
module HelperTwo; def two; '2'; end; end
|
443
|
+
|
444
|
+
describe 'Adding new helpers' do
|
445
|
+
it 'takes a list of modules to mix into the app' do
|
446
|
+
mock_app {
|
447
|
+
helpers HelperOne, HelperTwo
|
448
|
+
|
449
|
+
get '/one' do
|
450
|
+
one
|
451
|
+
end
|
452
|
+
|
453
|
+
get '/two' do
|
454
|
+
two
|
455
|
+
end
|
456
|
+
}
|
457
|
+
|
458
|
+
get '/one'
|
459
|
+
assert_equal '1', body
|
460
|
+
|
461
|
+
get '/two'
|
462
|
+
assert_equal '2', body
|
463
|
+
end
|
464
|
+
|
465
|
+
it 'takes a block to mix into the app' do
|
466
|
+
mock_app {
|
467
|
+
helpers do
|
468
|
+
def foo
|
469
|
+
'foo'
|
354
470
|
end
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
471
|
+
end
|
472
|
+
|
473
|
+
get '/' do
|
474
|
+
foo
|
475
|
+
end
|
476
|
+
}
|
477
|
+
|
478
|
+
get '/'
|
479
|
+
assert_equal 'foo', body
|
480
|
+
end
|
481
|
+
|
482
|
+
it 'evaluates the block in class context so that methods can be aliased' do
|
483
|
+
mock_app {
|
484
|
+
helpers do
|
485
|
+
alias_method :h, :escape_html
|
486
|
+
end
|
487
|
+
|
488
|
+
get '/' do
|
489
|
+
h('42 < 43')
|
490
|
+
end
|
491
|
+
}
|
359
492
|
|
493
|
+
get '/'
|
494
|
+
assert ok?
|
495
|
+
assert_equal '42 < 43', body
|
360
496
|
end
|
361
497
|
end
|