gds-api-adapters 7.4.0 → 7.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,20 +75,20 @@ module GdsApi
75
75
  end
76
76
  end
77
77
 
78
- def get_json!(url, &create_response)
79
- do_json_request(:get, url, nil, &create_response)
78
+ def get_json!(url, additional_headers = {}, &create_response)
79
+ do_json_request(:get, url, nil, additional_headers, &create_response)
80
80
  end
81
81
 
82
- def post_json!(url, params)
83
- do_json_request(:post, url, params)
82
+ def post_json!(url, params, additional_headers = {})
83
+ do_json_request(:post, url, params, additional_headers)
84
84
  end
85
85
 
86
- def put_json!(url, params)
87
- do_json_request(:put, url, params)
86
+ def put_json!(url, params, additional_headers = {})
87
+ do_json_request(:put, url, params, additional_headers)
88
88
  end
89
89
 
90
- def delete_json!(url, params = nil)
91
- do_request(:delete, url, params)
90
+ def delete_json!(url, params = nil, additional_headers = {})
91
+ do_request(:delete, url, params, additional_headers)
92
92
  end
93
93
 
94
94
  def post_multipart(url, params)
@@ -112,12 +112,13 @@ module GdsApi
112
112
  # method: the symbolic name of the method to use, e.g. :get, :post
113
113
  # url: the request URL
114
114
  # params: the data to send (JSON-serialised) in the request body
115
+ # additional_headers: headers to set on the request (in addition to the default ones)
115
116
  # create_response: optional block to instantiate a custom response object
116
117
  # from the Net::HTTPResponse
117
- def do_json_request(method, url, params = nil, &create_response)
118
+ def do_json_request(method, url, params = nil, additional_headers = {}, &create_response)
118
119
 
119
120
  begin
120
- response = do_request_with_cache(method, url, (params.to_json if params))
121
+ response = do_request_with_cache(method, url, (params.to_json if params), additional_headers)
121
122
 
122
123
  rescue RestClient::ResourceNotFound => e
123
124
  raise GdsApi::HTTPNotFound.new(e.http_code)
@@ -163,6 +164,12 @@ module GdsApi
163
164
  )
164
165
  end
165
166
 
167
+ def with_headers(method_params, headers)
168
+ method_params.merge(
169
+ headers: method_params[:headers].merge(headers)
170
+ )
171
+ end
172
+
166
173
  def with_ssl_options(method_params)
167
174
  method_params.merge(
168
175
  # This is the default value anyway, but we should probably be explicit
@@ -170,7 +177,7 @@ module GdsApi
170
177
  )
171
178
  end
172
179
 
173
- def do_request_with_cache(method, url, params = nil)
180
+ def do_request_with_cache(method, url, params = nil, additional_headers = {})
174
181
  # Only read GET requests from the cache: any other request methods should
175
182
  # always be passed through. Note that this means HEAD requests won't get
176
183
  # cached, but that would involve separating the cache by method and URL.
@@ -182,7 +189,7 @@ module GdsApi
182
189
  return cached_response if cached_response
183
190
  end
184
191
 
185
- response = do_request(method, url, params)
192
+ response = do_request(method, url, params, additional_headers)
186
193
 
187
194
  if use_cache
188
195
  cache_time = response_cache_time(response)
@@ -201,7 +208,7 @@ module GdsApi
201
208
  end
202
209
  end
203
210
 
204
- def do_request(method, url, params = nil)
211
+ def do_request(method, url, params = nil, additional_headers = {})
205
212
  loggable = {request_uri: url, start_time: Time.now.to_f}
206
213
  start_logging = loggable.merge(action: 'start')
207
214
  logger.debug start_logging.to_json
@@ -214,6 +221,7 @@ module GdsApi
214
221
  method_params[:payload] = params
215
222
  method_params = with_auth_options(method_params)
216
223
  method_params = with_timeout(method_params)
224
+ method_params = with_headers(method_params, additional_headers)
217
225
  if URI.parse(url).is_a? URI::HTTPS
218
226
  method_params = with_ssl_options(method_params)
219
227
  end
@@ -1,12 +1,12 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class GdsApi::Support < GdsApi::Base
4
- def create_foi_request(request_details)
5
- post_json!("#{base_url}/foi_requests", { :foi_request => request_details })
4
+ def create_foi_request(request_details, options = {})
5
+ post_json!("#{base_url}/foi_requests", { :foi_request => request_details }, options[:headers] || {})
6
6
  end
7
7
 
8
- def create_problem_report(request_details)
9
- post_json!("#{base_url}/problem_reports", { :problem_report => request_details })
8
+ def create_problem_report(request_details, options = {})
9
+ post_json!("#{base_url}/problem_reports", { :problem_report => request_details }, options[:headers] || {})
10
10
  end
11
11
 
12
12
  private
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '7.4.0'
2
+ VERSION = '7.5.0'
3
3
  end
@@ -468,6 +468,54 @@ class JsonClientTest < MiniTest::Spec
468
468
  assert_equal 2, response.a
469
469
  end
470
470
 
471
+ def test_client_can_set_custom_headers_on_gets
472
+ stub_request(:get, "http://some.other.endpoint/some.json").to_return(:status => 200)
473
+
474
+ response = GdsApi::JsonClient.new.get_json("http://some.other.endpoint/some.json",
475
+ { "HEADER-A" => "B", "HEADER-C" => "D" })
476
+
477
+ assert_requested(:get, %r{/some.json}) do |request|
478
+ headers_with_uppercase_names = Hash[request.headers.collect {|key, value| [key.upcase, value] }]
479
+ headers_with_uppercase_names["HEADER-A"] == "B" && headers_with_uppercase_names["HEADER-C"] == "D"
480
+ end
481
+ end
482
+
483
+ def test_client_can_set_custom_headers_on_posts
484
+ stub_request(:post, "http://some.other.endpoint/some.json").to_return(:status => 200)
485
+
486
+ response = GdsApi::JsonClient.new.post_json("http://some.other.endpoint/some.json", {},
487
+ { "HEADER-A" => "B", "HEADER-C" => "D" })
488
+
489
+ assert_requested(:post, %r{/some.json}) do |request|
490
+ headers_with_uppercase_names = Hash[request.headers.collect {|key, value| [key.upcase, value] }]
491
+ headers_with_uppercase_names["HEADER-A"] == "B" && headers_with_uppercase_names["HEADER-C"] == "D"
492
+ end
493
+ end
494
+
495
+ def test_client_can_set_custom_headers_on_puts
496
+ stub_request(:put, "http://some.other.endpoint/some.json").to_return(:status => 200)
497
+
498
+ response = GdsApi::JsonClient.new.put_json("http://some.other.endpoint/some.json", {},
499
+ { "HEADER-A" => "B", "HEADER-C" => "D" })
500
+
501
+ assert_requested(:put, %r{/some.json}) do |request|
502
+ headers_with_uppercase_names = Hash[request.headers.collect {|key, value| [key.upcase, value] }]
503
+ headers_with_uppercase_names["HEADER-A"] == "B" && headers_with_uppercase_names["HEADER-C"] == "D"
504
+ end
505
+ end
506
+
507
+ def test_client_can_set_custom_headers_on_deletes
508
+ stub_request(:delete, "http://some.other.endpoint/some.json").to_return(:status => 200)
509
+
510
+ response = GdsApi::JsonClient.new.delete_json("http://some.other.endpoint/some.json", {},
511
+ { "HEADER-A" => "B", "HEADER-C" => "D" })
512
+
513
+ assert_requested(:delete, %r{/some.json}) do |request|
514
+ headers_with_uppercase_names = Hash[request.headers.collect {|key, value| [key.upcase, value] }]
515
+ headers_with_uppercase_names["HEADER-A"] == "B" && headers_with_uppercase_names["HEADER-C"] == "D"
516
+ end
517
+ end
518
+
471
519
  def test_client_can_decompress_gzip_responses
472
520
  url = "http://some.endpoint/some.json"
473
521
  # {"test": "hello"}
@@ -22,7 +22,17 @@ describe GdsApi::Support do
22
22
  assert_requested(stub_post)
23
23
  end
24
24
 
25
- it "throws an exception when the support app isn't available" do
25
+ it "can add a custom header onto the FOI request to the support app" do
26
+ stub_request(:post, "#{@base_api_url}/foi_requests")
27
+
28
+ @api.create_foi_request({}, headers: { "X-Varnish" => "12345"})
29
+
30
+ assert_requested(:post, %r{/foi_requests}) do |request|
31
+ request.headers["X-Varnish"] == "12345"
32
+ end
33
+ end
34
+
35
+ it "throws an exception when the support app isn't available" do
26
36
  support_isnt_available
27
37
 
28
38
  assert_raises(GdsApi::HTTPErrorResponse) { @api.create_foi_request({}) }
@@ -40,7 +50,17 @@ describe GdsApi::Support do
40
50
  assert_requested(stub_post)
41
51
  end
42
52
 
43
- it "throws an exception when the support app isn't available" do
53
+ it "can add a custom header onto the problem_report request to the support app" do
54
+ stub_request(:post, "#{@base_api_url}/problem_reports")
55
+
56
+ @api.create_problem_report({}, headers: { "X-Varnish" => "12345"})
57
+
58
+ assert_requested(:post, %r{/problem_reports}) do |request|
59
+ request.headers["X-Varnish"] == "12345"
60
+ end
61
+ end
62
+
63
+ it "throws an exception when the support app isn't available" do
44
64
  support_isnt_available
45
65
 
46
66
  assert_raises(GdsApi::HTTPErrorResponse) { @api.create_problem_report({}) }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 7.4.0
5
+ version: 7.5.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Stewart
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-08-21 00:00:00 Z
13
+ date: 2013-09-05 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -260,7 +260,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
260
260
  requirements:
261
261
  - - ">="
262
262
  - !ruby/object:Gem::Version
263
- hash: 167124885650140799
263
+ hash: -3019861468380170636
264
264
  segments:
265
265
  - 0
266
266
  version: "0"
@@ -269,7 +269,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
269
269
  requirements:
270
270
  - - ">="
271
271
  - !ruby/object:Gem::Version
272
- hash: 167124885650140799
272
+ hash: -3019861468380170636
273
273
  segments:
274
274
  - 0
275
275
  version: "0"