LVS-JSONService 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LVS-JSONService
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  require_paths:
6
6
  - lib
7
7
  platform: ruby
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{LVS-JSONService}
5
- s.version = "0.3.6"
5
+ s.version = "0.3.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["LVS", "andyjeffries"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -220,7 +220,7 @@ module LVS
220
220
  class Error < StandardError
221
221
  attr_reader :message, :code, :service, :args, :json_response
222
222
 
223
- def initialize(message, code, service, args, response=nil)
223
+ def initialize(message, code=nil, service=nil, args=nil, response=nil)
224
224
  @message = message
225
225
  @code = code
226
226
  @service = service
@@ -235,5 +235,6 @@ module LVS
235
235
  class TimeoutError < Error; end
236
236
  class BackendUnavailableError < Error; end
237
237
  class NotModified < Error; end
238
+ class RequestMismatchError < Error; end
238
239
  end
239
240
  end
@@ -19,7 +19,8 @@ module LVS
19
19
  uri = URI.parse(service)
20
20
 
21
21
  req = Net::HTTP::Post.new(uri.path)
22
- args[:requestId] = unique_request_id
22
+ req.add_field("X-LVS-Request-ID", options[:request_id])
23
+
23
24
  req.form_data = { "object_request" => args.to_json }
24
25
 
25
26
  options[:encrypted] ||= require_ssl?
@@ -78,11 +79,13 @@ module LVS
78
79
  def run_remote_request(service, args, options = {})
79
80
  LVS::JsonService::Logger.debug "Requesting '#{service}' with #{args.to_json}"
80
81
 
82
+ options[:request_id] = unique_request_id
81
83
  if options[:cached_for]
82
84
  timing = "CACHED"
83
85
  response, result = Rails.cache.fetch([service, args].cache_key, :expires_in => options[:cached_for]) do
84
86
  start = Time.now
85
87
  response = http_request_with_timeout(service, args, options)
88
+ verify_request_id(response, options[:request_id])
86
89
  net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
87
90
  start = Time.now
88
91
  result = JSON.parse(response.body)
@@ -93,6 +96,7 @@ module LVS
93
96
  else
94
97
  start = Time.now
95
98
  response = http_request_with_timeout(service, args, options)
99
+ verify_request_id(response, options[:request_id])
96
100
  net_timing = ("%.1f" % ((Time.now - start) * 1000)) + "ms"
97
101
  start = Time.now
98
102
  result = JSON.parse(response.body)
@@ -110,6 +114,14 @@ module LVS
110
114
  end
111
115
  result
112
116
  end
117
+
118
+ def verify_request_id(response, request_id)
119
+ returned_request_id = response["X-LVS-Request-ID"]
120
+ if returned_request_id != request_id && !returned_request_id.blank?
121
+ raise LVS::JsonService::RequestMismatchError.new("The sent Request ID (#{request_id}) didn't " +
122
+ "match the returned Request ID (#{returned_request_id}) ")
123
+ end
124
+ end
113
125
  end
114
126
  end
115
127
  end
@@ -234,6 +234,78 @@ describe LVS::JsonService::Request do
234
234
  ClassWithRequest.run_remote_request(@url, @args, @options)
235
235
  }.should raise_error(LVS::JsonService::Error)
236
236
  end
237
+
238
+ it "should generate a unique Request ID" do
239
+ ClassWithRequest.unique_request_id.should_not eql(ClassWithRequest.unique_request_id)
240
+ end
241
+
242
+ it "should send a Request ID" do
243
+ @mock_post = mock(:post, :null_object => true)
244
+ Net::HTTP::Post.stub!(:new).and_return(@mock_post)
245
+ @mock_post.should_receive(:add_field).with("X-LVS-Request-ID", '1234')
246
+
247
+ @mock_http = MockNetHttp.new
248
+ @connection = @mock_http.connection
249
+ Net::HTTP.stub!(:new).and_return(@mock_http)
250
+
251
+ response = {}
252
+ response.stub!(:body).and_return("")
253
+ JSON.stub(:parse)
254
+ ClassWithRequest.stub!(:unique_request_id).and_return('1234')
255
+ ClassWithRequest.stub!(:verify_request_id)
256
+ ClassWithRequest.run_remote_request(@url, @args, @options)
257
+ end
258
+
259
+ it "should raise an error if the Request ID doesn't match the one returned" do
260
+ @mock_post = mock(:post, :null_object => true)
261
+ Net::HTTP::Post.stub!(:new).and_return(@mock_post)
262
+ @mock_http = MockNetHttp.new
263
+ @connection = @mock_http.connection
264
+ Net::HTTP.stub!(:new).and_return(@mock_http)
265
+
266
+ response = {"X-LVS-Request-ID" => "5678"}
267
+ response.stub!(:body).and_return("")
268
+ ClassWithRequest.stub!(:http_request_with_timeout).and_return(response)
269
+ JSON.stub(:parse)
270
+ ClassWithRequest.stub!(:unique_request_id).and_return('1234')
271
+ lambda {
272
+ ClassWithRequest.run_remote_request(@url, @args, @options)
273
+ }.should raise_error(LVS::JsonService::RequestMismatchError)
274
+ end
275
+
276
+ it "shouldn't raise an error if the Request ID matches the one returned" do
277
+ @mock_post = mock(:post, :null_object => true)
278
+ Net::HTTP::Post.stub!(:new).and_return(@mock_post)
279
+ @mock_http = MockNetHttp.new
280
+ @connection = @mock_http.connection
281
+ Net::HTTP.stub!(:new).and_return(@mock_http)
282
+
283
+ response = {"X-LVS-Request-ID" => "5678"}
284
+ response.stub!(:body).and_return("")
285
+ ClassWithRequest.stub!(:http_request_with_timeout).and_return(response)
286
+ JSON.stub(:parse)
287
+ ClassWithRequest.stub!(:unique_request_id).and_return('5678')
288
+ lambda {
289
+ ClassWithRequest.run_remote_request(@url, @args, @options)
290
+ }.should_not raise_error(LVS::JsonService::RequestMismatchError)
291
+ end
292
+
293
+ it "shouldn't raise an error if there is no response Request ID header" do
294
+ @mock_post = mock(:post, :null_object => true)
295
+ Net::HTTP::Post.stub!(:new).and_return(@mock_post)
296
+ @mock_http = MockNetHttp.new
297
+ @connection = @mock_http.connection
298
+ Net::HTTP.stub!(:new).and_return(@mock_http)
299
+
300
+ response = {}
301
+ response.stub!(:body).and_return("")
302
+ ClassWithRequest.stub!(:http_request_with_timeout).and_return(response)
303
+ JSON.stub(:parse)
304
+ ClassWithRequest.stub!(:unique_request_id).and_return('5678')
305
+ lambda {
306
+ ClassWithRequest.run_remote_request(@url, @args, @options)
307
+ }.should_not raise_error(LVS::JsonService::RequestMismatchError)
308
+ end
237
309
  end
238
310
 
239
311
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LVS-JSONService
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - LVS