gds-api-adapters 7.11.0 → 7.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/gds_api/json_client.rb +17 -0
- data/lib/gds_api/list_response.rb +1 -1
- data/lib/gds_api/need_api.rb +4 -0
- data/lib/gds_api/test_helpers/need_api.rb +9 -0
- data/lib/gds_api/version.rb +1 -1
- data/test/json_client_test.rb +108 -0
- data/test/need_api_test.rb +59 -0
- metadata +4 -4
data/lib/gds_api/json_client.rb
CHANGED
@@ -203,6 +203,23 @@ module GdsApi
|
|
203
203
|
# Return either a Time object representing the expiry time of the response
|
204
204
|
# or nil if no cache information is provided
|
205
205
|
def response_cache_time(response)
|
206
|
+
if response.headers[:cache_control]
|
207
|
+
# The Cache-control header is composed of a comma-separated string
|
208
|
+
# so split this apart before we look for particular values
|
209
|
+
cache_parts = response.headers[:cache_control].split(',').map(&:strip)
|
210
|
+
|
211
|
+
# If no-cache is present, this takes precedent over any other value
|
212
|
+
# in this header
|
213
|
+
return Time.now.utc if cache_parts.include?("no-cache")
|
214
|
+
|
215
|
+
# Otherwise, look for a 'max-age=123' value, which is the number of
|
216
|
+
# seconds for which to cache the response.
|
217
|
+
max_age = cache_parts.map {|x| x.match(/max-age=(\d+)/) }.compact.first
|
218
|
+
if max_age
|
219
|
+
return Time.now.utc + max_age[1].to_i
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
206
223
|
if response.headers[:expires]
|
207
224
|
Time.httpdate response.headers[:expires]
|
208
225
|
end
|
data/lib/gds_api/need_api.rb
CHANGED
@@ -21,6 +21,15 @@ module GdsApi
|
|
21
21
|
)
|
22
22
|
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
23
23
|
end
|
24
|
+
|
25
|
+
def need_api_has_needs(needs)
|
26
|
+
url = NEED_API_ENDPOINT + "/needs"
|
27
|
+
|
28
|
+
body = response_base.merge(
|
29
|
+
"results" => needs
|
30
|
+
)
|
31
|
+
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
32
|
+
end
|
24
33
|
end
|
25
34
|
end
|
26
35
|
end
|
data/lib/gds_api/version.rb
CHANGED
data/test/json_client_test.rb
CHANGED
@@ -202,6 +202,114 @@ class JsonClientTest < MiniTest::Spec
|
|
202
202
|
end
|
203
203
|
end
|
204
204
|
|
205
|
+
def test_should_respect_cache_control_headers_with_max_age
|
206
|
+
url = "http://some.endpoint/max_age.json"
|
207
|
+
result = {"foo" => "bar"}
|
208
|
+
stub_request(:get, url).to_return(
|
209
|
+
:body => JSON.dump(result),
|
210
|
+
:status => 200,
|
211
|
+
:headers => { "Cache-Control" => "max-age=420, public" } # 6 minutes
|
212
|
+
)
|
213
|
+
|
214
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
215
|
+
|
216
|
+
Timecop.travel( 7 * 60 - 30) do # now + 6 mins 30 secs
|
217
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
218
|
+
|
219
|
+
assert_requested :get, url, times: 1
|
220
|
+
assert_equal response_a.to_hash, response_b.to_hash
|
221
|
+
end
|
222
|
+
|
223
|
+
Timecop.travel( 7 * 60 + 30) do # now + 7 mins 30 secs
|
224
|
+
response_c = GdsApi::JsonClient.new.get_json(url)
|
225
|
+
|
226
|
+
assert_requested :get, url, times: 2
|
227
|
+
assert_equal response_a.to_hash, response_c.to_hash
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_should_respect_cache_control_headers_with_no_cache
|
232
|
+
url = "http://some.endpoint/no_cache.json"
|
233
|
+
result = {"foo" => "bar"}
|
234
|
+
stub_request(:get, url).to_return(
|
235
|
+
:body => JSON.dump(result),
|
236
|
+
:status => 200,
|
237
|
+
:headers => { "Cache-Control" => "no-cache, public" }
|
238
|
+
)
|
239
|
+
|
240
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
241
|
+
|
242
|
+
Timecop.travel( 7 * 60 - 30) do # now + 6 mins 30 secs
|
243
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
244
|
+
|
245
|
+
assert_requested :get, url, times: 2
|
246
|
+
assert_equal response_a.to_hash, response_b.to_hash
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_should_respect_cache_control_headers_with_no_cache_and_max_age
|
251
|
+
url = "http://some.endpoint/no_cache_and_max_age.json"
|
252
|
+
result = {"foo" => "bar"}
|
253
|
+
stub_request(:get, url).to_return(
|
254
|
+
:body => JSON.dump(result),
|
255
|
+
:status => 200,
|
256
|
+
:headers => { "Cache-Control" => "max-age=600, no-cache, public" }
|
257
|
+
)
|
258
|
+
|
259
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
260
|
+
|
261
|
+
Timecop.travel( 7 * 60 - 30) do # now + 6 mins 30 secs
|
262
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
263
|
+
|
264
|
+
assert_requested :get, url, times: 2
|
265
|
+
assert_equal response_a.to_hash, response_b.to_hash
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_should_use_cache_control_headers_over_expires_headers
|
270
|
+
url = "http://some.endpoint/url.json"
|
271
|
+
result = {"foo" => "bar"}
|
272
|
+
stub_request(:get, url).to_return(
|
273
|
+
:body => JSON.dump(result),
|
274
|
+
:status => 200,
|
275
|
+
:headers => {
|
276
|
+
"Cache-Control" => "no-cache",
|
277
|
+
"Expires" => (Time.now + 7 * 60).utc.httpdate
|
278
|
+
}
|
279
|
+
)
|
280
|
+
|
281
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
282
|
+
|
283
|
+
Timecop.travel( 7 * 60 - 30) do # now + 6 mins 30 secs
|
284
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
285
|
+
|
286
|
+
assert_requested :get, url, times: 2
|
287
|
+
assert_equal response_a.to_hash, response_b.to_hash
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_should_fallback_to_expires_headers_if_cache_control_is_malformed
|
292
|
+
url = "http://some.endpoint/url.json"
|
293
|
+
result = {"foo" => "bar"}
|
294
|
+
stub_request(:get, url).to_return(
|
295
|
+
:body => JSON.dump(result),
|
296
|
+
:status => 200,
|
297
|
+
:headers => {
|
298
|
+
"Cache-Control" => "foo, bar, baz",
|
299
|
+
"Expires" => (Time.now + 7 * 60).utc.httpdate
|
300
|
+
}
|
301
|
+
)
|
302
|
+
|
303
|
+
response_a = GdsApi::JsonClient.new.get_json(url)
|
304
|
+
|
305
|
+
Timecop.travel( 7 * 60 - 30) do # now + 6 mins 30 secs
|
306
|
+
response_b = GdsApi::JsonClient.new.get_json(url)
|
307
|
+
|
308
|
+
assert_requested :get, url, times: 1
|
309
|
+
assert_equal response_a.to_hash, response_b.to_hash
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
205
313
|
def test_should_raise_http_not_found_if_404_returned_from_endpoint
|
206
314
|
url = "http://some.endpoint/some.json"
|
207
315
|
stub_request(:get, url).to_return(:body => "{}", :status => 404)
|
data/test/need_api_test.rb
CHANGED
@@ -10,6 +10,65 @@ describe GdsApi::NeedApi do
|
|
10
10
|
@api = GdsApi::NeedApi.new(@base_api_url)
|
11
11
|
end
|
12
12
|
|
13
|
+
describe "requesting needs" do
|
14
|
+
it "should return a list of all needs" do
|
15
|
+
req = need_api_has_needs([
|
16
|
+
{
|
17
|
+
"role" => "parent",
|
18
|
+
"goal" => "apply for a primary school place",
|
19
|
+
"benefit" => "my child can start school",
|
20
|
+
"organisation_ids" => ["department-for-education"],
|
21
|
+
"organisations" => [
|
22
|
+
{
|
23
|
+
"id" => "department-for-education",
|
24
|
+
"name" => "Department for Education",
|
25
|
+
}
|
26
|
+
],
|
27
|
+
"justifications" => [
|
28
|
+
"it's something only government does",
|
29
|
+
"the government is legally obliged to provide it"
|
30
|
+
],
|
31
|
+
"impact" => "Has serious consequences for the day-to-day lives of your users",
|
32
|
+
"met_when" => [
|
33
|
+
"The user applies for a school place"
|
34
|
+
]
|
35
|
+
},
|
36
|
+
{
|
37
|
+
"role" => "user",
|
38
|
+
"goal" => "find out about becoming a British citizen",
|
39
|
+
"benefit" => "i can take the correct steps to apply for citizenship",
|
40
|
+
"organisation_ids" => ["home-office"],
|
41
|
+
"organisations" => [
|
42
|
+
{
|
43
|
+
"id" => "home-office",
|
44
|
+
"name" => "Home Office",
|
45
|
+
}
|
46
|
+
],
|
47
|
+
"justifications" => [
|
48
|
+
"it's something only government does",
|
49
|
+
"the government is legally obliged to provide it"
|
50
|
+
],
|
51
|
+
"impact" => "Has serious consequences for the day-to-day lives of your users",
|
52
|
+
"met_when" => [
|
53
|
+
"The user finds information about the citizenship test and the next steps"
|
54
|
+
]
|
55
|
+
}
|
56
|
+
])
|
57
|
+
|
58
|
+
needs = @api.needs
|
59
|
+
|
60
|
+
assert_requested(req)
|
61
|
+
assert_equal 2, needs.count
|
62
|
+
|
63
|
+
assert_equal ["parent", "user"], needs.map(&:role)
|
64
|
+
assert_equal ["apply for a primary school place", "find out about becoming a British citizen"], needs.map(&:goal)
|
65
|
+
assert_equal ["my child can start school", "i can take the correct steps to apply for citizenship"], needs.map(&:benefit)
|
66
|
+
|
67
|
+
assert_equal "department-for-education", needs.first.organisations.first.id
|
68
|
+
assert_equal "Department for Education", needs.first.organisations.first.name
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
13
72
|
describe "creating needs" do
|
14
73
|
it "should post to the right endpoint" do
|
15
74
|
request_stub = stub_request(:post, @base_api_url + "/needs").with(
|
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: 7.
|
4
|
+
version: 7.12.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: plek
|
@@ -338,7 +338,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
338
338
|
version: '0'
|
339
339
|
segments:
|
340
340
|
- 0
|
341
|
-
hash:
|
341
|
+
hash: -2562720960350177720
|
342
342
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
343
343
|
none: false
|
344
344
|
requirements:
|
@@ -347,7 +347,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
347
347
|
version: '0'
|
348
348
|
segments:
|
349
349
|
- 0
|
350
|
-
hash:
|
350
|
+
hash: -2562720960350177720
|
351
351
|
requirements: []
|
352
352
|
rubyforge_project:
|
353
353
|
rubygems_version: 1.8.23
|