@@ -177,6 +177,51 @@ def create_response(opts = {})
177
177
end
178
178
end
179
179
180
+ shared_examples_for 'status code matcher' do
181
+ # within the calling block, define:
182
+ # let(:expected_code) { <value> } # such as 555, 444, 333, 222
183
+ # let(:other_code) { <value> } # such as 555, 444, 333, 222
184
+ # let(:failure_message) {
185
+ # /an error status code \(5xx\) but it was 400/
186
+ # } # for example
187
+ # let(:negated_message) {
188
+ # /not to have an error status code \(5xx\) but it was 555/
189
+ # } # for example
190
+ # let(:description) {
191
+ # 'respond with an error status code (5xx)'
192
+ # } # for example
193
+ # subject(:matcher) { <matcher> } # e.g. { have_http_status(:error) }
194
+ describe "matching a response" do
195
+ it "returns true for a response with code" do
196
+ response = create_response ( :status => expected_code )
197
+ expect ( matcher . matches? ( response ) ) . to be ( true )
198
+ end
199
+
200
+ it "returns false for a response with a different code" do
201
+ response = create_response ( :status => other_code )
202
+ expect ( matcher . matches? ( response ) ) . to be ( false )
203
+ end
204
+ end
205
+
206
+ it "describes #{ description } " do
207
+ expect ( matcher . description ) . to eq ( description )
208
+ end
209
+
210
+ it "has a failure message reporting the expected and actual status codes" do
211
+ response = create_response ( :status => other_code )
212
+ expect { matcher . matches? response } .
213
+ to change ( matcher , :failure_message ) .
214
+ to ( failure_message )
215
+ end
216
+
217
+ it "has a negated failure message reporting the expected and actual status codes" do
218
+ response = create_response ( :status => expected_code )
219
+ expect { matcher . matches? response } .
220
+ to change ( matcher , :failure_message_when_negated ) .
221
+ to ( negated_message )
222
+ end
223
+ end
224
+
180
225
context "with general status code group" , ":error" do
181
226
# The error query is an alias for `server_error?`:
182
227
#
@@ -192,51 +237,42 @@ def create_response(opts = {})
192
237
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L122
193
238
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
194
239
195
- subject ( :have_error_status ) { have_http_status ( :error ) }
196
-
197
- it_behaves_like "supports different response instances" do
198
- subject ( :matcher ) { have_error_status }
199
-
200
- let ( :code ) { 555 }
201
- end
202
-
203
- describe "matching a response" do
204
- it "returns true for a response with a 5xx status code" do
205
- any_5xx_code = 555
206
- response = create_response ( :status => any_5xx_code )
207
-
208
- expect ( have_error_status . matches? ( response ) ) . to be ( true )
209
- end
210
-
211
- it "returns false for a response with a different code" do
212
- client_error_code = 400
213
- response = create_response ( :status => client_error_code )
240
+ subject ( :matcher ) { have_http_status ( :error ) }
214
241
215
- expect ( have_error_status . matches? ( response ) ) . to be ( false )
216
- end
242
+ it_behaves_like 'status code matcher' do
243
+ let ( :expected_code ) { 555 }
244
+ let ( :other_code ) { 400 }
245
+ let ( :failure_message ) { /an error status code \( 5xx\) but it was 400/ }
246
+ let ( :negated_message ) {
247
+ /not to have an error status code \( 5xx\) but it was 555/
248
+ }
249
+ let ( :description ) {
250
+ 'respond with an error status code (5xx)'
251
+ }
217
252
end
218
253
219
- it "describes responding with an error status code" do
220
- expect ( have_error_status . description ) .
221
- to eq ( "respond with an error status code (5xx)" )
254
+ it_behaves_like "supports different response instances" do
255
+ let ( :code ) { 555 }
222
256
end
257
+ end
223
258
224
- it "has a failure message reporting the expected and actual status codes" do
225
- client_error_code = 400
226
- response = create_response ( :status => client_error_code )
259
+ context "with general status code group" , ":server_error" do
260
+ subject ( :matcher ) { have_http_status ( :server_error ) }
227
261
228
- expect { have_error_status . matches? response } .
229
- to change ( have_error_status , :failure_message ) .
230
- to ( /an error status code \( 5xx\) but it was 400/ )
262
+ it_behaves_like 'status code matcher' do
263
+ let ( :expected_code ) { 555 }
264
+ let ( :other_code ) { 400 }
265
+ let ( :failure_message ) { /a server_error status code \( 5xx\) but it was 400/ }
266
+ let ( :negated_message ) {
267
+ /not to have a server_error status code \( 5xx\) but it was 555/
268
+ }
269
+ let ( :description ) {
270
+ 'respond with a server_error status code (5xx)'
271
+ }
231
272
end
232
273
233
- it "has a negated failure message reporting the expected and actual status codes" do
234
- any_5xx_code = 555
235
- response = create_response ( :status => any_5xx_code )
236
-
237
- expect { have_error_status . matches? response } .
238
- to change ( have_error_status , :failure_message_when_negated ) .
239
- to ( /not to have an error status code \( 5xx\) but it was 555/ )
274
+ it_behaves_like "supports different response instances" do
275
+ let ( :code ) { 555 }
240
276
end
241
277
end
242
278
@@ -255,51 +291,46 @@ def create_response(opts = {})
255
291
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119
256
292
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
257
293
258
- subject ( :have_success_status ) { have_http_status ( :success ) }
294
+ subject ( :matcher ) { have_http_status ( :success ) }
259
295
260
- it_behaves_like "supports different response instances" do
261
- subject ( :matcher ) { have_success_status }
262
-
263
- let ( :code ) { 222 }
264
- end
265
-
266
- describe "matching a response" do
267
- it "returns true for a response with a 2xx status code" do
268
- any_2xx_code = 222
269
- response = create_response ( :status => any_2xx_code )
270
-
271
- expect ( have_success_status . matches? ( response ) ) . to be ( true )
272
- end
273
-
274
- it "returns false for a response with a different code" do
275
- non_success_code = 400
276
- response = create_response ( :status => non_success_code )
277
-
278
- expect ( have_success_status . matches? ( response ) ) . to be ( false )
279
- end
296
+ it_behaves_like 'status code matcher' do
297
+ let ( :expected_code ) { 222 }
298
+ let ( :other_code ) { 400 }
299
+ let ( :failure_message ) {
300
+ /a success status code \( 2xx\) but it was 400/
301
+ }
302
+ let ( :negated_message ) {
303
+ /not to have a success status code \( 2xx\) but it was 222/
304
+ }
305
+ let ( :description ) {
306
+ 'respond with a success status code (2xx)'
307
+ }
280
308
end
281
309
282
- it "describes responding with a success status code" do
283
- expect ( have_success_status . description ) .
284
- to eq ( "respond with a success status code (2xx)" )
310
+ it_behaves_like "supports different response instances" do
311
+ let ( :code ) { 222 }
285
312
end
313
+ end
286
314
287
- it "has a failure message reporting the expected and actual status codes" do
288
- non_success_code = 400
289
- response = create_response ( :status => non_success_code )
315
+ context "with general status code group" , ":successful" do
316
+ subject ( :matcher ) { have_http_status ( :successful ) }
290
317
291
- expect { have_success_status . matches? response } .
292
- to change ( have_success_status , :failure_message ) .
293
- to ( /a success status code \( 2xx\) but it was 400/ )
318
+ it_behaves_like 'status code matcher' do
319
+ let ( :expected_code ) { 222 }
320
+ let ( :other_code ) { 400 }
321
+ let ( :failure_message ) {
322
+ /a successful status code \( 2xx\) but it was 400/
323
+ }
324
+ let ( :negated_message ) {
325
+ /not to have a successful status code \( 2xx\) but it was 222/
326
+ }
327
+ let ( :description ) {
328
+ 'respond with a successful status code (2xx)'
329
+ }
294
330
end
295
331
296
- it "has a negated failure message reporting the expected and actual status codes" do
297
- any_2xx_code = 222
298
- response = create_response ( :status => any_2xx_code )
299
-
300
- expect { have_success_status . matches? response } .
301
- to change ( have_success_status , :failure_message_when_negated ) .
302
- to ( /not to have a success status code \( 2xx\) but it was 222/ )
332
+ it_behaves_like "supports different response instances" do
333
+ let ( :code ) { 222 }
303
334
end
304
335
end
305
336
@@ -318,51 +349,47 @@ def create_response(opts = {})
318
349
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L130
319
350
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
320
351
321
- subject ( :have_missing_status ) { have_http_status ( :missing ) }
322
-
323
- it_behaves_like "supports different response instances" do
324
- subject ( :matcher ) { have_missing_status }
352
+ subject ( :matcher ) { have_http_status ( :missing ) }
325
353
326
- let ( :code ) { 404 }
354
+ it_behaves_like 'status code matcher' do
355
+ let ( :expected_code ) { 404 }
356
+ let ( :other_code ) { 400 }
357
+ let ( :failure_message ) {
358
+ /a missing status code \( 404\) but it was 400/
359
+ }
360
+ let ( :negated_message ) {
361
+ /not to have a missing status code \( 404\) but it was 404/
362
+ }
363
+ let ( :description ) {
364
+ 'respond with a missing status code (404)'
365
+ }
327
366
end
328
367
329
- describe "matching a response" do
330
- it "returns true for a response with a 404 status code" do
331
- not_found_status = 404
332
- response = create_response ( :status => not_found_status )
333
-
334
- expect ( have_missing_status . matches? ( response ) ) . to be ( true )
335
- end
336
-
337
- it "returns false for a response with a different code" do
338
- non_missing_status = 400
339
- response = create_response ( :status => non_missing_status )
340
-
341
- expect ( have_missing_status . matches? ( response ) ) . to be ( false )
342
- end
343
- end
344
-
345
- it "describes responding with a missing status code" do
346
- expect ( have_missing_status . description ) .
347
- to eq ( "respond with a missing status code (404)" )
368
+ it_behaves_like "supports different response instances" do
369
+ let ( :code ) { 404 }
348
370
end
371
+ end
349
372
350
- it "has a failure message reporting the expected and actual status codes" do
351
- non_missing_status = 400
352
- response = create_response ( :status => non_missing_status )
373
+ context "with general status code group" , ":not_found" do
374
+ subject ( :matcher ) { have_http_status ( :not_found ) }
353
375
354
- expect { have_missing_status . matches? response } .
355
- to change ( have_missing_status , :failure_message ) .
356
- to ( /a missing status code \( 404\) but it was 400/ )
376
+ it_behaves_like 'status code matcher' do
377
+ let ( :expected_code ) { 404 }
378
+ let ( :other_code ) { 400 }
379
+ let ( :failure_message ) {
380
+ /a not_found status code \( 404\) but it was 400/
381
+ }
382
+ let ( :negated_message ) {
383
+ /not to have a not_found status code \( 404\) but it was 404/
384
+ }
385
+ let ( :description ) {
386
+ 'respond with a not_found status code (404)'
387
+ }
357
388
end
358
389
359
- it "has a negated failure message reporting the expected status code" do
360
- not_found_status = 404
361
- response = create_response ( :status => not_found_status )
362
-
363
- expect { have_missing_status . matches? response } .
364
- to change ( have_missing_status , :failure_message_when_negated ) .
365
- to ( /not to have a missing status code \( 404\) but it was 404/ )
390
+ it_behaves_like "supports different response instances" do
391
+ subject ( :matcher ) { have_http_status ( :not_found ) }
392
+ let ( :code ) { 404 }
366
393
end
367
394
end
368
395
@@ -381,51 +408,24 @@ def create_response(opts = {})
381
408
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L120
382
409
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
383
410
384
- subject ( :have_redirect_status ) { have_http_status ( :redirect ) }
411
+ subject ( :matcher ) { have_http_status ( :redirect ) }
385
412
386
- it_behaves_like "supports different response instances" do
387
- subject ( :matcher ) { have_redirect_status }
388
-
389
- let ( :code ) { 308 }
413
+ it_behaves_like 'status code matcher' do
414
+ let ( :expected_code ) { 308 }
415
+ let ( :other_code ) { 400 }
416
+ let ( :failure_message ) {
417
+ /a redirect status code \( 3xx\) but it was 400/
418
+ }
419
+ let ( :negated_message ) {
420
+ /not to have a redirect status code \( 3xx\) but it was 308/
421
+ }
422
+ let ( :description ) {
423
+ 'respond with a redirect status code (3xx)'
424
+ }
390
425
end
391
426
392
- describe "matching a response" do
393
- it "returns true for a response with a 3xx status code" do
394
- any_3xx_code = 308
395
- response = create_response ( :status => any_3xx_code )
396
-
397
- expect ( have_redirect_status . matches? ( response ) ) . to be ( true )
398
- end
399
-
400
- it "returns false for a response with a different code" do
401
- non_redirect_code = 400
402
- response = create_response ( :status => non_redirect_code )
403
-
404
- expect ( have_redirect_status . matches? ( response ) ) . to be ( false )
405
- end
406
- end
407
-
408
- it "describes responding with a redirect status code" do
409
- expect ( have_redirect_status . description ) .
410
- to eq ( "respond with a redirect status code (3xx)" )
411
- end
412
-
413
- it "has a failure message reporting the expected and actual status codes" do
414
- non_redirect_code = 400
415
- response = create_response ( :status => non_redirect_code )
416
-
417
- expect { have_redirect_status . matches? response } .
418
- to change ( have_redirect_status , :failure_message ) .
419
- to ( /a redirect status code \( 3xx\) but it was 400/ )
420
- end
421
-
422
- it "has a negated failure message reporting the expected and actual status codes" do
423
- any_3xx_code = 308
424
- response = create_response ( :status => any_3xx_code )
425
-
426
- expect { have_redirect_status . matches? response } .
427
- to change ( have_redirect_status , :failure_message_when_negated ) .
428
- to ( /not to have a redirect status code \( 3xx\) but it was 308/ )
427
+ it_behaves_like "supports different response instances" do
428
+ let ( :code ) { 308 }
429
429
end
430
430
end
431
431
0 commit comments