govuk_content_item_loader 1.1.2 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 208a339dd9691090128a655acbe22bc1c403ce172fd1555318bf15fb67cd533e
4
- data.tar.gz: d32050cfdbaf157b5da48878fdb3e5c52c1a0510653d9deae36899da7adbbf0f
3
+ metadata.gz: d9f2c50ebc136f3c6f3303d1bff102d8f8cb603a2df8fa0d4fa23710ed4c0b6d
4
+ data.tar.gz: 5d86e91dded77543c5b30d6310245d230ed60cca32a3367406147952427640c4
5
5
  SHA512:
6
- metadata.gz: 5ed40d8b8dbf4e265ead813865c8b462e22134f4a2e74b55e9df2b4ae506c23622c9d9d085dffd03f4c7c083d74869756356d30ed96f5cff9d56e6dfe9bfaa72
7
- data.tar.gz: 65cc4a6631a46bc07d2ab8dc80daf212d1c953e8e4c300329c3b54fa4c75de5aae2da06af57a2accbca7d074988fb1a0c78bacfe56ccc2cab6599666e37ce199
6
+ metadata.gz: be560a01f12d317253875eeb418a1c3f4962bd31d3c0a63ca2cacb8e74505a3860a1c64080d48374fe8ecf70e66eeb514c88613a3bc1c3cf23921677d8be0188
7
+ data.tar.gz: 9173b6bf38e6eb65341ec4b7b77c96c65be812d4548a8795822a015ae6cae24bcd2be1656ce4ccabc1e9a82ddfbdf182318dc00e46c5f276b3d51b4c86941528
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0
4
+
5
+ * Add test helpers which provide stubs
6
+
3
7
  ## 1.1.2
4
8
 
5
9
  * Update dependencies
data/README.md CHANGED
@@ -86,6 +86,10 @@ loader = GovukGraphql::ConditionalContentItemLoader.new(
86
86
 
87
87
  The decision logic is also exposed via `can_load_from_graphql?`, allowing applications to make the routing decision themselves and implement custom fallback or error-handling behaviour if needed.
88
88
 
89
+ ## Test helpers
90
+
91
+ There are also test helpers for stubbing responses in other apps. See [lib/test_helpers.rb](/lib/test_helpers.rb).
92
+
89
93
  ## Licence
90
94
 
91
95
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,58 @@
1
+ require "json"
2
+
3
+ module GovukConditionalContentItemLoaderTestHelpers
4
+ Response = Struct.new(:status, :body, :headers) do
5
+ def to_h
6
+ JSON.parse(body)
7
+ end
8
+ end
9
+
10
+ # Stubs the loader returns a content item
11
+ # The following options can be passed in:
12
+ #
13
+ # :max_age will set the max-age of the Cache-Control header in the response. Defaults to 900
14
+ # :private if true, the Cache-Control header will include the "private" directive. By default it
15
+ # will include "public"
16
+ def stub_conditional_loader_returns_content_item(body, options = {})
17
+ body.to_json unless body.is_a?(String)
18
+ max_age = options.fetch(:max_age, 900)
19
+ visibility = options[:private] ? "private" : "public"
20
+
21
+ response = Response.new(200, body, {
22
+ cache_control: "#{visibility}, max-age=#{max_age}",
23
+ date: Time.now.httpdate,
24
+ })
25
+
26
+ loader = instance_double(GovukConditionalContentItemLoader, load: response)
27
+
28
+ allow(GovukConditionalContentItemLoader).to receive(:new).with(request: anything)
29
+ .and_return(loader)
30
+ allow(loader).to receive(:load).and_return(body)
31
+
32
+ response
33
+ end
34
+
35
+ # Stubs the loader returns a error response
36
+ # The following options can be passed in:
37
+ #
38
+ # :status the HTTP status code for the error. Defaults to 404
39
+ # :message the error message. Defaults to "Not Found"
40
+ # :error_details optional additional error details to attach to the exception
41
+ # :http_body optional raw response body to attach to the exception
42
+ def stub_conditional_loader_does_not_return_content_item(options = {})
43
+ status = options.fetch(:status, 404)
44
+ message = options.fetch(:message, "Not Found")
45
+ error_details = options[:error_details]
46
+ http_body = options[:http_body]
47
+
48
+ error = GdsApi::HTTPErrorResponse.new(status, message, error_details, http_body)
49
+
50
+ loader = instance_double(GovukConditionalContentItemLoader)
51
+
52
+ allow(GovukConditionalContentItemLoader).to receive(:new).with(request: anything)
53
+ .and_return(loader)
54
+ allow(loader).to receive(:load).and_raise(error)
55
+
56
+ error
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module GovukContentItemLoader
2
- VERSION = "1.1.2".freeze
2
+ VERSION = "1.2.0".freeze
3
3
  end
@@ -1,3 +1,4 @@
1
1
  require "govuk_content_item_loader/govuk_graphql_traffic_rates"
2
2
  require "govuk_content_item_loader/govuk_conditional_content_item_loader"
3
3
  require "govuk_content_item_loader/version"
4
+ require "govuk_content_item_loader/test_helpers"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_content_item_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
@@ -162,6 +162,7 @@ files:
162
162
  - lib/govuk_content_item_loader.rb
163
163
  - lib/govuk_content_item_loader/govuk_conditional_content_item_loader.rb
164
164
  - lib/govuk_content_item_loader/govuk_graphql_traffic_rates.rb
165
+ - lib/govuk_content_item_loader/test_helpers.rb
165
166
  - lib/govuk_content_item_loader/version.rb
166
167
  homepage: https://github.com/alphagov/govuk_content_item_loader
167
168
  licenses: