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.
@@ -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.should == 'foo'
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.should == 'bar'
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.should == 'foo'
104
- get('/bar').body.should == 'bar'
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.should == 404
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.should == 'foo'
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.should == 'foo'
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.should == 'bar'
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.should == 'foo'
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.should == 'bar'
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.should_not be_empty
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.should be_empty
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').should be_client_error
233
- get('/secret').body.strip.should == 'Access forbiden'
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').should be_not_found
239
- get('/nowhere').body.should == 'Nowhere'
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').should be_client_error
245
- get('/secret').body.should_not == 'Access forbiden'
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').should be_client_error
254
- get('/secret').body.should == 'What are you doing here?'
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.should == 'foo'
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.should == 'foo'
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.should == 'bar'
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.should == 'foo'
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.should == 'bar'
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.should == 'foo'
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.should == 'bar'
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.should == 'foo'
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.should == 'foo'
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.should == 'foo'
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.should == 'bar'
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
@@ -40,8 +40,8 @@ describe Sinatra::RespondWith do
40
40
  format.json { "json!" }
41
41
  end
42
42
 
43
- req(:html).body.should == "html!"
44
- req(:json).body.should == "json!"
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.should == "html!"
54
- req("text/html;q=0.3, application/json;q=0.7").body.should == "json!"
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.should == "html!"
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.should == "text!"
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.should == "anything!"
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.should == "json!"
92
- req(:html).body.should == "text!"
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.should_not be_ok
98
- status.should == 406
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.should == "html!"
111
- req(:json).body.should == "json!"
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.should == "html!"
121
- req("text/html;q=0.3, application/json;q=0.7").body.should == "json!"
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.should == "html!"
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.should == "text!"
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.should == "anything!"
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.should == "json!"
159
- req(:html).body.should == "text!"
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).should == {'a' => 'b'}
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).should == {'a' => 'b'}
175
- OkJson.decode(req('/b', :json).body).should == {'a' => 'b'}
176
- OkJson.decode(req('/c', :json).body).should == {'a' => 'c'}
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.should == "hello"
182
+ expect(req(:pdf).body).to eq("hello")
182
183
  end
183
184
 
184
- it 'results in a 406 if format cannot be produced' do
185
+ it 'results in a 500 if format cannot be produced' do
185
186
  respond_with({})
186
- req(:html).should_not be_ok
187
- status.should == 406
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).should be_ok
195
- body.should == "Hello World!"
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).should be_ok
201
- body.should == "guten Tag!"
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).should_not be_ok
207
- status.should == 406
208
- body.should be_empty
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).should be_ok
214
- OkJson.decode(body).should == {'name' => 'World'}
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).should be_ok
220
- body.should == 'json!'
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).should be_ok
228
- body.should == "hi"
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).should be_ok
235
- body.should == "\"yajl!\""
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).should be_ok
244
- body.should == "html!"
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).should be_ok
250
- body.should == "Hello World!"
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).should be_ok
256
- body.should == "generic!"
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).should_not be_ok
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).should_not be_ok
287
- req('/b', :html).should be_ok
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).should_not be_ok
298
- req('/b', :json).should be_ok
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).should_not be_ok
312
- req('/b', :html).should be_ok
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