sinatra-contrib 1.4.7 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -16
- data/Rakefile +0 -1
- data/lib/sinatra/config_file.rb +8 -2
- data/lib/sinatra/content_for.rb +63 -1
- data/lib/sinatra/contrib.rb +1 -1
- data/lib/sinatra/contrib/version.rb +1 -12
- data/lib/sinatra/cookies.rb +1 -1
- data/lib/sinatra/link_header.rb +2 -2
- data/lib/sinatra/namespace.rb +90 -18
- data/lib/sinatra/reloader.rb +15 -1
- data/lib/sinatra/required_params.rb +71 -0
- data/lib/sinatra/respond_with.rb +7 -5
- data/sinatra-contrib.gemspec +14 -10
- data/spec/capture_spec.rb +5 -5
- data/spec/config_file/key_value.yml +1 -0
- data/spec/config_file_spec.rb +27 -13
- data/spec/content_for/footer.haml +1 -1
- data/spec/content_for/footer.slim +1 -1
- data/spec/content_for_spec.rb +65 -36
- data/spec/cookies_spec.rb +167 -173
- data/spec/extension_spec.rb +4 -4
- data/spec/json_spec.rb +11 -11
- data/spec/link_header_spec.rb +13 -13
- data/spec/multi_route_spec.rb +13 -13
- data/spec/namespace_spec.rb +190 -140
- data/spec/reloader_spec.rb +56 -31
- data/spec/required_params_spec.rb +68 -0
- data/spec/respond_with_spec.rb +58 -56
- data/spec/streaming_spec.rb +57 -57
- metadata +37 -22
- data/lib/sinatra/decompile.rb +0 -127
- data/spec/decompile_spec.rb +0 -43
data/spec/reloader_spec.rb
CHANGED
@@ -88,31 +88,31 @@ describe Sinatra::Reloader do
|
|
88
88
|
end
|
89
89
|
|
90
90
|
it "doesn't mess up the application" do
|
91
|
-
get('/foo').body.
|
91
|
+
expect(get('/foo').body).to eq('foo')
|
92
92
|
end
|
93
93
|
|
94
94
|
it "knows when a route has been modified" do
|
95
95
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
96
|
-
get('/foo').body.
|
96
|
+
expect(get('/foo').body).to eq('bar')
|
97
97
|
end
|
98
98
|
|
99
99
|
it "knows when a route has been added" do
|
100
100
|
update_app_file(
|
101
101
|
:routes => ['get("/foo") { "foo" }', 'get("/bar") { "bar" }']
|
102
102
|
)
|
103
|
-
get('/foo').body.
|
104
|
-
get('/bar').body.
|
103
|
+
expect(get('/foo').body).to eq('foo')
|
104
|
+
expect(get('/bar').body).to eq('bar')
|
105
105
|
end
|
106
106
|
|
107
107
|
it "knows when a route has been removed" do
|
108
108
|
update_app_file(:routes => ['get("/bar") { "bar" }'])
|
109
|
-
get('/foo').status.
|
109
|
+
expect(get('/foo').status).to eq(404)
|
110
110
|
end
|
111
111
|
|
112
112
|
it "doesn't try to reload a removed file" do
|
113
113
|
update_app_file(:routes => ['get("/foo") { "i shall not be reloaded" }'])
|
114
114
|
FileUtils.rm app_file_path
|
115
|
-
get('/foo').body.strip.
|
115
|
+
expect(get('/foo').body.strip).to eq('foo')
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
@@ -125,7 +125,7 @@ describe Sinatra::Reloader do
|
|
125
125
|
end
|
126
126
|
|
127
127
|
it "doesn't mess up the application" do
|
128
|
-
get('/foo').body.strip.
|
128
|
+
expect(get('/foo').body.strip).to eq('foo')
|
129
129
|
end
|
130
130
|
|
131
131
|
it "reloads inline templates in the app file" do
|
@@ -133,7 +133,7 @@ describe Sinatra::Reloader do
|
|
133
133
|
:routes => ['get("/foo") { erb :foo }'],
|
134
134
|
:inline_templates => { :foo => 'bar' }
|
135
135
|
)
|
136
|
-
get('/foo').body.strip.
|
136
|
+
expect(get('/foo').body.strip).to eq('bar')
|
137
137
|
end
|
138
138
|
|
139
139
|
it "reloads inline templates in other file" do
|
@@ -144,11 +144,11 @@ describe Sinatra::Reloader do
|
|
144
144
|
end
|
145
145
|
require template_file_path
|
146
146
|
app_const.inline_templates= template_file_path
|
147
|
-
get('/foo').body.strip.
|
147
|
+
expect(get('/foo').body.strip).to eq('foo')
|
148
148
|
update_file(template_file_path) do |f|
|
149
149
|
f.write "__END__\n\n@@foo\nbar"
|
150
150
|
end
|
151
|
-
get('/foo').body.strip.
|
151
|
+
expect(get('/foo').body.strip).to eq('bar')
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
@@ -160,7 +160,7 @@ describe Sinatra::Reloader do
|
|
160
160
|
:middlewares => [Rack::Head]
|
161
161
|
)
|
162
162
|
get('/foo') # ...to perform the reload
|
163
|
-
app_const.middleware.
|
163
|
+
expect(app_const.middleware).not_to be_empty
|
164
164
|
end
|
165
165
|
|
166
166
|
it "knows when a middleware has been removed" do
|
@@ -170,7 +170,7 @@ describe Sinatra::Reloader do
|
|
170
170
|
)
|
171
171
|
update_app_file(:routes => ['get("/foo") { "foo" }'])
|
172
172
|
get('/foo') # ...to perform the reload
|
173
|
-
app_const.middleware.
|
173
|
+
expect(app_const.middleware).to be_empty
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
@@ -229,20 +229,20 @@ describe Sinatra::Reloader do
|
|
229
229
|
end
|
230
230
|
|
231
231
|
it "doesn't mess up the application" do
|
232
|
-
get('/secret').
|
233
|
-
get('/secret').body.strip.
|
232
|
+
expect(get('/secret')).to be_client_error
|
233
|
+
expect(get('/secret').body.strip).to eq('Access forbiden')
|
234
234
|
end
|
235
235
|
|
236
236
|
it "knows when a error has been added" do
|
237
237
|
update_app_file(:errors => { 404 => "'Nowhere'" })
|
238
|
-
get('/nowhere').
|
239
|
-
get('/nowhere').body.
|
238
|
+
expect(get('/nowhere')).to be_not_found
|
239
|
+
expect(get('/nowhere').body).to eq('Nowhere')
|
240
240
|
end
|
241
241
|
|
242
242
|
it "knows when a error has been removed" do
|
243
243
|
update_app_file(:routes => ['get("/secret") { 403 }'])
|
244
|
-
get('/secret').
|
245
|
-
get('/secret').body.
|
244
|
+
expect(get('/secret')).to be_client_error
|
245
|
+
expect(get('/secret').body).not_to eq('Access forbiden')
|
246
246
|
end
|
247
247
|
|
248
248
|
it "knows when a error has been modified" do
|
@@ -250,8 +250,8 @@ describe Sinatra::Reloader do
|
|
250
250
|
:routes => ['get("/secret") { 403 }'],
|
251
251
|
:errors => { 403 => "'What are you doing here?'" }
|
252
252
|
)
|
253
|
-
get('/secret').
|
254
|
-
get('/secret').body.
|
253
|
+
expect(get('/secret')).to be_client_error
|
254
|
+
expect(get('/secret').body).to eq('What are you doing here?')
|
255
255
|
end
|
256
256
|
end
|
257
257
|
|
@@ -352,20 +352,20 @@ describe Sinatra::Reloader do
|
|
352
352
|
it "allows to specify a file to stop from being reloaded" do
|
353
353
|
app_const.dont_reload app_file_path
|
354
354
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
355
|
-
get('/foo').body.strip.
|
355
|
+
expect(get('/foo').body.strip).to eq('foo')
|
356
356
|
end
|
357
357
|
|
358
358
|
it "allows to specify a glob to stop matching files from being reloaded" do
|
359
359
|
app_const.dont_reload '**/*.rb'
|
360
360
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
361
|
-
get('/foo').body.strip.
|
361
|
+
expect(get('/foo').body.strip).to eq('foo')
|
362
362
|
end
|
363
363
|
|
364
364
|
it "doesn't interfere with other application's reloading policy" do
|
365
365
|
app_const.dont_reload '**/*.rb'
|
366
366
|
setup_example_app(:routes => ['get("/foo") { "foo" }'])
|
367
367
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
368
|
-
get('/foo').body.strip.
|
368
|
+
expect(get('/foo').body.strip).to eq('bar')
|
369
369
|
end
|
370
370
|
end
|
371
371
|
|
@@ -382,19 +382,19 @@ describe Sinatra::Reloader do
|
|
382
382
|
end
|
383
383
|
|
384
384
|
it "allows to specify a file to be reloaded" do
|
385
|
-
get('/foo').body.strip.
|
385
|
+
expect(get('/foo').body.strip).to eq('foo')
|
386
386
|
update_file(@foo_path) do |f|
|
387
387
|
f.write 'class Foo; def self.foo() "bar" end end'
|
388
388
|
end
|
389
|
-
get('/foo').body.strip.
|
389
|
+
expect(get('/foo').body.strip).to eq('bar')
|
390
390
|
end
|
391
391
|
|
392
392
|
it "allows to specify glob to reaload matching files" do
|
393
|
-
get('/foo').body.strip.
|
393
|
+
expect(get('/foo').body.strip).to eq('foo')
|
394
394
|
update_file(@foo_path) do |f|
|
395
395
|
f.write 'class Foo; def self.foo() "bar" end end'
|
396
396
|
end
|
397
|
-
get('/foo').body.strip.
|
397
|
+
expect(get('/foo').body.strip).to eq('bar')
|
398
398
|
end
|
399
399
|
|
400
400
|
it "doesn't try to reload a removed file" do
|
@@ -402,17 +402,42 @@ describe Sinatra::Reloader do
|
|
402
402
|
f.write 'class Foo; def self.foo() "bar" end end'
|
403
403
|
end
|
404
404
|
FileUtils.rm @foo_path
|
405
|
-
get('/foo').body.strip.
|
405
|
+
expect(get('/foo').body.strip).to eq('foo')
|
406
406
|
end
|
407
407
|
|
408
408
|
it "doesn't interfere with other application's reloading policy" do
|
409
409
|
app_const.also_reload '**/*.rb'
|
410
410
|
setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
|
411
|
-
get('/foo').body.strip.
|
411
|
+
expect(get('/foo').body.strip).to eq('foo')
|
412
412
|
update_file(@foo_path) do |f|
|
413
413
|
f.write 'class Foo; def self.foo() "bar" end end'
|
414
414
|
end
|
415
|
-
get('/foo').body.strip.
|
415
|
+
expect(get('/foo').body.strip).to eq('foo')
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
describe ".after_reload" do
|
420
|
+
before(:each) do
|
421
|
+
setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
|
422
|
+
@foo_path = File.join(tmp_dir, 'foo.rb')
|
423
|
+
update_file(@foo_path) do |f|
|
424
|
+
f.write 'class Foo; def self.foo() "foo" end end'
|
425
|
+
end
|
426
|
+
$LOADED_FEATURES.delete @foo_path
|
427
|
+
require @foo_path
|
428
|
+
app_const.also_reload @foo_path
|
429
|
+
end
|
430
|
+
|
431
|
+
it "allows block execution after reloading files" do
|
432
|
+
app_const.after_reload do
|
433
|
+
$reloaded = true
|
434
|
+
end
|
435
|
+
expect($reloaded).to eq(nil)
|
436
|
+
expect(get('/foo').body.strip).to eq('foo')
|
437
|
+
update_file(@foo_path) do |f|
|
438
|
+
f.write 'class Foo; def self.foo() "bar" end end'
|
439
|
+
end
|
440
|
+
expect($reloaded).to eq(true)
|
416
441
|
end
|
417
442
|
end
|
418
443
|
|
@@ -434,7 +459,7 @@ describe Sinatra::Reloader do
|
|
434
459
|
:parent => 'Parent'
|
435
460
|
)
|
436
461
|
|
437
|
-
get('/foo').body.
|
462
|
+
expect(get('/foo').body).to eq('bar')
|
438
463
|
end
|
439
464
|
|
440
465
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinatra::RequiredParams do
|
4
|
+
context "#required_params" do
|
5
|
+
context "simple keys" do
|
6
|
+
before do
|
7
|
+
mock_app do
|
8
|
+
helpers Sinatra::RequiredParams
|
9
|
+
get('/') { required_params(:p1, :p2) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
it 'return 400 if required params do not exist' do
|
13
|
+
get('/')
|
14
|
+
expect(last_response.status).to eq(400)
|
15
|
+
end
|
16
|
+
it 'return 400 if required params do not exist partially' do
|
17
|
+
get('/', :p1 => 1)
|
18
|
+
expect(last_response.status).to eq(400)
|
19
|
+
end
|
20
|
+
it 'return 200 if required params exist' do
|
21
|
+
get('/', :p1 => 1, :p2 => 2)
|
22
|
+
expect(last_response.status).to eq(200)
|
23
|
+
end
|
24
|
+
it 'return 200 if required params exist with array' do
|
25
|
+
get('/', :p1 => 1, :p2 => [31, 32, 33])
|
26
|
+
expect(last_response.status).to eq(200)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
context "hash keys" do
|
30
|
+
before do
|
31
|
+
mock_app do
|
32
|
+
helpers Sinatra::RequiredParams
|
33
|
+
get('/') { required_params(:p1, :p2 => :p21) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
it 'return 400 if required params do not exist' do
|
37
|
+
get('/')
|
38
|
+
expect(last_response.status).to eq(400)
|
39
|
+
end
|
40
|
+
it 'return 200 if required params exist' do
|
41
|
+
get('/', :p1 => 1, :p2 => {:p21 => 21})
|
42
|
+
expect(last_response.status).to eq(200)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
context "complex keys" do
|
46
|
+
before do
|
47
|
+
mock_app do
|
48
|
+
helpers Sinatra::RequiredParams
|
49
|
+
get('/') { required_params(:p1 => [:p11, {:p12 => :p121, :p122 => [:p123, {:p124 => :p1241}]}]) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
it 'return 400 if required params do not exist' do
|
53
|
+
get('/')
|
54
|
+
expect(last_response.status).to eq(400)
|
55
|
+
end
|
56
|
+
it 'return 200 if required params exist' do
|
57
|
+
get('/', :p1 => {:p11 => 11, :p12 => {:p121 => 121}, :p122 => {:p123 => 123, :p124 => {:p1241 => 1241}}})
|
58
|
+
expect(last_response.status).to eq(200)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "#_required_params" do
|
64
|
+
it "is invisible" do
|
65
|
+
expect { _required_params }.to raise_error(NameError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/respond_with_spec.rb
CHANGED
@@ -40,8 +40,8 @@ describe Sinatra::RespondWith do
|
|
40
40
|
format.json { "json!" }
|
41
41
|
end
|
42
42
|
|
43
|
-
req(:html).body.
|
44
|
-
req(:json).body.
|
43
|
+
expect(req(:html).body).to eq("html!")
|
44
|
+
expect(req(:json).body).to eq("json!")
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'respects quality' do
|
@@ -50,8 +50,8 @@ describe Sinatra::RespondWith do
|
|
50
50
|
format.json { "json!" }
|
51
51
|
end
|
52
52
|
|
53
|
-
req("text/html;q=0.7, application/json;q=0.3").body.
|
54
|
-
req("text/html;q=0.3, application/json;q=0.7").body.
|
53
|
+
expect(req("text/html;q=0.7, application/json;q=0.3").body).to eq("html!")
|
54
|
+
expect(req("text/html;q=0.3, application/json;q=0.7").body).to eq("json!")
|
55
55
|
end
|
56
56
|
|
57
57
|
it 'allows using mime types' do
|
@@ -60,7 +60,7 @@ describe Sinatra::RespondWith do
|
|
60
60
|
format.json { "json!" }
|
61
61
|
end
|
62
62
|
|
63
|
-
req(:html).body.
|
63
|
+
expect(req(:html).body).to eq("html!")
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'allows using wildcards in format matchers' do
|
@@ -69,7 +69,7 @@ describe Sinatra::RespondWith do
|
|
69
69
|
format.json { "json!" }
|
70
70
|
end
|
71
71
|
|
72
|
-
req(:html).body.
|
72
|
+
expect(req(:html).body).to eq("text!")
|
73
73
|
end
|
74
74
|
|
75
75
|
it 'allows using catch all wildcards in format matchers' do
|
@@ -78,7 +78,7 @@ describe Sinatra::RespondWith do
|
|
78
78
|
format.json { "json!" }
|
79
79
|
end
|
80
80
|
|
81
|
-
req(:html).body.
|
81
|
+
expect(req(:html).body).to eq("anything!")
|
82
82
|
end
|
83
83
|
|
84
84
|
it 'prefers concret over generic' do
|
@@ -88,14 +88,15 @@ describe Sinatra::RespondWith do
|
|
88
88
|
format.json { "json!" }
|
89
89
|
end
|
90
90
|
|
91
|
-
req(:json).body.
|
92
|
-
req(:html).body.
|
91
|
+
expect(req(:json).body).to eq("json!")
|
92
|
+
expect(req(:html).body).to eq("text!")
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'does not set up default handlers' do
|
96
96
|
respond_to
|
97
|
-
req.
|
98
|
-
status.
|
97
|
+
expect(req).not_to be_ok
|
98
|
+
expect(status).to eq(500)
|
99
|
+
expect(body).to eq("Unknown template engine")
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
@@ -107,8 +108,8 @@ describe Sinatra::RespondWith do
|
|
107
108
|
format.json { "json!" }
|
108
109
|
end
|
109
110
|
|
110
|
-
req(:html).body.
|
111
|
-
req(:json).body.
|
111
|
+
expect(req(:html).body).to eq("html!")
|
112
|
+
expect(req(:json).body).to eq("json!")
|
112
113
|
end
|
113
114
|
|
114
115
|
it 'respects quality' do
|
@@ -117,8 +118,8 @@ describe Sinatra::RespondWith do
|
|
117
118
|
format.json { "json!" }
|
118
119
|
end
|
119
120
|
|
120
|
-
req("text/html;q=0.7, application/json;q=0.3").body.
|
121
|
-
req("text/html;q=0.3, application/json;q=0.7").body.
|
121
|
+
expect(req("text/html;q=0.7, application/json;q=0.3").body).to eq("html!")
|
122
|
+
expect(req("text/html;q=0.3, application/json;q=0.7").body).to eq("json!")
|
122
123
|
end
|
123
124
|
|
124
125
|
it 'allows using mime types' do
|
@@ -127,7 +128,7 @@ describe Sinatra::RespondWith do
|
|
127
128
|
format.json { "json!" }
|
128
129
|
end
|
129
130
|
|
130
|
-
req(:html).body.
|
131
|
+
expect(req(:html).body).to eq("html!")
|
131
132
|
end
|
132
133
|
|
133
134
|
it 'allows using wildcards in format matchers' do
|
@@ -136,7 +137,7 @@ describe Sinatra::RespondWith do
|
|
136
137
|
format.json { "json!" }
|
137
138
|
end
|
138
139
|
|
139
|
-
req(:html).body.
|
140
|
+
expect(req(:html).body).to eq("text!")
|
140
141
|
end
|
141
142
|
|
142
143
|
it 'allows using catch all wildcards in format matchers' do
|
@@ -145,7 +146,7 @@ describe Sinatra::RespondWith do
|
|
145
146
|
format.json { "json!" }
|
146
147
|
end
|
147
148
|
|
148
|
-
req(:html).body.
|
149
|
+
expect(req(:html).body).to eq("anything!")
|
149
150
|
end
|
150
151
|
|
151
152
|
it 'prefers concret over generic' do
|
@@ -155,15 +156,15 @@ describe Sinatra::RespondWith do
|
|
155
156
|
format.json { "json!" }
|
156
157
|
end
|
157
158
|
|
158
|
-
req(:json).body.
|
159
|
-
req(:html).body.
|
159
|
+
expect(req(:json).body).to eq("json!")
|
160
|
+
expect(req(:html).body).to eq("text!")
|
160
161
|
end
|
161
162
|
end
|
162
163
|
|
163
164
|
describe "default behavior" do
|
164
165
|
it 'converts objects to json out of the box' do
|
165
166
|
respond_with 'a' => 'b'
|
166
|
-
OkJson.decode(req(:json).body).
|
167
|
+
expect(OkJson.decode(req(:json).body)).to eq({'a' => 'b'})
|
167
168
|
end
|
168
169
|
|
169
170
|
it 'handles multiple routes correctly' do
|
@@ -171,68 +172,69 @@ describe Sinatra::RespondWith do
|
|
171
172
|
get('/') { respond_with 'a' => 'b' }
|
172
173
|
get('/:name') { respond_with 'a' => params[:name] }
|
173
174
|
end
|
174
|
-
OkJson.decode(req('/', :json).body).
|
175
|
-
OkJson.decode(req('/b', :json).body).
|
176
|
-
OkJson.decode(req('/c', :json).body).
|
175
|
+
expect(OkJson.decode(req('/', :json).body)).to eq({'a' => 'b'})
|
176
|
+
expect(OkJson.decode(req('/b', :json).body)).to eq({'a' => 'b'})
|
177
|
+
expect(OkJson.decode(req('/c', :json).body)).to eq({'a' => 'c'})
|
177
178
|
end
|
178
179
|
|
179
180
|
it "calls to_EXT if available" do
|
180
181
|
respond_with Struct.new(:to_pdf).new("hello")
|
181
|
-
req(:pdf).body.
|
182
|
+
expect(req(:pdf).body).to eq("hello")
|
182
183
|
end
|
183
184
|
|
184
|
-
it 'results in a
|
185
|
+
it 'results in a 500 if format cannot be produced' do
|
185
186
|
respond_with({})
|
186
|
-
req(:html).
|
187
|
-
status.
|
187
|
+
expect(req(:html)).not_to be_ok
|
188
|
+
expect(status).to eq(500)
|
189
|
+
expect(body).to eq("Unknown template engine")
|
188
190
|
end
|
189
191
|
end
|
190
192
|
|
191
193
|
describe 'templates' do
|
192
194
|
it 'looks for templates with name.target.engine' do
|
193
195
|
respond_with :foo, :name => 'World'
|
194
|
-
req(:html).
|
195
|
-
body.
|
196
|
+
expect(req(:html)).to be_ok
|
197
|
+
expect(body).to eq("Hello World!")
|
196
198
|
end
|
197
199
|
|
198
200
|
it 'looks for templates with name.engine for specific engines' do
|
199
201
|
respond_with :bar
|
200
|
-
req(:html).
|
201
|
-
body.
|
202
|
+
expect(req(:html)).to be_ok
|
203
|
+
expect(body).to eq("guten Tag!")
|
202
204
|
end
|
203
205
|
|
204
206
|
it 'does not use name.engine for engines producing other formats' do
|
205
207
|
respond_with :not_html
|
206
|
-
req(:html).
|
207
|
-
status.
|
208
|
-
body.
|
208
|
+
expect(req(:html)).not_to be_ok
|
209
|
+
expect(status).to eq(500)
|
210
|
+
expect(body).to eq("Unknown template engine")
|
209
211
|
end
|
210
212
|
|
211
213
|
it 'falls back to #json if no template is found' do
|
212
214
|
respond_with :foo, :name => 'World'
|
213
|
-
req(:json).
|
214
|
-
OkJson.decode(body).
|
215
|
+
expect(req(:json)).to be_ok
|
216
|
+
expect(OkJson.decode(body)).to eq({'name' => 'World'})
|
215
217
|
end
|
216
218
|
|
217
219
|
it 'favors templates over #json' do
|
218
220
|
respond_with :bar, :name => 'World'
|
219
|
-
req(:json).
|
220
|
-
body.
|
221
|
+
expect(req(:json)).to be_ok
|
222
|
+
expect(body).to eq('json!')
|
221
223
|
end
|
222
224
|
|
223
225
|
it 'falls back to to_EXT if no template is found' do
|
224
226
|
object = {:name => 'World'}
|
225
227
|
def object.to_pdf; "hi" end
|
226
228
|
respond_with :foo, object
|
227
|
-
req(:pdf).
|
228
|
-
body.
|
229
|
+
expect(req(:pdf)).to be_ok
|
230
|
+
expect(body).to eq("hi")
|
229
231
|
end
|
230
232
|
|
231
233
|
unless defined? JRUBY_VERSION
|
232
234
|
it 'uses yajl for json' do
|
233
235
|
respond_with :baz
|
234
|
-
req(:json).
|
235
|
-
body.
|
236
|
+
expect(req(:json)).to be_ok
|
237
|
+
expect(body).to eq("\"yajl!\"")
|
236
238
|
end
|
237
239
|
end
|
238
240
|
end
|
@@ -240,20 +242,20 @@ describe Sinatra::RespondWith do
|
|
240
242
|
describe 'customizing' do
|
241
243
|
it 'allows customizing' do
|
242
244
|
respond_with(:foo, :name => 'World') { |f| f.html { 'html!' }}
|
243
|
-
req(:html).
|
244
|
-
body.
|
245
|
+
expect(req(:html)).to be_ok
|
246
|
+
expect(body).to eq("html!")
|
245
247
|
end
|
246
248
|
|
247
249
|
it 'falls back to default behavior if none matches' do
|
248
250
|
respond_with(:foo, :name => 'World') { |f| f.json { 'json!' }}
|
249
|
-
req(:html).
|
250
|
-
body.
|
251
|
+
expect(req(:html)).to be_ok
|
252
|
+
expect(body).to eq("Hello World!")
|
251
253
|
end
|
252
254
|
|
253
255
|
it 'favors generic rule over default behavior' do
|
254
256
|
respond_with(:foo, :name => 'World') { |f| f.on('*/*') { 'generic!' }}
|
255
|
-
req(:html).
|
256
|
-
body.
|
257
|
+
expect(req(:html)).to be_ok
|
258
|
+
expect(body).to eq("generic!")
|
257
259
|
end
|
258
260
|
end
|
259
261
|
|
@@ -270,7 +272,7 @@ describe Sinatra::RespondWith do
|
|
270
272
|
end
|
271
273
|
|
272
274
|
self.app = Sinatra.new(app)
|
273
|
-
req('/a', :json).
|
275
|
+
expect(req('/a', :json)).not_to be_ok
|
274
276
|
end
|
275
277
|
end
|
276
278
|
end
|
@@ -283,8 +285,8 @@ describe Sinatra::RespondWith do
|
|
283
285
|
get('/b') { 'ok' }
|
284
286
|
end
|
285
287
|
|
286
|
-
req('/b', :xml).
|
287
|
-
req('/b', :html).
|
288
|
+
expect(req('/b', :xml)).not_to be_ok
|
289
|
+
expect(req('/b', :html)).to be_ok
|
288
290
|
end
|
289
291
|
|
290
292
|
it 'still allows provides' do
|
@@ -294,8 +296,8 @@ describe Sinatra::RespondWith do
|
|
294
296
|
get('/b', :provides => :json) { 'ok' }
|
295
297
|
end
|
296
298
|
|
297
|
-
req('/b', :html).
|
298
|
-
req('/b', :json).
|
299
|
+
expect(req('/b', :html)).not_to be_ok
|
300
|
+
expect(req('/b', :json)).to be_ok
|
299
301
|
end
|
300
302
|
|
301
303
|
it 'plays well with namespaces' do
|
@@ -308,8 +310,8 @@ describe Sinatra::RespondWith do
|
|
308
310
|
get('/b') { 'anything' }
|
309
311
|
end
|
310
312
|
|
311
|
-
req('/a', :html).
|
312
|
-
req('/b', :html).
|
313
|
+
expect(req('/a', :html)).not_to be_ok
|
314
|
+
expect(req('/b', :html)).to be_ok
|
313
315
|
end
|
314
316
|
end
|
315
317
|
end
|