gds-api-adapters 8.0.0 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -43,12 +43,25 @@ the form:
43
43
  PANOPTICON_API_CREDENTIALS = { bearer_token: 'MY_BEARER_TOKEN' }
44
44
 
45
45
 
46
+ ## Middleware for request tracing
47
+
48
+ We set a unique header at the cache level called `GOVUK-Request-Id`. In order
49
+ to serve a user's request, if apps make API requests they should pass on this
50
+ header, so that we can trace a request across the entire GOV.UK stack.
51
+
52
+ `GdsApi::GovukRequestIdSniffer` middleware takes care of this. This gem contains
53
+ a railtie that configures this middleware for Rails apps without extra effort.
54
+ Other Rack-based apps should opt-in by adding this line to your `config.ru`:
55
+
56
+ ```use GdsApi::GovukRequestIdSniffer```
57
+
58
+
46
59
  ## Test Helpers
47
60
 
48
61
  There are also test helpers for stubbing various requests in other apps.
49
62
  Example usage of the panopticon helper:
50
63
 
51
- In test_helper.rb:
64
+ In `test_helper.rb`:
52
65
 
53
66
  require 'gds_api/test_helpers/panopticon'
54
67
 
@@ -63,7 +76,7 @@ In the test:
63
76
 
64
77
  ### Dependencies
65
78
 
66
- Some of the helpers come with additional dependencies that you'll need to
79
+ Some of the helpers come with additional dependencies that you'll need to
67
80
  have installed and configured in your consuming app/lib.
68
81
 
69
82
  At time of writing, these are:
@@ -0,0 +1 @@
1
+ require 'gds_api/railtie' if defined?(Rails)
@@ -0,0 +1,15 @@
1
+ class GdsApi::GovukRequestId
2
+ class << self
3
+ def set?
4
+ !(value.nil? || value.empty?)
5
+ end
6
+
7
+ def value
8
+ Thread.current[:govuk_request_id]
9
+ end
10
+
11
+ def value=(new_id)
12
+ Thread.current[:govuk_request_id] = new_id
13
+ end
14
+ end
15
+ end
@@ -2,6 +2,7 @@ require_relative 'response'
2
2
  require_relative 'exceptions'
3
3
  require_relative 'version'
4
4
  require_relative 'null_cache'
5
+ require_relative 'govuk_request_id'
5
6
  require 'lrucache'
6
7
  require 'rest-client'
7
8
 
@@ -165,6 +166,7 @@ module GdsApi
165
166
  end
166
167
 
167
168
  def with_headers(method_params, headers)
169
+ headers = headers.merge(govuk_request_id: GdsApi::GovukRequestId.value) if GdsApi::GovukRequestId.set?
168
170
  method_params.merge(
169
171
  headers: method_params[:headers].merge(headers)
170
172
  )
@@ -0,0 +1,14 @@
1
+ require_relative '../govuk_request_id'
2
+
3
+ module GdsApi
4
+ class GovukRequestIdSniffer
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ GdsApi::GovukRequestId.value = env['HTTP_GOVUK_REQUEST_ID']
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ require_relative 'middleware/govuk_request_id_sniffer'
2
+
3
+ module GdsApi
4
+ class Railtie < Rails::Railtie
5
+ initializer "gds_api.initialize_govuk_request_id_sniffer" do |app|
6
+ Rails.logger.info "Using middleware GdsApi::GovukRequestIdSniffer to sniff for GOVUK-Request-Id header"
7
+ app.middleware.use GdsApi::GovukRequestIdSniffer
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '8.0.0'
2
+ VERSION = '8.1.0'
3
3
  end
@@ -579,7 +579,7 @@ class JsonClientTest < MiniTest::Spec
579
579
  def test_client_can_set_custom_headers_on_gets
580
580
  stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
581
581
 
582
- response = GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json",
582
+ response = GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json",
583
583
  { "HEADER-A" => "B", "HEADER-C" => "D" })
584
584
 
585
585
  assert_requested(:get, %r{/some.json}) do |request|
@@ -591,7 +591,7 @@ class JsonClientTest < MiniTest::Spec
591
591
  def test_client_can_set_custom_headers_on_posts
592
592
  stub_request(:post, "http://some.other.endpoint/some.json").to_return(:status => 200)
593
593
 
594
- response = GdsApi::JsonClient.new.post_json("http://some.other.endpoint/some.json", {},
594
+ response = GdsApi::JsonClient.new.post_json("http://some.other.endpoint/some.json", {},
595
595
  { "HEADER-A" => "B", "HEADER-C" => "D" })
596
596
 
597
597
  assert_requested(:post, %r{/some.json}) do |request|
@@ -603,7 +603,7 @@ class JsonClientTest < MiniTest::Spec
603
603
  def test_client_can_set_custom_headers_on_puts
604
604
  stub_request(:put, "http://some.other.endpoint/some.json").to_return(:status => 200)
605
605
 
606
- response = GdsApi::JsonClient.new.put_json("http://some.other.endpoint/some.json", {},
606
+ response = GdsApi::JsonClient.new.put_json("http://some.other.endpoint/some.json", {},
607
607
  { "HEADER-A" => "B", "HEADER-C" => "D" })
608
608
 
609
609
  assert_requested(:put, %r{/some.json}) do |request|
@@ -615,7 +615,7 @@ class JsonClientTest < MiniTest::Spec
615
615
  def test_client_can_set_custom_headers_on_deletes
616
616
  stub_request(:delete, "http://some.other.endpoint/some.json").to_return(:status => 200)
617
617
 
618
- response = GdsApi::JsonClient.new.delete_json("http://some.other.endpoint/some.json", {},
618
+ response = GdsApi::JsonClient.new.delete_json("http://some.other.endpoint/some.json", {},
619
619
  { "HEADER-A" => "B", "HEADER-C" => "D" })
620
620
 
621
621
  assert_requested(:delete, %r{/some.json}) do |request|
@@ -624,6 +624,26 @@ class JsonClientTest < MiniTest::Spec
624
624
  end
625
625
  end
626
626
 
627
+ def test_govuk_request_id_gets_set_if_present
628
+ GdsApi::GovukRequestId.value = "12345" # set by middleware GovukRequestIdSniffer
629
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
630
+
631
+ GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json")
632
+
633
+ assert_requested(:get, %r{/some.json}) do |request|
634
+ request.headers['Govuk-Request-Id'] == '12345'
635
+ end
636
+ end
637
+
638
+ def test_additional_headers_passed_in_do_not_get_modified
639
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
640
+
641
+ headers = { 'HEADER-A' => 'A' }
642
+ GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json", headers)
643
+
644
+ assert_equal({ 'HEADER-A' => 'A' }, headers)
645
+ end
646
+
627
647
  def test_client_can_decompress_gzip_responses
628
648
  url = "http://some.endpoint/some.json"
629
649
  # {"test": "hello"}
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+ require 'gds_api/middleware/govuk_request_id_sniffer'
3
+
4
+ describe GdsApi::GovukRequestIdSniffer do
5
+ include Rack::Test::Methods
6
+
7
+ let(:inner_app) do
8
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['All good!']] }
9
+ end
10
+
11
+ let(:app) { GdsApi::GovukRequestIdSniffer.new(inner_app) }
12
+
13
+ it "sniffs the govuk request id from request headers" do
14
+ header "Govuk-Request-Id", "12345"
15
+ get "/"
16
+ assert_equal '12345', GdsApi::GovukRequestId.value
17
+ end
18
+ end
data/test/test_helper.rb CHANGED
@@ -8,6 +8,7 @@ Bundler.setup :default, :development, :test
8
8
 
9
9
  require 'minitest/autorun'
10
10
  require 'rack/utils'
11
+ require 'rack/test'
11
12
  require 'simplecov'
12
13
  require 'simplecov-rcov'
13
14
  require 'mocha'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.0
4
+ version: 8.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -187,6 +187,22 @@ dependencies:
187
187
  - - ! '>='
188
188
  - !ruby/object:Gem::Version
189
189
  version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: rack-test
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
190
206
  - !ruby/object:Gem::Dependency
191
207
  name: simplecov
192
208
  requirement: !ruby/object:Gem::Requirement
@@ -258,10 +274,12 @@ executables: []
258
274
  extensions: []
259
275
  extra_rdoc_files: []
260
276
  files:
277
+ - lib/gds-api-adapters.rb
261
278
  - lib/gds_api/support.rb
262
279
  - lib/gds_api/rummager.rb
263
280
  - lib/gds_api/part_methods.rb
264
281
  - lib/gds_api/need_api.rb
282
+ - lib/gds_api/railtie.rb
265
283
  - lib/gds_api/exceptions.rb
266
284
  - lib/gds_api/json_client.rb
267
285
  - lib/gds_api/base.rb
@@ -290,9 +308,11 @@ files:
290
308
  - lib/gds_api/needotron.rb
291
309
  - lib/gds_api/null_cache.rb
292
310
  - lib/gds_api/imminence.rb
311
+ - lib/gds_api/govuk_request_id.rb
293
312
  - lib/gds_api/worldwide.rb
294
313
  - lib/gds_api/mapit.rb
295
314
  - lib/gds_api/helpers.rb
315
+ - lib/gds_api/middleware/govuk_request_id_sniffer.rb
296
316
  - lib/gds_api/panopticon.rb
297
317
  - lib/gds_api/asset_manager.rb
298
318
  - lib/gds_api/gov_uk_delivery.rb
@@ -314,6 +334,7 @@ files:
314
334
  - test/gov_uk_delivery_test.rb
315
335
  - test/panopticon_api_test.rb
316
336
  - test/panopticon_registerer_test.rb
337
+ - test/middleware/govuk_request_id_sniffer_test.rb
317
338
  - test/rummager_test.rb
318
339
  - test/json_client_test.rb
319
340
  - test/content_api_test.rb
@@ -343,7 +364,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
343
364
  version: '0'
344
365
  segments:
345
366
  - 0
346
- hash: -2067444062740093796
367
+ hash: -1483996754784440811
347
368
  required_rubygems_version: !ruby/object:Gem::Requirement
348
369
  none: false
349
370
  requirements:
@@ -352,7 +373,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
352
373
  version: '0'
353
374
  segments:
354
375
  - 0
355
- hash: -2067444062740093796
376
+ hash: -1483996754784440811
356
377
  requirements: []
357
378
  rubyforge_project:
358
379
  rubygems_version: 1.8.23
@@ -369,6 +390,7 @@ test_files:
369
390
  - test/gov_uk_delivery_test.rb
370
391
  - test/panopticon_api_test.rb
371
392
  - test/panopticon_registerer_test.rb
393
+ - test/middleware/govuk_request_id_sniffer_test.rb
372
394
  - test/rummager_test.rb
373
395
  - test/json_client_test.rb
374
396
  - test/content_api_test.rb