manticore 0.6.1-java → 0.6.2-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,7 @@ module Manticore
9
9
  def async
10
10
  AsyncProxy.new(self)
11
11
  end
12
+
12
13
  alias_method :parallel, :async
13
14
  alias_method :batch, :async
14
15
 
@@ -20,6 +21,7 @@ module Manticore
20
21
 
21
22
  class BaseProxy
22
23
  include ProxiesInterface
24
+
23
25
  def initialize(client)
24
26
  @client = client
25
27
  end
@@ -55,4 +57,4 @@ module Manticore
55
57
  end
56
58
  end
57
59
  end
58
- end
60
+ end
@@ -36,7 +36,7 @@ module Manticore
36
36
  value: cookie.get_value,
37
37
  secure: cookie.is_secure,
38
38
  persistent: cookie.is_persistent,
39
- spec_version: cookie.get_version
39
+ spec_version: cookie.get_version,
40
40
  )
41
41
  end
42
42
 
@@ -63,16 +63,16 @@ module Manticore
63
63
  attr_reader :comment, :comment_url, :domain, :expires, :name, :path, :ports, :value, :spec_version
64
64
 
65
65
  def initialize(args)
66
- @comment = args.fetch(:comment, nil)
67
- @comment_url = args.fetch(:comment_url, nil)
68
- @domain = args.fetch(:domain, nil)
69
- @expires = args.fetch(:expires, nil)
70
- @name = args.fetch(:name, nil)
71
- @path = args.fetch(:path, nil)
72
- @ports = args.fetch(:ports, nil)
73
- @value = args.fetch(:value, nil)
74
- @secure = args.fetch(:secure, nil)
75
- @persistent = args.fetch(:persistent, nil)
66
+ @comment = args.fetch(:comment, nil)
67
+ @comment_url = args.fetch(:comment_url, nil)
68
+ @domain = args.fetch(:domain, nil)
69
+ @expires = args.fetch(:expires, nil)
70
+ @name = args.fetch(:name, nil)
71
+ @path = args.fetch(:path, nil)
72
+ @ports = args.fetch(:ports, nil)
73
+ @value = args.fetch(:value, nil)
74
+ @secure = args.fetch(:secure, nil)
75
+ @persistent = args.fetch(:persistent, nil)
76
76
  @spec_version = args.fetch(:spec_version, nil)
77
77
  end
78
78
 
@@ -104,4 +104,4 @@ module Manticore
104
104
  !@persistent
105
105
  end
106
106
  end
107
- end
107
+ end
@@ -1,4 +1,4 @@
1
- require 'forwardable'
1
+ require "forwardable"
2
2
 
3
3
  module Manticore
4
4
  # Mix-in that can be used to add Manticore functionality to arbitrary classes.
@@ -46,4 +46,4 @@ module Manticore
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
@@ -4,7 +4,7 @@ class Java::OrgApacheHttpClientMethods::HttpRequestBase
4
4
 
5
5
  # Provides an easy way to get the request headers from any request
6
6
  def headers
7
- Hash[*get_all_headers.flat_map {|h| [h.name, h.value] }]
7
+ Hash[*get_all_headers.flat_map { |h| [h.name, h.value] }]
8
8
  end
9
9
 
10
10
  # Get a single request header
@@ -15,7 +15,7 @@ module Manticore
15
15
  include_package "org.apache.http.util"
16
16
  include_package "org.apache.http.protocol"
17
17
  java_import "org.apache.http.client.protocol.HttpClientContext"
18
- java_import 'java.util.concurrent.Callable'
18
+ java_import "java.util.concurrent.Callable"
19
19
 
20
20
  include ResponseHandler
21
21
  include Callable
@@ -29,14 +29,14 @@ module Manticore
29
29
  # @param request [HttpRequestBase] The underlying request object
30
30
  # @param context [HttpContext] The underlying HttpContext
31
31
  def initialize(client, request, context, &block)
32
- @client = client
32
+ @client = client
33
33
  @request = request
34
34
  @context = context
35
35
  @handlers = {
36
- success: block || Proc.new {|resp| resp.body },
37
- failure: Proc.new {|ex| raise ex },
38
- cancelled: Proc.new {},
39
- complete: []
36
+ success: block || Proc.new { |resp| resp.body },
37
+ failure: Proc.new { |ex| raise ex },
38
+ cancelled: Proc.new { },
39
+ complete: [],
40
40
  }
41
41
  end
42
42
 
@@ -91,9 +91,9 @@ module Manticore
91
91
  def final_url
92
92
  call_once
93
93
  last_request = context.get_attribute ExecutionContext.HTTP_REQUEST
94
- last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
95
- host = last_host.to_uri
96
- url = last_request.get_uri
94
+ last_host = context.get_attribute ExecutionContext.HTTP_TARGET_HOST
95
+ host = last_host.to_uri
96
+ url = last_request.get_uri
97
97
  URI.join(host, url.to_s)
98
98
  end
99
99
 
@@ -115,15 +115,16 @@ module Manticore
115
115
  def body(&block)
116
116
  call_once
117
117
  @body ||= begin
118
- if entity = @response.get_entity
119
- EntityConverter.new.read_entity(entity, &block)
120
- end
121
- rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
122
- raise StreamClosedException.new("Could not read from stream: #{e.message}")
123
- # ensure
124
- # @request.release_connection
125
- end
118
+ if entity = @response.get_entity
119
+ EntityConverter.new.read_entity(entity, &block)
120
+ end
121
+ rescue Java::JavaIo::IOException, Java::JavaNet::SocketException, IOError => e
122
+ raise StreamClosedException.new("Could not read from stream: #{e.message}")
123
+ # ensure
124
+ # @request.release_connection
125
+ end
126
126
  end
127
+
127
128
  alias_method :read_body, :body
128
129
 
129
130
  # Returns true if this response has been called (requested and populated) yet
@@ -196,6 +197,7 @@ module Manticore
196
197
  @handlers[:success] = block
197
198
  self
198
199
  end
200
+
199
201
  alias_method :success, :on_success
200
202
 
201
203
  # Set handler for failure responses
@@ -206,8 +208,9 @@ module Manticore
206
208
  @handlers[:failure] = block
207
209
  self
208
210
  end
211
+
209
212
  alias_method :failure, :on_failure
210
- alias_method :fail, :on_failure
213
+ alias_method :fail, :on_failure
211
214
 
212
215
  # Set handler for cancelled requests. NB: Not actually used right now?
213
216
  # @param block Proc which will be invoked on a on a cancelled response.
@@ -217,8 +220,9 @@ module Manticore
217
220
  @handlers[:cancelled] = block
218
221
  self
219
222
  end
220
- alias_method :cancelled, :on_cancelled
221
- alias_method :cancellation, :on_cancelled
223
+
224
+ alias_method :cancelled, :on_cancelled
225
+ alias_method :cancellation, :on_cancelled
222
226
  alias_method :on_cancellation, :on_cancelled
223
227
 
224
228
  # Set handler for completed requests
@@ -229,8 +233,9 @@ module Manticore
229
233
  @handlers[:complete] = Array(@handlers[:complete]).compact + [block]
230
234
  self
231
235
  end
232
- alias_method :complete, :on_complete
233
- alias_method :completed, :on_complete
236
+
237
+ alias_method :complete, :on_complete
238
+ alias_method :completed, :on_complete
234
239
  alias_method :on_completed, :on_complete
235
240
 
236
241
  def times_retried
@@ -247,10 +252,10 @@ module Manticore
247
252
  # Implementation of {http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html#handleResponse(org.apache.http.HttpResponse) ResponseHandler#handleResponse}
248
253
  # @param response [Response] The underlying Java Response object
249
254
  def handleResponse(response)
250
- @response = response
251
- @code = response.get_status_line.get_status_code
252
- @message = response.get_status_line.get_reason_phrase
253
- @headers = response.get_all_headers.each_with_object({}) do |h, o|
255
+ @response = response
256
+ @code = response.get_status_line.get_status_code
257
+ @message = response.get_status_line.get_reason_phrase
258
+ @headers = response.get_all_headers.each_with_object({}) do |h, o|
254
259
  key = h.get_name.downcase
255
260
  if o.key?(key)
256
261
  o[key] = Array(o[key]) unless o[key].is_a?(Array)
@@ -274,7 +279,7 @@ module Manticore
274
279
  end
275
280
 
276
281
  def execute_complete
277
- @handlers[:complete].each {|h| h.call(self) }
282
+ @handlers[:complete].each { |h| h.call(self) }
278
283
  end
279
284
  end
280
285
  end
@@ -33,12 +33,12 @@ module Manticore
33
33
  # @return [Manticore::StubbedResponse] self
34
34
  def stub(stubs)
35
35
  if stubs.key? :cookies
36
- stubs[:cookies].keys.each {|key| stubs[:cookies][key] = Array(stubs[:cookies][key]) }
36
+ stubs[:cookies].keys.each { |key| stubs[:cookies][key] = Array(stubs[:cookies][key]) }
37
37
  end
38
38
  stubs[:code] ||= 200
39
39
 
40
40
  stubs[:headers] ||= {}
41
- stubs[:headers] = Hash[*stubs[:headers].flat_map {|k, v| [k.downcase, v] }]
41
+ stubs[:headers] = Hash[*stubs[:headers].flat_map { |k, v| [k.downcase, v] }]
42
42
  stubs[:headers]["content-length"] ||= stubs[:body].length.to_s if stubs.key?(:body)
43
43
 
44
44
  @stubs = stubs
@@ -69,6 +69,7 @@ module Manticore
69
69
  @body
70
70
  end
71
71
  end
72
+
72
73
  alias_method :read_body, :body
73
74
 
74
75
  # Returns the stubbed cookies of this response. This is the union of cookies from the `:cookies`
@@ -82,8 +83,8 @@ module Manticore
82
83
 
83
84
  def handleResponse(response)
84
85
  raise response[:raises] if response.key?(:raises)
85
- @body = response[:body]
86
- @code = response[:code]
86
+ @body = response[:body]
87
+ @code = response[:code]
87
88
  @headers = response[:headers]
88
89
  @cookies = response[:cookies]
89
90
  Array(@headers["set-cookie"]).each do |cookie|
@@ -99,4 +100,4 @@ module Manticore
99
100
  call unless @called
100
101
  end
101
102
  end
102
- end
103
+ end
@@ -1,3 +1,3 @@
1
1
  module Manticore
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
@@ -1,8 +1,8 @@
1
1
  # this is a generated file, to avoid over-writing it just delete this comment
2
- require 'jar_dependencies'
2
+ require "jar_dependencies"
3
3
 
4
- require_jar( 'commons-logging', 'commons-logging', '1.2' )
5
- require_jar( 'org.apache.httpcomponents', 'httpmime', '4.5.2' )
6
- require_jar( 'commons-codec', 'commons-codec', '1.10' )
7
- require_jar( 'org.apache.httpcomponents', 'httpclient', '4.5.2' )
8
- require_jar( 'org.apache.httpcomponents', 'httpcore', '4.4.4' )
4
+ require_jar("commons-logging", "commons-logging", "1.2")
5
+ require_jar("org.apache.httpcomponents", "httpmime", "4.5.2")
6
+ require_jar("commons-codec", "commons-codec", "1.10")
7
+ require_jar("org.apache.httpcomponents", "httpclient", "4.5.2")
8
+ require_jar("org.apache.httpcomponents", "httpcore", "4.4.4")
@@ -1,4 +1,5 @@
1
- require 'spec_helper'
1
+ # encoding: utf-8
2
+ require "spec_helper"
2
3
  describe Manticore::Client do
3
4
  let(:client) { Manticore::Client.new }
4
5
 
@@ -29,7 +30,7 @@ describe Manticore::Client do
29
30
  stub.async.get(local_server)
30
31
  stub.async.get(local_server)
31
32
 
32
- expect( client.execute!.map(&:class) ).to eq [Manticore::StubbedResponse, Manticore::StubbedResponse]
33
+ expect(client.execute!.map(&:class)).to eq [Manticore::StubbedResponse, Manticore::StubbedResponse]
33
34
  end
34
35
  end
35
36
  end
@@ -58,7 +59,7 @@ describe Manticore::Client do
58
59
  end
59
60
 
60
61
  it "can chain handlers" do
61
- client.async.get("http://localhost:55441/").on_success {|r| r.code }
62
+ client.async.get("http://localhost:55441/").on_success { |r| r.code }
62
63
  expect(client.execute!.map(&:callback_result)).to eq [200]
63
64
  end
64
65
  end
@@ -95,4 +96,4 @@ describe Manticore::Client do
95
96
  end
96
97
  end
97
98
  end
98
- end
99
+ end
@@ -1,11 +1,10 @@
1
1
  # encoding: utf-8
2
- require 'spec_helper'
2
+ require "spec_helper"
3
3
 
4
- java_import 'org.apache.http.entity.mime.MultipartEntityBuilder'
5
- java_import 'org.apache.http.entity.ContentType'
4
+ java_import "org.apache.http.entity.mime.MultipartEntityBuilder"
5
+ java_import "org.apache.http.entity.ContentType"
6
6
 
7
7
  describe Manticore::Client do
8
-
9
8
  let(:client) { Manticore::Client.new }
10
9
 
11
10
  it "fetches a URL and return a response" do
@@ -52,14 +51,19 @@ describe Manticore::Client do
52
51
  expect(j["uri"]["port"]).to eq 55441
53
52
  end
54
53
 
54
+ it "automatically decodes application/JSON as UTF-8" do
55
+ j = JSON.parse client.get(local_server("/json_utf8")).body
56
+ expect(j["last_name"]).to eq "Töger"
57
+ end
58
+
55
59
  context "via an authenticated proxy" do
56
60
  let(:proxy) { "http://localhost:55442" }
57
61
  let(:auth) { nil }
58
- let(:req) { client.get(local_server("/authproxy"), proxy: proxy, auth: auth) }
59
62
  let(:j) { JSON.parse req.body }
60
63
 
61
64
  context "with authentication as a hash" do
62
65
  let(:auth) { {user: "user", pass: "pass"} }
66
+ let(:req) { client.get(local_server("/authproxy"), proxy: proxy, auth: auth) }
63
67
 
64
68
  it "proxies" do
65
69
  expect(j["server_port"]).to eq 55442
@@ -69,7 +73,8 @@ describe Manticore::Client do
69
73
  end
70
74
 
71
75
  context "with authentication as a string" do
72
- let(:proxy) { "http://user:pass@localhost:55442"}
76
+ let(:proxy) { "http://user:pass@localhost:55442" }
77
+ let(:req) { client.get(local_server("/authproxy"), proxy: proxy) }
73
78
 
74
79
  it "proxies" do
75
80
  expect(j["server_port"]).to eq 55442
@@ -78,8 +83,6 @@ describe Manticore::Client do
78
83
  end
79
84
  end
80
85
  end
81
- it "proxies with authentication as a hash" do
82
- end
83
86
 
84
87
  describe "with a custom user agent" do
85
88
  let(:client) { Manticore::Client.new user_agent: "test-agent/1.0" }
@@ -109,17 +112,21 @@ describe Manticore::Client do
109
112
  end
110
113
  end
111
114
 
112
- describe 'ssl settings' do
113
- describe 'verify' do
114
- context 'default' do
115
+ describe "ssl settings" do
116
+ describe "verify" do
117
+ context "default" do
115
118
  let(:client) { Manticore::Client.new }
116
119
 
117
120
  it "breaks on SSL validation errors" do
118
121
  expect { client.get("https://localhost:55444/").call }.to raise_exception(Manticore::ClientProtocolException)
119
122
  end
123
+
124
+ it "breaks on SSL expiry errors" do
125
+ expect { client.get("https://localhost:55446/").call }.to raise_exception(Manticore::ClientProtocolException)
126
+ end
120
127
  end
121
128
 
122
- context 'when on and no trust store is given' do
129
+ context "when on and no trust store is given" do
123
130
  let(:client) { Manticore::Client.new :ssl => {:verify => :strict} }
124
131
 
125
132
  it "breaks on SSL validation errors" do
@@ -127,7 +134,7 @@ describe Manticore::Client do
127
134
  end
128
135
  end
129
136
 
130
- context 'when on and custom trust store is given' do
137
+ context "when on and custom trust store is given" do
131
138
  let(:client) { Manticore::Client.new :ssl => {verify: :strict, truststore: File.expand_path("../../ssl/truststore.jks", __FILE__), truststore_password: "test123"} }
132
139
 
133
140
  it "verifies the request and succeed" do
@@ -143,7 +150,7 @@ describe Manticore::Client do
143
150
  end
144
151
  end
145
152
 
146
- context 'when on and custom trust store is given with the wrong password' do
153
+ context "when on and custom trust store is given with the wrong password" do
147
154
  let(:client) { Manticore::Client.new :ssl => {verify: :strict, truststore: File.expand_path("../../ssl/truststore.jks", __FILE__), truststore_password: "wrongpass"} }
148
155
 
149
156
  it "fails to load the keystore" do
@@ -151,22 +158,24 @@ describe Manticore::Client do
151
158
  end
152
159
  end
153
160
 
154
- context 'when ca_file is given' do
155
- let(:client) { Manticore::Client.new :ssl => {verify: :strict, ca_file: File.expand_path("../../ssl/root-ca.crt", __FILE__) } }
161
+ context "when ca_file is given" do
162
+ let(:client) { Manticore::Client.new :ssl => {verify: :strict, ca_file: File.expand_path("../../ssl/root-ca.crt", __FILE__)} }
156
163
 
157
164
  it "verifies the request and succeed" do
158
165
  expect { client.get("https://localhost:55444/").body }.to_not raise_exception
159
166
  end
160
167
  end
161
168
 
162
- context 'when client_cert and client_key are given' do
163
- let(:client) { Manticore::Client.new(
164
- :ssl => {
165
- verify: :strict,
166
- ca_file: File.expand_path("../../ssl/root-ca.crt", __FILE__),
167
- client_cert: File.expand_path("../../ssl/client.crt", __FILE__),
168
- client_key: File.expand_path("../../ssl/client.key", __FILE__)
169
- })
169
+ context "when client_cert and client_key are given" do
170
+ let(:client) {
171
+ Manticore::Client.new(
172
+ :ssl => {
173
+ verify: :strict,
174
+ ca_file: File.expand_path("../../ssl/root-ca.crt", __FILE__),
175
+ client_cert: File.expand_path("../../ssl/client.crt", __FILE__),
176
+ client_key: File.expand_path("../../ssl/client.key", __FILE__),
177
+ },
178
+ )
170
179
  }
171
180
 
172
181
  it "successfully auths requests" do
@@ -174,12 +183,16 @@ describe Manticore::Client do
174
183
  end
175
184
  end
176
185
 
177
- context 'when off' do
178
- let(:client) { Manticore::Client.new :ssl => {:verify => :none} }
186
+ context "when off" do
187
+ let(:client) { Manticore::Client.new :ssl => {:verify => :disable} }
179
188
 
180
189
  it "does not break on SSL validation errors" do
181
190
  expect { client.get("https://localhost:55444/").body }.to_not raise_exception
182
191
  end
192
+
193
+ it "does not break on expired SSL certificates" do
194
+ expect { client.get("https://localhost:55446/").body }.to_not raise_exception
195
+ end
183
196
  end
184
197
 
185
198
  context "against a server that verifies clients" do
@@ -190,7 +203,7 @@ describe Manticore::Client do
190
203
  truststore_password: "test123",
191
204
  keystore: File.expand_path("../../ssl/client.p12", __FILE__),
192
205
  keystore_password: "test123",
193
- verify: :strict
206
+ verify: :strict,
194
207
  }
195
208
  Manticore::Client.new :ssl => options
196
209
  }
@@ -205,7 +218,7 @@ describe Manticore::Client do
205
218
  options = {
206
219
  truststore: File.expand_path("../../ssl/truststore.jks", __FILE__),
207
220
  truststore_password: "test123",
208
- verify: :strict
221
+ verify: :strict,
209
222
  }
210
223
  Manticore::Client.new :ssl => options
211
224
  }
@@ -241,40 +254,39 @@ describe Manticore::Client do
241
254
  expect(subject).to receive(:call).once.and_call_original
242
255
  end
243
256
 
244
- specify { expect { subject.body }.to change { subject.called? } }
245
- specify { expect { subject.headers }.to change { subject.called? } }
257
+ specify { expect { subject.body }.to change { subject.called? } }
258
+ specify { expect { subject.headers }.to change { subject.called? } }
246
259
  specify { expect { subject.final_url }.to change { subject.called? } }
247
- specify { expect { subject.code }.to change { subject.called? } }
248
- specify { expect { subject.length }.to change { subject.called? } }
249
- specify { expect { subject.cookies }.to change { subject.called? } }
260
+ specify { expect { subject.code }.to change { subject.called? } }
261
+ specify { expect { subject.length }.to change { subject.called? } }
262
+ specify { expect { subject.cookies }.to change { subject.called? } }
250
263
  end
251
264
 
252
265
  it "automatically calls synchronous requests that pass a handler block" do
253
- req = client.get(local_server) {|r| }
266
+ req = client.get(local_server) { |r| }
254
267
  expect(req).to be_called
255
268
  end
256
269
 
257
270
  it "does not call asynchronous requests even if a block is passed" do
258
- req = client.async.get(local_server) {|r| }
271
+ req = client.async.get(local_server) { |r| }
259
272
  expect(req).to_not be_called
260
273
  end
261
274
 
262
275
  it "does not call asynchronous requests when on_success is passed" do
263
- req = client.async.get(local_server).on_success {|r| }
276
+ req = client.async.get(local_server).on_success { |r| }
264
277
  expect(req).to_not be_called
265
278
  end
266
279
 
267
280
  it "calls async requests on client execution" do
268
- req = client.async.get(local_server).on_success {|r| }
281
+ req = client.async.get(local_server).on_success { |r| }
269
282
  expect { client.execute! }.to change { req.called? }.from(false).to(true)
270
283
  end
271
284
 
272
-
273
285
  describe "with a bad port number" do
274
286
  it "returns a Manticore::InvalidArgumentException" do
275
287
  failure = nil
276
288
  client.async.get(local_server("/", 65536)).
277
- on_failure {|f| failure = f }
289
+ on_failure { |f| failure = f }
278
290
  client.execute!
279
291
  expect(failure).to be_a(Manticore::InvalidArgumentException)
280
292
  end
@@ -286,7 +298,7 @@ describe Manticore::Client do
286
298
  # I'm not crazy about reaching into the client via instance_variable_get here, but it works.
287
299
  expect(client.instance_variable_get("@client")).to receive(:execute).and_raise(StandardError.new("Uh oh"))
288
300
  client.async.get(local_server).
289
- on_failure {|f| failure = f }
301
+ on_failure { |f| failure = f }
290
302
  client.execute!
291
303
  expect(failure).to be_a(StandardError)
292
304
  end
@@ -479,7 +491,7 @@ describe Manticore::Client do
479
491
 
480
492
  it "sends an arbitrary entity" do
481
493
  f = open(File.expand_path(File.join(__FILE__, "..", "..", "spec_helper.rb")), "r").to_inputstream
482
- multipart_entity = MultipartEntityBuilder.create.add_text_body("foo", "bar").add_binary_body("whatever", f , ContentType::TEXT_PLAIN, __FILE__)
494
+ multipart_entity = MultipartEntityBuilder.create.add_text_body("foo", "bar").add_binary_body("whatever", f, ContentType::TEXT_PLAIN, __FILE__)
483
495
  response = client.post(local_server, entity: multipart_entity.build)
484
496
  expect(response.body).to match "RSpec.configure"
485
497
  end
@@ -550,8 +562,8 @@ describe Manticore::Client do
550
562
  futures = [55441, 55442].map do |port|
551
563
  client.async.get("http://localhost:#{port}/?sleep=1").
552
564
  on_success do |response|
553
- Time.now.to_f
554
- end
565
+ Time.now.to_f
566
+ end
555
567
  end
556
568
 
557
569
  client.execute!
@@ -562,7 +574,7 @@ describe Manticore::Client do
562
574
  it "returns the results of the handler blocks" do
563
575
  [55441, 55442].each do |port|
564
576
  client.async.get("http://localhost:#{port}/").
565
- on_success {|response, request| "Result" }
577
+ on_success { |response, request| "Result" }
566
578
  end
567
579
 
568
580
  expect(client.execute!.map(&:callback_result)).to eq ["Result", "Result"]
@@ -572,7 +584,7 @@ describe Manticore::Client do
572
584
  describe "#clear_pending" do
573
585
  it "removes pending requests" do
574
586
  ran = false
575
- client.async.get("http://google.com").on_success {|r| ran = true }
587
+ client.async.get("http://google.com").on_success { |r| ran = true }
576
588
  client.clear_pending
577
589
  expect(client.execute!).to be_empty
578
590
  expect(ran).to be false
@@ -603,25 +615,25 @@ describe Manticore::Client do
603
615
  end
604
616
  end
605
617
 
606
- context 'stubbing' do
618
+ context "stubbing" do
607
619
  it "only the provided URLs" do
608
620
  client.stub local_server, body: "body"
609
- client.async.get(local_server).on_success {|r| expect(r).to be_a Manticore::StubbedResponse }
610
- client.async.get(local_server("/other")).on_success {|r| expect(r).to_not be_a Manticore::StubbedResponse }
621
+ client.async.get(local_server).on_success { |r| expect(r).to be_a Manticore::StubbedResponse }
622
+ client.async.get(local_server("/other")).on_success { |r| expect(r).to_not be_a Manticore::StubbedResponse }
611
623
  client.execute!
612
624
  end
613
625
 
614
626
  it "by regex matching" do
615
627
  client.stub %r{#{local_server("/foo")}}, body: "body"
616
- client.async.get(local_server("/foo")).on_success {|r| expect(r).to be_a Manticore::StubbedResponse }
617
- client.async.get(local_server("/bar")).on_success {|r| expect(r).to_not be_a Manticore::StubbedResponse }
628
+ client.async.get(local_server("/foo")).on_success { |r| expect(r).to be_a Manticore::StubbedResponse }
629
+ client.async.get(local_server("/bar")).on_success { |r| expect(r).to_not be_a Manticore::StubbedResponse }
618
630
  client.execute!
619
631
  end
620
632
 
621
633
  it "strictly matches string stubs" do
622
634
  client.stub local_server("/foo"), body: "body"
623
- client.async.get(local_server("/foo")).on_success {|r| expect(r).to be_a Manticore::StubbedResponse }
624
- client.async.get(local_server("/other")).on_success {|r| expect(r).to_not be_a Manticore::StubbedResponse }
635
+ client.async.get(local_server("/foo")).on_success { |r| expect(r).to be_a Manticore::StubbedResponse }
636
+ client.async.get(local_server("/other")).on_success { |r| expect(r).to_not be_a Manticore::StubbedResponse }
625
637
  client.execute!
626
638
  end
627
639
 
@@ -653,10 +665,9 @@ describe Manticore::Client do
653
665
  describe "keepalive" do
654
666
  let(:url) { "http://www.facebook.com/" }
655
667
 
656
-
657
668
  context "with keepalive" do
658
669
  it "adds the Connection: Keep-Alive header for http/1.0" do
659
- expect( client.get(url).request["Connection"] ).to eq "Keep-Alive"
670
+ expect(client.get(url).request["Connection"]).to eq "Keep-Alive"
660
671
  end
661
672
 
662
673
  let(:client) { Manticore::Client.new keepalive: true, pool_max: 1 }
@@ -674,7 +685,7 @@ describe Manticore::Client do
674
685
  let(:client) { Manticore::Client.new keepalive: false, pool_max: 1 }
675
686
 
676
687
  it "does not add the Connection: Keep-Alive header for http/1.0" do
677
- expect( client.get(url).request["Connection"] ).to be_nil
688
+ expect(client.get(url).request["Connection"]).to be_nil
678
689
  end
679
690
 
680
691
  it "closes the connection after a request" do
@@ -689,19 +700,33 @@ describe Manticore::Client do
689
700
  end
690
701
 
691
702
  context "with a misbehaving endpoint" do
703
+ let(:port) do
704
+ p = 4000
705
+ server = nil
706
+ begin
707
+ server = TCPServer.new p
708
+ rescue Errno::EADDRINUSE
709
+ p += 1
710
+ retry
711
+ ensure
712
+ server.close
713
+ end
714
+ p
715
+ end
716
+
692
717
  before do
693
- @socket = TCPServer.new 4567
718
+ @socket = TCPServer.new port
694
719
  @server = Thread.new do
695
720
  loop do
696
- client = @socket.accept
697
721
  begin
722
+ client = @socket.accept
698
723
  client.puts([
699
724
  "HTTP/1.1 200 OK",
700
725
  "Keep-Alive: timeout=3000",
701
726
  "Connection: Keep-Alive",
702
727
  "Content-Length: 6",
703
728
  "",
704
- "Hello!"
729
+ "Hello!",
705
730
  ].join("\n"))
706
731
  client.close
707
732
  rescue IOError => e
@@ -716,8 +741,8 @@ describe Manticore::Client do
716
741
  # The first time, reply with keepalive, then close the connection
717
742
  # The second connection should succeed
718
743
 
719
- request1 = client.get("http://localhost:4567/")
720
- request2 = client.get("http://localhost:4567/")
744
+ request1 = client.get("http://localhost:#{port}/")
745
+ request2 = client.get("http://localhost:#{port}/")
721
746
  expect { request1.call }.to_not raise_exception
722
747
  expect { request2.call }.to_not raise_exception
723
748
 
@@ -731,8 +756,8 @@ describe Manticore::Client do
731
756
  it "retries 0 times and fail on the second request" do
732
757
  # The first time, reply with keepalive, then close the connection
733
758
  # The second connection should succeed
734
- expect { client.get("http://localhost:4567/").call }.to_not raise_exception
735
- expect { client.get("http://localhost:4567/").call }.to raise_exception(Manticore::SocketException)
759
+ expect { client.get("http://localhost:#{port}/").call }.to_not raise_exception
760
+ expect { client.get("http://localhost:#{port}/").call }.to raise_exception(Manticore::SocketException)
736
761
  end
737
762
  end
738
763
 
@@ -742,8 +767,8 @@ describe Manticore::Client do
742
767
  it "succeeds without any retries" do
743
768
  # The first time, reply with keepalive, then close the connection
744
769
  # The second connection should succeed
745
- request1 = client.get("http://localhost:4567/")
746
- request2 = client.get("http://localhost:4567/")
770
+ request1 = client.get("http://localhost:#{port}/")
771
+ request2 = client.get("http://localhost:#{port}/")
747
772
  expect { request1.call }.to_not raise_exception
748
773
  expect { request2.call }.to_not raise_exception
749
774
 
@@ -815,15 +840,15 @@ describe Manticore::Client do
815
840
 
816
841
  describe "background" do
817
842
  it "returns a response" do
818
- expect( client.background.get(local_server) ).to be_a Manticore::Response
843
+ expect(client.background.get(local_server)).to be_a Manticore::Response
819
844
  end
820
845
 
821
846
  it "has the background flag set" do
822
- expect( client.background.get(local_server).background ).to eq true
847
+ expect(client.background.get(local_server).background).to eq true
823
848
  end
824
849
 
825
850
  it "returns a future when called" do
826
- expect( client.background.get(local_server).call ).to be_a Java::JavaUtilConcurrent::FutureTask
851
+ expect(client.background.get(local_server).call).to be_a Java::JavaUtilConcurrent::FutureTask
827
852
  end
828
853
 
829
854
  it "evaluates immediately when given a block" do
@@ -842,7 +867,7 @@ describe Manticore::Client do
842
867
  java_import "java.util.concurrent.TimeUnit"
843
868
  host = URI.parse(uri).host
844
869
  pool = client.instance_variable_get("@pool")
845
- req = pool.requestConnection(Java::OrgApacheHttpConnRouting::HttpRoute.new( Java::OrgApacheHttp::HttpHost.new(host) ), nil)
870
+ req = pool.requestConnection(Java::OrgApacheHttpConnRouting::HttpRoute.new(Java::OrgApacheHttp::HttpHost.new(host)), nil)
846
871
  conn = req.get(3, TimeUnit::SECONDS)
847
872
  begin
848
873
  yield conn