-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontent_store.rb
93 lines (83 loc) · 3.41 KB
/
content_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require "gds_api/test_helpers/json_client_helper"
require "gds_api/test_helpers/content_item_helpers"
require "json"
module GdsApi
module TestHelpers
module ContentStore
include ContentItemHelpers
def content_store_endpoint(draft: false)
draft ? Plek.find("draft-content-store") : Plek.find("content-store")
end
# Stubs a content item in the content store.
# The following options can be passed in:
#
# :max_age will set the max-age of the Cache-Control header in the response. Defaults to 900
# :private if true, the Cache-Control header will include the "private" directive. By default it
# will include "public"
# :draft will point to the draft content store if set to true
def stub_content_store_has_item(base_path, body = content_item_for_base_path(base_path), options = {})
max_age = options.fetch(:max_age, 900)
visibility = options[:private] ? "private" : "public"
body = body.to_json unless body.is_a?(String)
endpoint = content_store_endpoint(draft: options[:draft])
stub_request(:get, "#{endpoint}/content#{base_path}").to_return(
status: 200,
body:,
headers: {
cache_control: "#{visibility}, max-age=#{max_age}",
date: Time.now.httpdate,
},
)
end
def stub_content_store_does_not_have_item(base_path, options = {})
endpoint = content_store_endpoint(draft: options[:draft])
stub_request(:get, "#{endpoint}/content#{base_path}").to_return(status: 404, headers: {})
stub_request(:get, "#{endpoint}/incoming-links#{base_path}").to_return(status: 404, headers: {})
end
# Content store has gone item
#
# Stubs a content item in the content store to respond with 410 HTTP Status Code and response body with 'format' set to 'gone'.
#
# @param base_path [String]
# @param body [Hash]
# @param options [Hash]
# @option options [String] draft Will point to the draft content store if set to true
#
# @example
#
# stub_content_store.stub_content_store_has_gone_item('/sample-slug')
#
# # Will return HTTP Status Code 410 and the following response body:
# {
# "title" => nil,
# "description" => nil,
# "format" => "gone",
# "schema_name" => "gone",
# "public_updated_at" => nil,
# "base_path" => "/sample-slug",
# "withdrawn_notice" => {},
# "details" => {}
# }
def stub_content_store_has_gone_item(base_path, body = gone_content_item_for_base_path(base_path), options = {})
body = body.to_json unless body.is_a?(String)
endpoint = content_store_endpoint(draft: options[:draft])
stub_request(:get, "#{endpoint}/content#{base_path}").to_return(
status: 410,
body:,
headers: {},
)
end
def stub_content_store_isnt_available
stub_request(:any, /#{content_store_endpoint}\/.*/).to_return(status: 503)
end
def content_item_for_base_path(base_path)
super.merge("base_path" => base_path)
end
def stub_content_store_has_incoming_links(base_path, links)
url = "#{content_store_endpoint}/incoming-links#{base_path}"
body = links.to_json
stub_request(:get, url).to_return(body:)
end
end
end
end