gds-api-adapters 5.5.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,56 +0,0 @@
1
- module GdsApi
2
- class ContentApi < GdsApi::Base
3
- class Response < GdsApi::Response
4
- # Responses from the content API can be configured to use relative URLs
5
- # for `web_url` properties. This is useful on non-canonical frontends,
6
- # such as those in staging environments.
7
- #
8
- # Example:
9
- #
10
- # r = Response.new(response, web_urls_relative_to: "https://www.gov.uk")
11
- # r.results[0].web_url
12
- # => "/bank-holidays"
13
-
14
- WEB_URL_KEYS = ["web_url"]
15
-
16
- def initialize(http_response, options = {})
17
- if options[:web_urls_relative_to]
18
- @web_urls_relative_to = URI.parse(options[:web_urls_relative_to])
19
- else
20
- @web_urls_relative_to = nil
21
- end
22
-
23
- super(http_response)
24
- end
25
-
26
- def to_hash
27
- @parsed ||= transform_parsed(JSON.parse(@http_response.body))
28
- end
29
-
30
- private
31
- def transform_parsed(value)
32
- case value
33
- when Hash
34
- Hash[value.map { |k, v|
35
- # NOTE: Don't bother transforming if the value is nil
36
- if @web_urls_relative_to && WEB_URL_KEYS.include?(k) && v
37
- # Use relative URLs to route when the web_url value is on the
38
- # same domain as the site root. Note that we can't just use the
39
- # `route_to` method, as this would give us technically correct
40
- # but potentially confusing `//host/path` URLs for URLs with the
41
- # same scheme but different hosts.
42
- relative_url = @web_urls_relative_to.route_to(v)
43
- [k, relative_url.host ? v : relative_url.to_s]
44
- else
45
- [k, transform_parsed(v)]
46
- end
47
- }]
48
- when Array
49
- value.map { |v| transform_parsed(v) }
50
- else
51
- value
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,97 +0,0 @@
1
- require "test_helper"
2
- require "gds_api/content_api"
3
-
4
- describe "GdsApi::ContentApi::Response" do
5
-
6
- DummyNetResponse = Struct.new(:body)
7
-
8
- def response_for(net_response)
9
- root = "https://www.gov.uk"
10
- GdsApi::ContentApi::Response.new(net_response, web_urls_relative_to: root)
11
- end
12
-
13
- it "should map web URLs" do
14
- body = {
15
- "web_url" => "https://www.gov.uk/test"
16
- }.to_json
17
- assert_equal "/test", response_for(DummyNetResponse.new(body)).web_url
18
- end
19
-
20
- it "should leave other properties alone" do
21
- body = {
22
- "title" => "Title",
23
- "description" => "Description"
24
- }.to_json
25
- response = response_for(DummyNetResponse.new(body))
26
- assert_equal "Title", response.title
27
- assert_equal "Description", response.description
28
- end
29
-
30
- it "should traverse into arrays" do
31
- body = {
32
- "other_urls" => [
33
- { "title" => "Pies", "web_url" => "https://www.gov.uk/pies" },
34
- { "title" => "Cheese", "web_url" => "https://www.gov.uk/cheese" }
35
- ]
36
- }.to_json
37
-
38
- response = response_for(DummyNetResponse.new(body))
39
- assert_equal "/pies", response.other_urls[0].web_url
40
- assert_equal "/cheese", response.other_urls[1].web_url
41
- end
42
-
43
- it "should traverse into hashes" do
44
- body = {
45
- "details" => {
46
- "chirality" => "widdershins",
47
- "web_url" => "https://www.gov.uk/left"
48
- }
49
- }.to_json
50
-
51
- response = response_for(DummyNetResponse.new(body))
52
- assert_equal "/left", response.details.web_url
53
- end
54
-
55
- it "should handle nil values" do
56
- body = {"web_url" => nil}.to_json
57
-
58
- response = response_for(DummyNetResponse.new(body))
59
- assert_nil response.web_url
60
- end
61
-
62
- it "should handle query parameters" do
63
- body = {
64
- "web_url" => "https://www.gov.uk/thing?does=stuff"
65
- }.to_json
66
-
67
- response = response_for(DummyNetResponse.new(body))
68
- assert_equal "/thing?does=stuff", response.web_url
69
- end
70
-
71
- it "should handle fragments" do
72
- body = {
73
- "web_url" => "https://www.gov.uk/thing#part-2"
74
- }.to_json
75
-
76
- response = response_for(DummyNetResponse.new(body))
77
- assert_equal "/thing#part-2", response.web_url
78
- end
79
-
80
- it "should keep URLs from other domains absolute" do
81
- body = {
82
- "web_url" => "https://www.example.com/example"
83
- }.to_json
84
-
85
- response = response_for(DummyNetResponse.new(body))
86
- assert_equal "https://www.example.com/example", response.web_url
87
- end
88
-
89
- it "should keep URLs with other schemes absolute" do
90
- body = {
91
- "web_url" => "http://www.example.com/example"
92
- }.to_json
93
-
94
- response = response_for(DummyNetResponse.new(body))
95
- assert_equal "http://www.example.com/example", response.web_url
96
- end
97
- end