gds-api-adapters 36.4.1 → 37.0.0

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.
@@ -20,48 +20,36 @@ describe GdsApi::ContentStore do
20
20
  assert_equal base_path, response["base_path"]
21
21
  end
22
22
 
23
- it "returns nil if the item doesn't exist" do
23
+ it "raises if the item doesn't exist" do
24
24
  content_store_does_not_have_item("/non-existent")
25
25
 
26
- assert_nil @api.content_item("/non-existent")
27
- end
28
-
29
- it "raises if the item doesn't exist and `always_raise_for_not_found` enabled" do
30
- GdsApi.configure do |config|
31
- config.always_raise_for_not_found = true
26
+ assert_raises(GdsApi::HTTPNotFound) do
27
+ @api.content_item("/non-existent")
32
28
  end
29
+ end
33
30
 
31
+ it "raises if the item doesn't exist" do
34
32
  content_store_does_not_have_item("/non-existent")
35
33
 
36
34
  assert_raises GdsApi::HTTPNotFound do
37
35
  @api.content_item("/non-existent")
38
36
  end
39
-
40
- GdsApi.configure do |config|
41
- config.always_raise_for_not_found = false
42
- end
43
37
  end
44
38
 
45
- it "returns nil if the item is gone" do
39
+ it "raises if the item is gone" do
46
40
  content_store_has_gone_item("/it-is-gone")
47
41
 
48
- assert_nil @api.content_item("/it-is-gone")
49
- end
50
-
51
- it "raises if the item is gone and `always_raise_for_not_found` enabled" do
52
- GdsApi.configure do |config|
53
- config.always_raise_for_not_found = true
42
+ assert_raises(GdsApi::HTTPGone) do
43
+ @api.content_item("/it-is-gone")
54
44
  end
45
+ end
55
46
 
47
+ it "raises if the item is gone" do
56
48
  content_store_has_gone_item("/it-is-gone")
57
49
 
58
50
  assert_raises GdsApi::HTTPGone do
59
51
  @api.content_item("/it-is-gone")
60
52
  end
61
-
62
- GdsApi.configure do |config|
63
- config.always_raise_for_not_found = false
64
- end
65
53
  end
66
54
  end
67
55
 
@@ -36,18 +36,20 @@ describe GdsApi::GovUkDelivery do
36
36
  end
37
37
 
38
38
  it "can get a subscription URL" do
39
- expected_payload = { feed_url: 'http://example.com/feed'}
39
+ expected_payload = { feed_url: 'http://example.com/feed' }
40
40
  stub = stub_gov_uk_delivery_get_request('list-url', expected_payload).to_return(created_response_json_hash({list_url: 'thing'}))
41
41
 
42
42
  assert @api.signup_url('http://example.com/feed')
43
43
  assert_requested stub
44
44
  end
45
45
 
46
- it "returns nil if the API 404s" do
46
+ it "raises if the API 404s" do
47
47
  expected_payload = { feed_url: 'http://example.com/feed'}
48
48
  stub = stub_gov_uk_delivery_get_request('list-url', expected_payload).to_return(not_found_hash)
49
49
 
50
- assert @api.signup_url('http://example.com/feed').nil?
50
+ assert_raises(GdsApi::HTTPNotFound) do
51
+ @api.signup_url('http://example.com/feed')
52
+ end
51
53
  end
52
54
 
53
55
  private
@@ -28,6 +28,21 @@ class JsonClientTest < MiniTest::Spec
28
28
  {}
29
29
  end
30
30
 
31
+ # TODO: When we remove `GdsApi.config.hash_response_for_requests`, this helper
32
+ # method no longer makes sense and it should be deleted.
33
+ def with_hash_response_for_requests_disabled
34
+ @old_hash_response_for_requests = GdsApi.config.hash_response_for_requests
35
+ GdsApi.configure do |config|
36
+ config.hash_response_for_requests = false
37
+ end
38
+
39
+ yield
40
+
41
+ GdsApi.configure do |config|
42
+ config.hash_response_for_requests = @old_hash_response_for_requests
43
+ end
44
+ end
45
+
31
46
  def test_long_get_requests_timeout
32
47
  url = "http://www.example.com/timeout.json"
33
48
  stub_request(:get, url).to_timeout
@@ -79,14 +94,14 @@ class JsonClientTest < MiniTest::Spec
79
94
 
80
95
  def test_should_fetch_and_parse_json_into_response
81
96
  url = "http://some.endpoint/some.json"
82
- stub_request(:get, url).to_return(:body => "{}", :status => 200)
97
+ stub_request(:get, url).to_return(body: "{}", status: 200)
83
98
  assert_equal GdsApi::Response, @client.get_json(url).class
84
99
  end
85
100
 
86
101
  def test_should_cache_multiple_requests_to_same_url_across_instances
87
102
  url = "http://some.endpoint/some.json"
88
103
  result = {"foo" => "bar"}
89
- stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)
104
+ stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
90
105
  response_a = GdsApi::JsonClient.new.get_json(url)
91
106
  response_b = GdsApi::JsonClient.new.get_json(url)
92
107
  assert_equal response_a.to_hash, response_b.to_hash
@@ -101,7 +116,7 @@ class JsonClientTest < MiniTest::Spec
101
116
  url = "http://some.endpoint/"
102
117
  result = {"foo" => "bar"}
103
118
  stub_request(:get, %r{\A#{url}}).to_return do |request|
104
- {:body => {"url" => request.uri}.to_json, :status => 200}
119
+ { body: { "url" => request.uri }.to_json, status: 200 }
105
120
  end
106
121
 
107
122
  response_a = GdsApi::JsonClient.new(:cache_size => 5).get_json("#{url}/first.json")
@@ -120,7 +135,7 @@ class JsonClientTest < MiniTest::Spec
120
135
  def test_should_cache_requests_for_15_mins_by_default
121
136
  url = "http://some.endpoint/some.json"
122
137
  result = {"foo" => "bar"}
123
- stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)#.times(1)
138
+ stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
124
139
  response_a = GdsApi::JsonClient.new.get_json(url)
125
140
  response_b = GdsApi::JsonClient.new.get_json(url)
126
141
 
@@ -149,7 +164,7 @@ class JsonClientTest < MiniTest::Spec
149
164
 
150
165
  url = "http://some.endpoint/some.json"
151
166
  result = {"foo" => "bar"}
152
- stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)#.times(1)
167
+ stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
153
168
  response_a = GdsApi::JsonClient.new(:cache_ttl => 5 * 60).get_json(url)
154
169
  response_b = GdsApi::JsonClient.new.get_json(url)
155
170
 
@@ -174,7 +189,7 @@ class JsonClientTest < MiniTest::Spec
174
189
  def test_should_allow_disabling_caching
175
190
  url = "http://some.endpoint/some.json"
176
191
  result = {"foo" => "bar"}
177
- stub_request(:get, url).to_return(:body => JSON.dump(result), :status => 200)
192
+ stub_request(:get, url).to_return(body: JSON.dump(result), status: 200)
178
193
 
179
194
  client = GdsApi::JsonClient.new(disable_cache: true)
180
195
 
@@ -192,9 +207,9 @@ class JsonClientTest < MiniTest::Spec
192
207
  url = "http://some.endpoint/some.json"
193
208
  result = {"foo" => "bar"}
194
209
  stub_request(:get, url).to_return(
195
- :body => JSON.dump(result),
196
- :status => 200,
197
- :headers => { "Expires" => (Time.now + 7 * 60).utc.httpdate }
210
+ body: JSON.dump(result),
211
+ status: 200,
212
+ headers: { "Expires" => (Time.now + 7 * 60).utc.httpdate }
198
213
  )
199
214
 
200
215
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -218,9 +233,9 @@ class JsonClientTest < MiniTest::Spec
218
233
  url = "http://some.endpoint/max_age.json"
219
234
  result = {"foo" => "bar"}
220
235
  stub_request(:get, url).to_return(
221
- :body => JSON.dump(result),
222
- :status => 200,
223
- :headers => { "Cache-Control" => "max-age=420, public" } # 7 minutes
236
+ body: JSON.dump(result),
237
+ status: 200,
238
+ headers: { "Cache-Control" => "max-age=420, public" } # 7 minutes
224
239
  )
225
240
 
226
241
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -244,9 +259,9 @@ class JsonClientTest < MiniTest::Spec
244
259
  url = "http://some.endpoint/no_cache.json"
245
260
  result = {"foo" => "bar"}
246
261
  stub_request(:get, url).to_return(
247
- :body => JSON.dump(result),
248
- :status => 200,
249
- :headers => { "Cache-Control" => "no-cache, public" }
262
+ body: JSON.dump(result),
263
+ status: 200,
264
+ headers: { "Cache-Control" => "no-cache, public" }
250
265
  )
251
266
 
252
267
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -263,9 +278,9 @@ class JsonClientTest < MiniTest::Spec
263
278
  url = "http://some.endpoint/private.json"
264
279
  result = {"foo" => "bar"}
265
280
  stub_request(:get, url).to_return(
266
- :body => JSON.dump(result),
267
- :status => 200,
268
- :headers => { "Cache-Control" => "max-age=600, private" }
281
+ body: JSON.dump(result),
282
+ status: 200,
283
+ headers: { "Cache-Control" => "max-age=600, private" }
269
284
  )
270
285
 
271
286
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -282,9 +297,9 @@ class JsonClientTest < MiniTest::Spec
282
297
  url = "http://some.endpoint/private.json"
283
298
  result = {"foo" => "bar"}
284
299
  stub_request(:get, url).to_return(
285
- :body => JSON.dump(result),
286
- :status => 200,
287
- :headers => { "Cache-Control" => "max-age=600, no-store" }
300
+ body: JSON.dump(result),
301
+ status: 200,
302
+ headers: { "Cache-Control" => "max-age=600, no-store" }
288
303
  )
289
304
 
290
305
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -301,9 +316,9 @@ class JsonClientTest < MiniTest::Spec
301
316
  url = "http://some.endpoint/no_cache_and_max_age.json"
302
317
  result = {"foo" => "bar"}
303
318
  stub_request(:get, url).to_return(
304
- :body => JSON.dump(result),
305
- :status => 200,
306
- :headers => { "Cache-Control" => "max-age=600, no-cache, public" }
319
+ body: JSON.dump(result),
320
+ status: 200,
321
+ headers: { "Cache-Control" => "max-age=600, no-cache, public" }
307
322
  )
308
323
 
309
324
  response_a = GdsApi::JsonClient.new.get_json(url)
@@ -320,9 +335,9 @@ class JsonClientTest < MiniTest::Spec
320
335
  url = "http://some.endpoint/url.json"
321
336
  result = {"foo" => "bar"}
322
337
  stub_request(:get, url).to_return(
323
- :body => JSON.dump(result),
324
- :status => 200,
325
- :headers => {
338
+ body: JSON.dump(result),
339
+ status: 200,
340
+ headers: {
326
341
  "Cache-Control" => "no-cache",
327
342
  "Expires" => (Time.now + 7 * 60).utc.httpdate
328
343
  }
@@ -342,9 +357,9 @@ class JsonClientTest < MiniTest::Spec
342
357
  url = "http://some.endpoint/url.json"
343
358
  result = {"foo" => "bar"}
344
359
  stub_request(:get, url).to_return(
345
- :body => JSON.dump(result),
346
- :status => 200,
347
- :headers => {
360
+ body: JSON.dump(result),
361
+ status: 200,
362
+ headers: {
348
363
  "Cache-Control" => "foo, bar, baz",
349
364
  "Expires" => (Time.now + 7 * 60).utc.httpdate
350
365
  }
@@ -362,7 +377,7 @@ class JsonClientTest < MiniTest::Spec
362
377
 
363
378
  def test_get_bang_should_raise_http_not_found_if_404_returned_from_endpoint
364
379
  url = "http://some.endpoint/some.json"
365
- stub_request(:get, url).to_return(:body => "{}", :status => 404)
380
+ stub_request(:get, url).to_return(body: "{}", status: 404)
366
381
  assert_raises GdsApi::HTTPNotFound do
367
382
  @client.get_json!(url)
368
383
  end
@@ -370,7 +385,7 @@ class JsonClientTest < MiniTest::Spec
370
385
 
371
386
  def test_get_bang_should_raise_http_gone_if_410_returned_from_endpoint
372
387
  url = "http://some.endpoint/some.json"
373
- stub_request(:get, url).to_return(:body => "{}", :status => 410)
388
+ stub_request(:get, url).to_return(body: "{}", status: 410)
374
389
  assert_raises GdsApi::HTTPGone do
375
390
  @client.get_json!(url)
376
391
  end
@@ -378,19 +393,22 @@ class JsonClientTest < MiniTest::Spec
378
393
 
379
394
  def test_get_bang_should_raise_http_forbidden_if_403_returned_from_endpoint
380
395
  url = "http://some.endpoint/some.json"
381
- stub_request(:get, url).to_return(:body => "{}", :status => 403)
396
+ stub_request(:get, url).to_return(body: "{}", status: 403)
382
397
  assert_raises GdsApi::HTTPForbidden do
383
398
  @client.get_json!(url)
384
399
  end
385
400
  end
386
401
 
402
+ # TODO: always_raise_for_not_found will be gone by December 1st, 2016. We will
403
+ # need to remove it from this test.
387
404
  def test_get_should_be_nil_if_404_returned_from_endpoint_and_always_raise_for_not_found_is_disabled
388
405
  @old_always_raise = GdsApi.config.always_raise_for_not_found
389
406
  GdsApi.configure do |config|
390
407
  config.always_raise_for_not_found = false
391
408
  end
392
409
  url = "http://some.endpoint/some.json"
393
- stub_request(:get, url).to_return(:body => "{}", :status => 404)
410
+ stub_request(:get, url).to_return(body: "{}", status: 404)
411
+
394
412
  assert_nil @client.get_json(url)
395
413
  ensure
396
414
  GdsApi.configure do |config|
@@ -398,13 +416,15 @@ class JsonClientTest < MiniTest::Spec
398
416
  end
399
417
  end
400
418
 
419
+ # TODO: always_raise_for_not_found will be gone by December 1st, 2016. We will
420
+ # need to remove it from this test.
401
421
  def test_get_should_be_nil_if_410_returned_from_endpoint_and_always_raise_for_not_found_is_disabled
402
422
  @old_always_raise = GdsApi.config.always_raise_for_not_found
403
423
  GdsApi.configure do |config|
404
424
  config.always_raise_for_not_found = false
405
425
  end
406
426
  url = "http://some.endpoint/some.json"
407
- stub_request(:get, url).to_return(:body => "{}", :status => 410)
427
+ stub_request(:get, url).to_return(body: "{}", status: 410)
408
428
  assert_nil @client.get_json(url)
409
429
  ensure
410
430
  GdsApi.configure do |config|
@@ -412,45 +432,31 @@ class JsonClientTest < MiniTest::Spec
412
432
  end
413
433
  end
414
434
 
415
- def test_get_should_be_nil_if_404_returned_from_endpoint_and_always_raise_for_not_found_is_enabled
416
- @old_always_raise = GdsApi.config.always_raise_for_not_found
417
- GdsApi.configure do |config|
418
- config.always_raise_for_not_found = true
419
- end
435
+ def test_get_should_raise_if_404_returned_from_endpoint
420
436
  url = "http://some.endpoint/some.json"
421
- stub_request(:get, url).to_return(:body => "{}", :status => 404)
437
+ stub_request(:get, url).to_return(body: "{}", status: 404)
422
438
  assert_raises GdsApi::HTTPNotFound do
423
439
  @client.get_json(url)
424
440
  end
425
- ensure
426
- GdsApi.configure do |config|
427
- config.always_raise_for_not_found = @old_always_raise
428
- end
429
441
  end
430
442
 
431
- def test_get_should_be_nil_if_410_returned_from_endpoint_and_always_raise_for_not_found_is_enabled
432
- @old_always_raise = GdsApi.config.always_raise_for_not_found
433
- GdsApi.configure do |config|
434
- config.always_raise_for_not_found = true
435
- end
443
+ def test_get_should_raise_if_410_returned_from_endpoint
436
444
  url = "http://some.endpoint/some.json"
437
- stub_request(:get, url).to_return(:body => "{}", :status => 410)
445
+ stub_request(:get, url).to_return(body: "{}", status: 410)
438
446
  assert_raises GdsApi::HTTPGone do
439
447
  @client.get_json(url)
440
448
  end
441
- ensure
442
- GdsApi.configure do |config|
443
- config.always_raise_for_not_found = @old_always_raise
444
- end
445
449
  end
446
450
 
451
+ # TODO: always_raise_for_not_found will be gone by December 1st, 2016. We will
452
+ # need to remove it from this test.
447
453
  def test_get_raw_should_be_nil_if_404_returned_from_endpoint_and_always_raise_for_not_found_is_disabled
448
454
  @old_always_raise = GdsApi.config.always_raise_for_not_found
449
455
  GdsApi.configure do |config|
450
456
  config.always_raise_for_not_found = false
451
457
  end
452
458
  url = "http://some.endpoint/some.json"
453
- stub_request(:get, url).to_return(:body => "{}", :status => 404)
459
+ stub_request(:get, url).to_return(body: "{}", status: 404)
454
460
  assert_nil @client.get_raw(url)
455
461
  ensure
456
462
  GdsApi.configure do |config|
@@ -458,13 +464,15 @@ class JsonClientTest < MiniTest::Spec
458
464
  end
459
465
  end
460
466
 
467
+ # TODO: always_raise_for_not_found will be gone by December 1st, 2016. We will
468
+ # need to remove it from this test.
461
469
  def test_get_raw_should_be_nil_if_410_returned_from_endpoint_and_always_raise_for_not_found_is_disabled
462
470
  @old_always_raise = GdsApi.config.always_raise_for_not_found
463
471
  GdsApi.configure do |config|
464
472
  config.always_raise_for_not_found = false
465
473
  end
466
474
  url = "http://some.endpoint/some.json"
467
- stub_request(:get, url).to_return(:body => "{}", :status => 410)
475
+ stub_request(:get, url).to_return(body: "{}", status: 410)
468
476
  assert_nil @client.get_raw(url)
469
477
  ensure
470
478
  GdsApi.configure do |config|
@@ -472,41 +480,25 @@ class JsonClientTest < MiniTest::Spec
472
480
  end
473
481
  end
474
482
 
475
- def test_get_raw_should_be_nil_if_404_returned_from_endpoint_and_always_raise_for_not_found_is_enabled
476
- @old_always_raise = GdsApi.config.always_raise_for_not_found
477
- GdsApi.configure do |config|
478
- config.always_raise_for_not_found = true
479
- end
483
+ def test_get_raw_should_raise_if_404_returned_from_endpoint
480
484
  url = "http://some.endpoint/some.json"
481
- stub_request(:get, url).to_return(:body => "{}", :status => 404)
485
+ stub_request(:get, url).to_return(body: "{}", status: 404)
482
486
  assert_raises GdsApi::HTTPNotFound do
483
487
  @client.get_raw(url)
484
488
  end
485
- ensure
486
- GdsApi.configure do |config|
487
- config.always_raise_for_not_found = @old_always_raise
488
- end
489
489
  end
490
490
 
491
- def test_get_raw_should_be_nil_if_410_returned_from_endpoint_and_always_raise_for_not_found_is_enabled
492
- @old_always_raise = GdsApi.config.always_raise_for_not_found
493
- GdsApi.configure do |config|
494
- config.always_raise_for_not_found = true
495
- end
491
+ def test_get_raw_should_be_nil_if_410_returned_from_endpoint
496
492
  url = "http://some.endpoint/some.json"
497
- stub_request(:get, url).to_return(:body => "{}", :status => 410)
493
+ stub_request(:get, url).to_return(body: "{}", status: 410)
498
494
  assert_raises GdsApi::HTTPGone do
499
495
  @client.get_raw(url)
500
496
  end
501
- ensure
502
- GdsApi.configure do |config|
503
- config.always_raise_for_not_found = @old_always_raise
504
- end
505
497
  end
506
498
 
507
499
  def test_get_should_raise_error_if_non_404_non_410_error_code_returned_from_endpoint
508
500
  url = "http://some.endpoint/some.json"
509
- stub_request(:get, url).to_return(:body => "{}", :status => 500)
501
+ stub_request(:get, url).to_return(body: "{}", status: 500)
510
502
  assert_raises GdsApi::HTTPServerError do
511
503
  @client.get_json(url)
512
504
  end
@@ -514,7 +506,7 @@ class JsonClientTest < MiniTest::Spec
514
506
 
515
507
  def test_get_should_raise_conflict_for_409
516
508
  url = "http://some.endpoint/some.json"
517
- stub_request(:delete, url).to_return(:body => "{}", :status => 409)
509
+ stub_request(:delete, url).to_return(body: "{}", status: 409)
518
510
  assert_raises GdsApi::HTTPConflict do
519
511
  @client.delete_json!(url)
520
512
  end
@@ -524,60 +516,60 @@ class JsonClientTest < MiniTest::Spec
524
516
  url = "http://some.endpoint/some.json"
525
517
  new_url = "http://some.endpoint/other.json"
526
518
  stub_request(:get, url).to_return(
527
- :body => "",
528
- :status => 301,
529
- :headers => {"Location" => new_url}
519
+ body: "",
520
+ status: 301,
521
+ headers: { "Location" => new_url }
530
522
  )
531
- stub_request(:get, new_url).to_return(:body => '{"a": 1}', :status => 200)
523
+ stub_request(:get, new_url).to_return(body: '{"a": 1}', status: 200)
532
524
  result = @client.get_json(url)
533
- assert_equal 1, result.a
525
+ assert_equal 1, result['a']
534
526
  end
535
527
 
536
528
  def test_get_should_follow_found_redirect
537
529
  url = "http://some.endpoint/some.json"
538
530
  new_url = "http://some.endpoint/other.json"
539
531
  stub_request(:get, url).to_return(
540
- :body => "",
541
- :status => 302,
542
- :headers => {"Location" => new_url}
532
+ body: "",
533
+ status: 302,
534
+ headers: { "Location" => new_url }
543
535
  )
544
- stub_request(:get, new_url).to_return(:body => '{"a": 1}', :status => 200)
536
+ stub_request(:get, new_url).to_return(body: '{"a": 1}', status: 200)
545
537
  result = @client.get_json(url)
546
- assert_equal 1, result.a
538
+ assert_equal 1, result['a']
547
539
  end
548
540
 
549
541
  def test_get_should_follow_see_other
550
542
  url = "http://some.endpoint/some.json"
551
543
  new_url = "http://some.endpoint/other.json"
552
544
  stub_request(:get, url).to_return(
553
- :body => "",
554
- :status => 303,
555
- :headers => {"Location" => new_url}
545
+ body: "",
546
+ status: 303,
547
+ headers: { "Location" => new_url }
556
548
  )
557
- stub_request(:get, new_url).to_return(:body => '{"a": 1}', :status => 200)
549
+ stub_request(:get, new_url).to_return(body: '{"a": 1}', status: 200)
558
550
  result = @client.get_json(url)
559
- assert_equal 1, result.a
551
+ assert_equal 1, result['a']
560
552
  end
561
553
 
562
554
  def test_get_should_follow_temporary_redirect
563
555
  url = "http://some.endpoint/some.json"
564
556
  new_url = "http://some.endpoint/other.json"
565
557
  stub_request(:get, url).to_return(
566
- :body => "",
567
- :status => 307,
568
- :headers => {"Location" => new_url}
558
+ body: "",
559
+ status: 307,
560
+ headers: { "Location" => new_url }
569
561
  )
570
- stub_request(:get, new_url).to_return(:body => '{"a": 1}', :status => 200)
562
+ stub_request(:get, new_url).to_return(body: '{"a": 1}', status: 200)
571
563
  result = @client.get_json(url)
572
- assert_equal 1, result.a
564
+ assert_equal 1, result['a']
573
565
  end
574
566
 
575
567
  def test_should_handle_infinite_redirects
576
568
  url = "http://some.endpoint/some.json"
577
569
  redirect = {
578
- :body => "",
579
- :status => 302,
580
- :headers => {"Location" => url}
570
+ body: "",
571
+ status: 302,
572
+ headers: { "Location" => url }
581
573
  }
582
574
 
583
575
  # Theoretically, we could set this up to mock out any number of requests
@@ -597,14 +589,14 @@ class JsonClientTest < MiniTest::Spec
597
589
  second_url = "http://some.endpoint/some-other.json"
598
590
 
599
591
  first_redirect = {
600
- :body => "",
601
- :status => 302,
602
- :headers => {"Location" => second_url}
592
+ body: "",
593
+ status: 302,
594
+ headers: { "Location" => second_url }
603
595
  }
604
596
  second_redirect = {
605
- :body => "",
606
- :status => 302,
607
- :headers => {"Location" => first_url}
597
+ body: "",
598
+ status: 302,
599
+ headers: { "Location" => first_url }
608
600
  }
609
601
 
610
602
  # See the comment in the above test for an explanation of this
@@ -617,15 +609,17 @@ class JsonClientTest < MiniTest::Spec
617
609
  end
618
610
  end
619
611
 
620
- def test_post_should_be_nil_if_404_returned_from_endpoint
612
+ def test_post_should_be_raise_if_404_returned_from_endpoint
621
613
  url = "http://some.endpoint/some.json"
622
- stub_request(:post, url).to_return(:body => "{}", :status => 404)
623
- assert_nil @client.post_json(url, {})
614
+ stub_request(:post, url).to_return(body: "{}", status: 404)
615
+ assert_raises(GdsApi::HTTPNotFound) do
616
+ @client.post_json(url, {})
617
+ end
624
618
  end
625
619
 
626
620
  def test_post_should_raise_error_if_non_404_error_code_returned_from_endpoint
627
621
  url = "http://some.endpoint/some.json"
628
- stub_request(:post, url).to_return(:body => "{}", :status => 500)
622
+ stub_request(:post, url).to_return(body: "{}", status: 500)
629
623
  assert_raises GdsApi::HTTPServerError do
630
624
  @client.post_json(url, {})
631
625
  end
@@ -635,95 +629,104 @@ class JsonClientTest < MiniTest::Spec
635
629
  url = "http://some.endpoint/some.json"
636
630
  new_url = "http://some.endpoint/other.json"
637
631
  stub_request(:post, url).to_return(
638
- :body => "",
639
- :status => 302,
640
- :headers => {"Location" => new_url}
632
+ body: "",
633
+ status: 302,
634
+ headers: { "Location" => new_url }
641
635
  )
642
636
  assert_raises GdsApi::HTTPErrorResponse do
643
637
  @client.post_json(url, {})
644
638
  end
645
639
  end
646
640
 
647
- def test_put_should_be_nil_if_404_returned_from_endpoint
641
+ def test_put_should_be_raise_if_404_returned_from_endpoint
648
642
  url = "http://some.endpoint/some.json"
649
- stub_request(:put, url).to_return(:body => "{}", :status => 404)
650
- assert_nil @client.put_json(url, {})
643
+ stub_request(:put, url).to_return(body: "{}", status: 404)
644
+
645
+ assert_raises(GdsApi::HTTPNotFound) do
646
+ @client.put_json(url, {})
647
+ end
651
648
  end
652
649
 
653
650
  def test_put_should_raise_error_if_non_404_error_code_returned_from_endpoint
654
651
  url = "http://some.endpoint/some.json"
655
- stub_request(:put, url).to_return(:body => "{}", :status => 500)
652
+ stub_request(:put, url).to_return(body: "{}", status: 500)
656
653
  assert_raises GdsApi::HTTPServerError do
657
654
  @client.put_json(url, {})
658
655
  end
659
656
  end
660
657
 
661
658
  def empty_response
662
- net_http_response = stub(:body => '{}')
659
+ net_http_response = stub(body: '{}')
663
660
  GdsApi::Response.new(net_http_response)
664
661
  end
665
662
 
666
663
  def test_put_json_does_put_with_json_encoded_packet
667
664
  url = "http://some.endpoint/some.json"
668
665
  payload = {a: 1}
669
- stub_request(:put, url).with(body: payload.to_json).to_return(:body => "{}", :status => 200)
666
+ stub_request(:put, url).with(body: payload.to_json).to_return(body: "{}", status: 200)
670
667
  assert_equal({}, @client.put_json(url, payload).to_hash)
671
668
  end
672
669
 
673
670
  def test_does_not_encode_json_if_payload_is_nil
674
671
  url = "http://some.endpoint/some.json"
675
- stub_request(:put, url).with(body: nil).to_return(:body => "{}", :status => 200)
672
+ stub_request(:put, url).with(body: nil).to_return(body: "{}", status: 200)
676
673
  assert_equal({}, @client.put_json(url, nil).to_hash)
677
674
  end
678
675
 
679
676
  def test_can_build_custom_response_object
680
677
  url = "http://some.endpoint/some.json"
681
- stub_request(:get, url).to_return(:body => "Hello there!")
678
+ stub_request(:get, url).to_return(body: "Hello there!")
682
679
 
683
680
  response = @client.get_json(url) { |http_response| http_response.body }
684
681
  assert response.is_a? String
685
682
  assert_equal "Hello there!", response
686
683
  end
687
684
 
688
- def test_responds_with_nil_on_custom_response_404
685
+ def test_raises_on_custom_response_404
689
686
  url = "http://some.endpoint/some.json"
690
- stub_request(:get, url).to_return(:body => "", :status => 404)
687
+ stub_request(:get, url).to_return(body: "", status: 404)
691
688
 
692
- response = @client.get_json(url) { |http_response| http_response.body }
693
- assert_nil response
689
+ assert_raises(GdsApi::HTTPNotFound) do
690
+ @client.get_json(url, &:body)
691
+ end
694
692
  end
695
693
 
696
694
  def test_can_build_custom_response_object_in_bang_method
697
695
  url = "http://some.endpoint/some.json"
698
- stub_request(:get, url).to_return(:body => "Hello there!")
696
+ stub_request(:get, url).to_return(body: "Hello there!")
699
697
 
700
698
  response = @client.get_json!(url) { |http_response| http_response.body }
701
699
  assert response.is_a? String
702
700
  assert_equal "Hello there!", response
703
701
  end
704
702
 
703
+ # TODO: When we remove `GdsApi.config.hash_response_for_requests`, this test
704
+ # no longer makes sense and it should be deleted.
705
705
  def test_can_convert_response_to_ostruct
706
- url = "http://some.endpoint/some.json"
707
- payload = {a: 1}
708
- stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":1}', :status => 200)
709
- response = @client.put_json(url, payload)
710
- assert_equal(OpenStruct.new(a: 1), response.to_ostruct)
706
+ with_hash_response_for_requests_disabled do
707
+ url = "http://some.endpoint/some.json"
708
+ payload = { a: 1 }
709
+ stub_request(:put, url).with(body: payload.to_json).to_return(body: '{"a":1}', status: 200)
710
+ response = @client.put_json(url, payload)
711
+ assert_equal(OpenStruct.new(a: 1), response.to_ostruct)
712
+ end
711
713
  end
712
714
 
713
715
  def test_can_access_attributes_of_response_directly
714
716
  url = "http://some.endpoint/some.json"
715
- payload = {a: 1}
716
- stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":{"b":2}}', :status => 200)
717
+ payload = { a: 1 }
718
+ stub_request(:put, url).with(body: payload.to_json).to_return(body: '{"a":{"b":2}}', status: 200)
717
719
  response = @client.put_json(url, payload)
718
- assert_equal 2, response.a.b
720
+ assert_equal 2, response['a']['b']
719
721
  end
720
722
 
721
723
  def test_cant_access_attributes_of_response_directly_if_hash_only
722
724
  url = "http://some.endpoint/some.json"
723
- payload = {a: 1}
724
- stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":{"b":2}}', :status => 200)
725
+ payload = { a: 1 }
726
+ stub_request(:put, url).with(body: payload.to_json).to_return(body: '{"a":{"b":2}}', status: 200)
725
727
  response = @client.put_json(url, payload)
726
728
 
729
+ @old_hash_response_for_requests = GdsApi.config.hash_response_for_requests
727
730
  GdsApi.configure do |config|
728
731
  config.hash_response_for_requests = true
729
732
  end
@@ -733,28 +736,36 @@ class JsonClientTest < MiniTest::Spec
733
736
  end
734
737
 
735
738
  GdsApi.configure do |config|
736
- config.hash_response_for_requests = false
739
+ config.hash_response_for_requests = @old_hash_response_for_requests
737
740
  end
738
741
  end
739
742
 
743
+ # TODO: When we remove `GdsApi.config.hash_response_for_requests`, this test
744
+ # no longer makes sense and it should be deleted.
740
745
  def test_accessing_non_existent_attribute_of_response_returns_nil
741
- url = "http://some.endpoint/some.json"
742
- stub_request(:put, url).to_return(:body => '{"a":1}', :status => 200)
743
- response = @client.put_json(url, {})
744
- assert_equal nil, response.does_not_exist
746
+ with_hash_response_for_requests_disabled do
747
+ url = "http://some.endpoint/some.json"
748
+ stub_request(:put, url).to_return(body: '{"a":1}', status: 200)
749
+ response = @client.put_json(url, {})
750
+ assert_equal nil, response.does_not_exist
751
+ end
745
752
  end
746
753
 
754
+ # TODO: When we remove `GdsApi.config.hash_response_for_requests`, this test
755
+ # no longer makes sense and it should be deleted.
747
756
  def test_response_does_not_claim_to_respond_to_methods_corresponding_to_non_existent_attributes
748
- # This mimics the behaviour of OpenStruct
749
- url = "http://some.endpoint/some.json"
750
- stub_request(:put, url).to_return(:body => '{"a":1}', :status => 200)
751
- response = @client.put_json(url, {})
752
- assert ! response.respond_to?(:does_not_exist)
757
+ with_hash_response_for_requests_disabled do
758
+ # This mimics the behaviour of OpenStruct
759
+ url = "http://some.endpoint/some.json"
760
+ stub_request(:put, url).to_return(body: '{"a":1}', status: 200)
761
+ response = @client.put_json(url, {})
762
+ assert ! response.respond_to?(:does_not_exist)
763
+ end
753
764
  end
754
765
 
755
766
  def test_a_response_is_always_considered_present_and_not_blank
756
767
  url = "http://some.endpoint/some.json"
757
- stub_request(:put, url).to_return(:body => '{"a":1}', :status => 200)
768
+ stub_request(:put, url).to_return(body: '{"a":1}', status: 200)
758
769
  response = @client.put_json(url, {})
759
770
  assert ! response.blank?
760
771
  assert response.present?
@@ -764,10 +775,10 @@ class JsonClientTest < MiniTest::Spec
764
775
  client = GdsApi::JsonClient.new(basic_auth: {user: 'user', password: 'password'})
765
776
 
766
777
  stub_request(:put, "http://user:password@some.endpoint/some.json").
767
- to_return(:body => '{"a":1}', :status => 200)
778
+ to_return(body: '{"a":1}', status: 200)
768
779
 
769
780
  response = client.put_json("http://some.endpoint/some.json", {})
770
- assert_equal 1, response.a
781
+ assert_equal 1, response['a']
771
782
  end
772
783
 
773
784
  def test_client_can_use_bearer_token
@@ -776,15 +787,15 @@ class JsonClientTest < MiniTest::Spec
776
787
  merge('Authorization' => 'Bearer SOME_BEARER_TOKEN')
777
788
 
778
789
  stub_request(:put, "http://some.other.endpoint/some.json").
779
- with(:headers => expected_headers).
780
- to_return(:body => '{"a":2}', :status => 200)
790
+ with(headers: expected_headers).
791
+ to_return(body: '{"a":2}', status: 200)
781
792
 
782
793
  response = client.put_json("http://some.other.endpoint/some.json", {})
783
- assert_equal 2, response.a
794
+ assert_equal 2, response['a']
784
795
  end
785
796
 
786
797
  def test_client_can_set_custom_headers_on_gets
787
- stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
798
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(status: 200)
788
799
 
789
800
  response = GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json",
790
801
  { "HEADER-A" => "B", "HEADER-C" => "D" })
@@ -796,7 +807,7 @@ class JsonClientTest < MiniTest::Spec
796
807
  end
797
808
 
798
809
  def test_client_can_set_custom_headers_on_posts
799
- stub_request(:post, "http://some.other.endpoint/some.json").to_return(:status => 200)
810
+ stub_request(:post, "http://some.other.endpoint/some.json").to_return(status: 200)
800
811
 
801
812
  response = GdsApi::JsonClient.new.post_json("http://some.other.endpoint/some.json", {},
802
813
  { "HEADER-A" => "B", "HEADER-C" => "D" })
@@ -808,7 +819,7 @@ class JsonClientTest < MiniTest::Spec
808
819
  end
809
820
 
810
821
  def test_client_can_set_custom_headers_on_puts
811
- stub_request(:put, "http://some.other.endpoint/some.json").to_return(:status => 200)
822
+ stub_request(:put, "http://some.other.endpoint/some.json").to_return(status: 200)
812
823
 
813
824
  response = GdsApi::JsonClient.new.put_json("http://some.other.endpoint/some.json", {},
814
825
  { "HEADER-A" => "B", "HEADER-C" => "D" })
@@ -820,7 +831,7 @@ class JsonClientTest < MiniTest::Spec
820
831
  end
821
832
 
822
833
  def test_client_can_set_custom_headers_on_deletes
823
- stub_request(:delete, "http://some.other.endpoint/some.json").to_return(:status => 200)
834
+ stub_request(:delete, "http://some.other.endpoint/some.json").to_return(status: 200)
824
835
 
825
836
  response = GdsApi::JsonClient.new.delete_json("http://some.other.endpoint/some.json",
826
837
  { "HEADER-A" => "B", "HEADER-C" => "D" })
@@ -836,7 +847,7 @@ class JsonClientTest < MiniTest::Spec
836
847
  GdsApi::GovukHeaders.set_header(:govuk_request_id, "12345")
837
848
  GdsApi::GovukHeaders.set_header(:govuk_original_url, "http://example.com")
838
849
 
839
- stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
850
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(status: 200)
840
851
 
841
852
  GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json")
842
853
 
@@ -848,7 +859,7 @@ class JsonClientTest < MiniTest::Spec
848
859
 
849
860
  def test_govuk_headers_ignored_in_requests_if_not_present
850
861
  GdsApi::GovukHeaders.set_header(:x_govuk_authenticated_user, "")
851
- stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
862
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(status: 200)
852
863
 
853
864
  GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json")
854
865
 
@@ -858,7 +869,7 @@ class JsonClientTest < MiniTest::Spec
858
869
  end
859
870
 
860
871
  def test_additional_headers_passed_in_do_not_get_modified
861
- stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
872
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(status: 200)
862
873
 
863
874
  headers = { 'HEADER-A' => 'A' }
864
875
  GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json", headers)
@@ -869,7 +880,11 @@ class JsonClientTest < MiniTest::Spec
869
880
  def test_client_can_decompress_gzip_responses
870
881
  url = "http://some.endpoint/some.json"
871
882
  # {"test": "hello"}
872
- stub_request(:get, url).to_return(:body => "\u001F\x8B\b\u0000Q\u000F\u0019Q\u0000\u0003\xABVP*I-.Q\xB2RP\xCAH\xCD\xC9\xC9WR\xA8\u0005\u0000\xD1C\u0018\xFE\u0013\u0000\u0000\u0000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' })
883
+ stub_request(:get, url).to_return(
884
+ body: "\u001F\x8B\b\u0000Q\u000F\u0019Q\u0000\u0003\xABVP*I-.Q\xB2RP\xCAH\xCD\xC9\xC9WR\xA8\u0005\u0000\xD1C\u0018\xFE\u0013\u0000\u0000\u0000",
885
+ status: 200,
886
+ headers: { 'Content-Encoding' => 'gzip' }
887
+ )
873
888
  response = @client.get_json(url)
874
889
 
875
890
  assert_equal "hello", response["test"]
@@ -878,11 +893,11 @@ class JsonClientTest < MiniTest::Spec
878
893
  def test_client_can_post_multipart_responses
879
894
  url = "http://some.endpoint/some.json"
880
895
  stub_request(:post, url).
881
- with(:body => %r{------RubyFormBoundary\w+\r\nContent-Disposition: form-data; name="a"\r\n\r\n123\r\n------RubyFormBoundary\w+--\r\n},
882
- :headers => {
896
+ with(body: %r{------RubyFormBoundary\w+\r\nContent-Disposition: form-data; name="a"\r\n\r\n123\r\n------RubyFormBoundary\w+--\r\n},
897
+ headers: {
883
898
  'Content-Type' => %r{multipart/form-data; boundary=----RubyFormBoundary\w+}
884
899
  }).
885
- to_return(:body => '{"b": "1"}', :status => 200)
900
+ to_return(body: '{"b": "1"}', status: 200)
886
901
 
887
902
  response = @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
888
903
  assert_equal "1", response["b"]
@@ -890,7 +905,7 @@ class JsonClientTest < MiniTest::Spec
890
905
 
891
906
  def test_post_multipart_should_raise_exception_if_not_found
892
907
  url = "http://some.endpoint/some.json"
893
- stub_request(:post, url).to_return(:body => '', :status => 404)
908
+ stub_request(:post, url).to_return(body: '', status: 404)
894
909
 
895
910
  assert_raises GdsApi::HTTPNotFound do
896
911
  @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
@@ -899,7 +914,7 @@ class JsonClientTest < MiniTest::Spec
899
914
 
900
915
  def test_post_multipart_should_raise_error_responses
901
916
  url = "http://some.endpoint/some.json"
902
- stub_request(:post, url).to_return(:body => '', :status => 500)
917
+ stub_request(:post, url).to_return(body: '', status: 500)
903
918
 
904
919
  assert_raises GdsApi::HTTPServerError do
905
920
  @client.post_multipart("http://some.endpoint/some.json", {"a" => "123"})
@@ -910,11 +925,11 @@ class JsonClientTest < MiniTest::Spec
910
925
  def test_client_can_put_multipart_responses
911
926
  url = "http://some.endpoint/some.json"
912
927
  stub_request(:put, url).
913
- with(:body => %r{------RubyFormBoundary\w+\r\nContent-Disposition: form-data; name="a"\r\n\r\n123\r\n------RubyFormBoundary\w+--\r\n},
914
- :headers => {
928
+ with(body: %r{------RubyFormBoundary\w+\r\nContent-Disposition: form-data; name="a"\r\n\r\n123\r\n------RubyFormBoundary\w+--\r\n},
929
+ headers: {
915
930
  'Content-Type' => %r{multipart/form-data; boundary=----RubyFormBoundary\w+}
916
931
  }).
917
- to_return(:body => '{"b": "1"}', :status => 200)
932
+ to_return(body: '{"b": "1"}', status: 200)
918
933
 
919
934
  response = @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
920
935
  assert_equal "1", response["b"]
@@ -922,7 +937,7 @@ class JsonClientTest < MiniTest::Spec
922
937
 
923
938
  def test_put_multipart_should_raise_exception_if_not_found
924
939
  url = "http://some.endpoint/some.json"
925
- stub_request(:put, url).to_return(:body => '', :status => 404)
940
+ stub_request(:put, url).to_return(body: '', status: 404)
926
941
 
927
942
  assert_raises GdsApi::HTTPNotFound do
928
943
  @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
@@ -931,7 +946,7 @@ class JsonClientTest < MiniTest::Spec
931
946
 
932
947
  def test_put_multipart_should_raise_error_responses
933
948
  url = "http://some.endpoint/some.json"
934
- stub_request(:put, url).to_return(:body => '', :status => 500)
949
+ stub_request(:put, url).to_return(body: '', status: 500)
935
950
 
936
951
  assert_raises GdsApi::HTTPServerError do
937
952
  @client.put_multipart("http://some.endpoint/some.json", {"a" => "123"})
@@ -952,7 +967,7 @@ class JsonClientTest < MiniTest::Spec
952
967
  ENV['GOVUK_APP_NAME'] = "api-tests"
953
968
 
954
969
  url = "http://some.other.endpoint/some.json"
955
- stub_request(:get, url).to_return(:status => 200)
970
+ stub_request(:get, url).to_return(status: 200)
956
971
 
957
972
  GdsApi::JsonClient.new.get_json(url)
958
973