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/test/helpers_test.rb CHANGED
@@ -1,361 +1,497 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- describe 'Sinatra::Helpers' do
4
- describe '#status' do
5
- setup do
6
- mock_app {
7
- get '/' do
8
- status 207
9
- nil
10
- end
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
- it 'sets the response status code' do
15
- get '/'
16
- assert_equal 207, response.status
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
- describe '#body' do
21
- it 'takes a block for defered body generation' do
22
- mock_app {
23
- get '/' do
24
- body { 'Hello World' }
25
- end
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
- get '/'
29
- assert_equal 'Hello World', body
30
- end
27
+ get '/'
28
+ assert_equal 'Hello World', body
29
+ end
31
30
 
32
- it 'takes a String, Array, or other object responding to #each' do
33
- mock_app {
34
- get '/' do
35
- body 'Hello World'
36
- end
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
- get '/'
40
- assert_equal 'Hello World', body
41
- end
38
+ get '/'
39
+ assert_equal 'Hello World', body
42
40
  end
41
+ end
43
42
 
44
- describe '#redirect' do
45
- it 'uses a 302 when only a path is given' do
46
- mock_app {
47
- get '/' do
48
- redirect '/foo'
49
- fail 'redirect should halt'
50
- end
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
- get '/'
68
- assert_equal 301, status
69
- assert_equal '', body
70
- assert_equal '/foo', response['Location']
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
- describe '#error' do
75
- it 'sets a status code and halts' do
76
- mock_app {
77
- get '/' do
78
- error 501
79
- fail 'error should halt'
80
- end
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
- get '/'
110
- assert_equal 500, status
111
- assert_equal 'FAIL', body
112
- end
66
+ get '/'
67
+ assert_equal 301, status
68
+ assert_equal '', body
69
+ assert_equal '/foo', response['Location']
113
70
  end
114
71
 
115
- describe '#not_found' do
116
- it 'halts with a 404 status' do
117
- mock_app {
118
- get '/' do
119
- not_found
120
- fail 'not_found should halt'
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
- get '/'
125
- assert_equal 404, status
126
- assert_equal '', body
127
- end
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
- describe '#session' do
131
- it 'uses the existing rack.session' do
132
- mock_app {
133
- get '/' do
134
- session[:foo]
135
- end
136
- }
137
-
138
- get '/', :env => { 'rack.session' => { :foo => 'bar' } }
139
- assert_equal 'bar', body
140
- end
141
-
142
- it 'creates a new session when none provided' do
143
- mock_app {
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
- describe '#send_file' do
230
- before {
231
- @file = File.dirname(__FILE__) + '/file.txt'
232
- File.open(@file, 'wb') { |io| io.write('Hello World') }
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
- after {
235
- File.unlink @file
236
- @file = nil
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
- def send_file_app
240
- path = @file
241
- mock_app {
242
- get '/file.txt' do
243
- send_file path
244
- end
245
- }
246
- end
247
-
248
- it "sends the contents of the file" do
249
- send_file_app
250
- get '/file.txt'
251
- assert ok?
252
- assert_equal 'Hello World', body
253
- end
254
-
255
- it 'sets the Content-Type response header if a mime-type can be located' do
256
- send_file_app
257
- get '/file.txt'
258
- assert_equal 'text/plain', response['Content-Type']
259
- end
260
-
261
- it 'sets the Content-Length response header' do
262
- send_file_app
263
- get '/file.txt'
264
- assert_equal 'Hello World'.length.to_s, response['Content-Length']
265
- end
266
-
267
- it 'sets the Last-Modified response header' do
268
- send_file_app
269
- get '/file.txt'
270
- assert_equal File.mtime(@file).httpdate, response['Last-Modified']
271
- end
272
-
273
- it "returns a 404 when not found" do
274
- mock_app {
275
- get '/' do
276
- send_file 'this-file-does-not-exist.txt'
277
- end
278
- }
279
- get '/'
280
- assert not_found?
281
- end
282
- end
283
-
284
- describe '#last_modified' do
285
- before do
286
- now = Time.now
287
- mock_app {
288
- get '/' do
289
- body { 'Hello World' }
290
- last_modified now
291
- 'Boo!'
292
- end
293
- }
294
- @now = now
295
- end
296
-
297
- it 'sets the Last-Modified header to a valid RFC 2616 date value' do
298
- get '/'
299
- assert_equal @now.httpdate, response['Last-Modified']
300
- end
301
-
302
- it 'returns a body when conditional get misses' do
303
- get '/'
304
- assert_equal 200, status
305
- assert_equal 'Boo!', body
306
- end
307
-
308
- it 'halts when a conditional GET matches' do
309
- get '/', :env => { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
310
- assert_equal 304, status
311
- assert_equal '', body
312
- end
313
- end
314
-
315
- describe '#etag' do
316
- before do
317
- mock_app {
318
- get '/' do
319
- body { 'Hello World' }
320
- etag 'FOO'
321
- 'Boo!'
322
- end
323
- }
324
- end
325
-
326
- it 'sets the ETag header' do
327
- get '/'
328
- assert_equal '"FOO"', response['ETag']
329
- end
330
-
331
- it 'returns a body when conditional get misses' do
332
- get '/'
333
- assert_equal 200, status
334
- assert_equal 'Boo!', body
335
- end
336
-
337
- it 'halts when a conditional GET matches' do
338
- get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"FOO"' }
339
- assert_equal 304, status
340
- assert_equal '', body
341
- end
342
-
343
- it 'should handle multiple ETag values in If-None-Match header' do
344
- get '/', :env => { 'HTTP_IF_NONE_MATCH' => '"BAR", *' }
345
- assert_equal 304, status
346
- assert_equal '', body
347
- end
348
-
349
- it 'uses a weak etag with the :weak option' do
350
- mock_app {
351
- get '/' do
352
- etag 'FOO', :weak
353
- "that's weak, dude."
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
- get '/'
357
- assert_equal 'W/"FOO"', response['ETag']
358
- end
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 &lt; 43', body
360
496
  end
361
497
  end