Skip to content

Commit 2196e44

Browse files
committed
Dry up and add Generic code tests for :not_found, :server_error, :successful
1 parent 6db0717 commit 2196e44

File tree

1 file changed

+153
-153
lines changed

1 file changed

+153
-153
lines changed

spec/rspec/rails/matchers/have_http_status_spec.rb

Lines changed: 153 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ def create_response(opts = {})
177177
end
178178
end
179179

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+
180225
context "with general status code group", ":error" do
181226
# The error query is an alias for `server_error?`:
182227
#
@@ -192,51 +237,42 @@ def create_response(opts = {})
192237
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L122
193238
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
194239

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) }
214241

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+
}
217252
end
218253

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 }
222256
end
257+
end
223258

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) }
227261

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+
}
231272
end
232273

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 }
240276
end
241277
end
242278

@@ -255,51 +291,46 @@ def create_response(opts = {})
255291
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119
256292
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
257293

258-
subject(:have_success_status) { have_http_status(:success) }
294+
subject(:matcher) { have_http_status(:success) }
259295

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+
}
280308
end
281309

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 }
285312
end
313+
end
286314

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) }
290317

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+
}
294330
end
295331

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 }
303334
end
304335
end
305336

@@ -318,51 +349,47 @@ def create_response(opts = {})
318349
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L130
319350
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
320351

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) }
325353

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+
}
327366
end
328367

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 }
348370
end
371+
end
349372

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) }
353375

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+
}
357388
end
358389

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 }
366393
end
367394
end
368395

@@ -381,51 +408,24 @@ def create_response(opts = {})
381408
# - https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L120
382409
# - https://github.com/rack/rack/blob/master/lib/rack/response.rb
383410

384-
subject(:have_redirect_status) { have_http_status(:redirect) }
411+
subject(:matcher) { have_http_status(:redirect) }
385412

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+
}
390425
end
391426

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 }
429429
end
430430
end
431431

0 commit comments

Comments
 (0)