Skip to content

Commit c11c1d5

Browse files
committed
Add test helper that returns content item
stub_conditional_loader_returns_content_item helper corresponds to GdsApi adapters helpers stubs: - https://github.com/alphagov/gds-api-adapters/blob/4d4b2ab6969f333eb1cab94ff8e93d7ff8aeebf1/lib/gds_api/test_helpers/content_store.rb#L21 - https://github.com/alphagov/gds-api-adapters/blob/4d4b2ab6969f333eb1cab94ff8e93d7ff8aeebf1/lib/gds_api/test_helpers/publishing_api.rb#L210 The Struct mimics WebMock response returned by the stubs. The `to_h` method parses JSON, like WebMock middleware would. I considered returning a RestClient::Response but has a lot of unnecessary complexity (e.g. redirect history). A lightweight Response struct seems sufficient.
1 parent f595f91 commit c11c1d5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/govuk_content_item_loader.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require "govuk_content_item_loader/govuk_graphql_traffic_rates"
22
require "govuk_content_item_loader/govuk_conditional_content_item_loader"
33
require "govuk_content_item_loader/version"
4+
require "govuk_content_item_loader/test_helpers"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
end

0 commit comments

Comments
 (0)