-
Notifications
You must be signed in to change notification settings - Fork 965
/
connection_adapter_spec.rb
495 lines (408 loc) · 15.8 KB
/
connection_adapter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
RSpec.describe HTTParty::ConnectionAdapter do
describe "initialization" do
let(:uri) { URI 'http://www.google.com' }
it "takes a URI as input" do
HTTParty::ConnectionAdapter.new(uri)
end
it "raises an ArgumentError if the uri is nil" do
expect { HTTParty::ConnectionAdapter.new(nil) }.to raise_error ArgumentError
end
it "raises an ArgumentError if the uri is a String" do
expect { HTTParty::ConnectionAdapter.new('http://www.google.com') }.to raise_error ArgumentError
end
it "sets the uri" do
adapter = HTTParty::ConnectionAdapter.new(uri)
expect(adapter.uri).to be uri
end
it "also accepts an optional options hash" do
HTTParty::ConnectionAdapter.new(uri, {})
end
it "sets the options" do
options = {foo: :bar}
adapter = HTTParty::ConnectionAdapter.new(uri, options)
expect(adapter.options.keys).to include(:verify, :verify_peer, :foo)
end
end
describe ".call" do
it "generates an HTTParty::ConnectionAdapter instance with the given uri and options" do
expect(HTTParty::ConnectionAdapter).to receive(:new).with(@uri, @options).and_return(double(connection: nil))
HTTParty::ConnectionAdapter.call(@uri, @options)
end
it "calls #connection on the connection adapter" do
adapter = double('Adapter')
connection = double('Connection')
expect(adapter).to receive(:connection).and_return(connection)
allow(HTTParty::ConnectionAdapter).to receive_messages(new: adapter)
expect(HTTParty::ConnectionAdapter.call(@uri, @options)).to be connection
end
end
describe '#connection' do
let(:uri) { URI 'http://www.google.com' }
let(:options) { Hash.new }
let(:adapter) { HTTParty::ConnectionAdapter.new(uri, options) }
describe "the resulting connection" do
subject { adapter.connection }
it { is_expected.to be_an_instance_of Net::HTTP }
context "using port 80" do
let(:uri) { URI 'http://foobar.com' }
it { is_expected.not_to use_ssl }
end
context "when dealing with ssl" do
let(:uri) { URI 'https://foobar.com' }
context "uses the system cert_store, by default" do
let!(:system_cert_store) do
system_cert_store = double('default_cert_store')
expect(system_cert_store).to receive(:set_default_paths)
expect(OpenSSL::X509::Store).to receive(:new).and_return(system_cert_store)
system_cert_store
end
it { is_expected.to use_cert_store(system_cert_store) }
end
context "should use the specified cert store, when one is given" do
let(:custom_cert_store) { double('custom_cert_store') }
let(:options) { {cert_store: custom_cert_store} }
it { is_expected.to use_cert_store(custom_cert_store) }
end
context "using port 443 for ssl" do
let(:uri) { URI 'https://api.foo.com/v1:443' }
it { is_expected.to use_ssl }
end
context "https scheme with default port" do
it { is_expected.to use_ssl }
end
context "https scheme with non-standard port" do
let(:uri) { URI 'https://foobar.com:123456' }
it { is_expected.to use_ssl }
end
context "when ssl version is set" do
let(:options) { {ssl_version: :TLSv1} }
it "sets ssl version" do
expect(subject.ssl_version).to eq(:TLSv1)
end
end if RUBY_VERSION > '1.9'
end
context "when dealing with IPv6" do
let(:uri) { URI 'http://[fd00::1]' }
it "strips brackets from the address" do
expect(subject.address).to eq('fd00::1')
end
end
context "specifying ciphers" do
let(:options) { {ciphers: 'RC4-SHA' } }
it "should set the ciphers on the connection" do
expect(subject.ciphers).to eq('RC4-SHA')
end
end if RUBY_VERSION > '1.9'
context "when timeout is not set" do
it "doesn't set the timeout" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false
)
expect(http).not_to receive(:open_timeout=)
expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
context "when setting timeout" do
context "to 5 seconds" do
let(:options) { {timeout: 5} }
describe '#open_timeout' do
subject { super().open_timeout }
it { is_expected.to eq(5) }
end
describe '#read_timeout' do
subject { super().read_timeout }
it { is_expected.to eq(5) }
end
end
context "and timeout is a string" do
let(:options) { {timeout: "five seconds"} }
it "doesn't set the timeout" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false
)
expect(http).not_to receive(:open_timeout=)
expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
end
context "when timeout is not set and read_timeout is set to 6 seconds" do
let(:options) { {read_timeout: 6} }
describe '#read_timeout' do
subject { super().read_timeout }
it { is_expected.to eq(6) }
end
it "should not set the open_timeout" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0
)
expect(http).not_to receive(:open_timeout=)
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
context "when timeout is set and read_timeout is set to 6 seconds" do
let(:options) { {timeout: 5, read_timeout: 6} }
describe '#open_timeout' do
subject { super().open_timeout }
it { is_expected.to eq(5) }
end
describe '#read_timeout' do
subject { super().read_timeout }
it { is_expected.to eq(6) }
end
it "should override the timeout option" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0,
:open_timeout= => 0
)
expect(http).to receive(:open_timeout=)
expect(http).to receive(:read_timeout=).twice
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
context "when timeout is not set and open_timeout is set to 7 seconds" do
let(:options) { {open_timeout: 7} }
describe '#open_timeout' do
subject { super().open_timeout }
it { is_expected.to eq(7) }
end
it "should not set the read_timeout" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:open_timeout= => 0
)
expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
context "when timeout is set and open_timeout is set to 7 seconds" do
let(:options) { {timeout: 5, open_timeout: 7} }
describe '#open_timeout' do
subject { super().open_timeout }
it { is_expected.to eq(7) }
end
describe '#read_timeout' do
subject { super().read_timeout }
it { is_expected.to eq(5) }
end
it "should override the timeout option" do
http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0,
:open_timeout= => 0
)
expect(http).to receive(:open_timeout=).twice
expect(http).to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http)
adapter.connection
end
end
context "when debug_output" do
let(:http) { Net::HTTP.new(uri) }
before do
allow(Net::HTTP).to receive_messages(new: http)
end
context "is set to $stderr" do
let(:options) { {debug_output: $stderr} }
it "has debug output set" do
expect(http).to receive(:set_debug_output).with($stderr)
adapter.connection
end
end
context "is not provided" do
it "does not set_debug_output" do
expect(http).not_to receive(:set_debug_output)
adapter.connection
end
end
end
context 'when providing proxy address and port' do
let(:options) { {http_proxyaddr: '1.2.3.4', http_proxyport: 8080} }
it { is_expected.to be_a_proxy }
describe '#proxy_address' do
subject { super().proxy_address }
it { is_expected.to eq('1.2.3.4') }
end
describe '#proxy_port' do
subject { super().proxy_port }
it { is_expected.to eq(8080) }
end
context 'as well as proxy user and password' do
let(:options) do
{http_proxyaddr: '1.2.3.4', http_proxyport: 8080,
http_proxyuser: 'user', http_proxypass: 'pass'}
end
describe '#proxy_user' do
subject { super().proxy_user }
it { is_expected.to eq('user') }
end
describe '#proxy_pass' do
subject { super().proxy_pass }
it { is_expected.to eq('pass') }
end
end
end
context 'when providing nil as proxy address' do
let(:uri) { URI 'http://noproxytest.com' }
let(:options) { {http_proxyaddr: nil} }
it { is_expected.not_to be_a_proxy }
it "does pass nil proxy parameters to the connection, this forces to not use a proxy" do
http = Net::HTTP.new("noproxytest.com")
expect(Net::HTTP).to receive(:new).once.with("noproxytest.com", 80, nil, nil, nil, nil).and_return(http)
adapter.connection
end
end
context 'when not providing a proxy address' do
let(:uri) { URI 'http://proxytest.com' }
it "does not pass any proxy parameters to the connection" do
http = Net::HTTP.new("proxytest.com")
expect(Net::HTTP).to receive(:new).once.with("proxytest.com", 80).and_return(http)
adapter.connection
end
end
context 'when providing a local bind address and port' do
let(:options) { {local_host: "127.0.0.1", local_port: 12345 } }
describe '#local_host' do
subject { super().local_host }
it { is_expected.to eq('127.0.0.1') }
end
describe '#local_port' do
subject { super().local_port }
it { is_expected.to eq(12345) }
end
end if RUBY_VERSION >= '2.0'
context "when providing PEM certificates" do
let(:pem) { :pem_contents }
let(:options) { {pem: pem, pem_password: "password"} }
context "when scheme is https" do
let(:uri) { URI 'https://google.com' }
let(:cert) { double("OpenSSL::X509::Certificate") }
let(:key) { double("OpenSSL::PKey::RSA") }
before do
expect(OpenSSL::X509::Certificate).to receive(:new).with(pem).and_return(cert)
expect(OpenSSL::PKey::RSA).to receive(:new).with(pem, "password").and_return(key)
end
it "uses the provided PEM certificate" do
expect(subject.cert).to eq(cert)
expect(subject.key).to eq(key)
end
it "will verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
end
context "when options include verify=false" do
let(:options) { {pem: pem, pem_password: "password", verify: false} }
it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end
end
context "when options include verify_peer=false" do
let(:options) { {pem: pem, pem_password: "password", verify_peer: false} }
it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end
end
end
context "when scheme is not https" do
let(:uri) { URI 'http://google.com' }
let(:http) { Net::HTTP.new(uri) }
before do
allow(Net::HTTP).to receive_messages(new: http)
expect(OpenSSL::X509::Certificate).not_to receive(:new).with(pem)
expect(OpenSSL::PKey::RSA).not_to receive(:new).with(pem, "password")
expect(http).not_to receive(:cert=)
expect(http).not_to receive(:key=)
end
it "has no PEM certificate " do
expect(subject.cert).to be_nil
expect(subject.key).to be_nil
end
end
end
context "when providing PKCS12 certificates" do
let(:p12) { :p12_contents }
let(:options) { {p12: p12, p12_password: "password"} }
context "when scheme is https" do
let(:uri) { URI 'https://google.com' }
let(:pkcs12) { double("OpenSSL::PKCS12", certificate: cert, key: key) }
let(:cert) { double("OpenSSL::X509::Certificate") }
let(:key) { double("OpenSSL::PKey::RSA") }
before do
expect(OpenSSL::PKCS12).to receive(:new).with(p12, "password").and_return(pkcs12)
end
it "uses the provided P12 certificate " do
expect(subject.cert).to eq(cert)
expect(subject.key).to eq(key)
end
it "will verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
end
context "when options include verify=false" do
let(:options) { {p12: p12, p12_password: "password", verify: false} }
it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end
end
context "when options include verify_peer=false" do
let(:options) { {p12: p12, p12_password: "password", verify_peer: false} }
it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end
end
end
context "when scheme is not https" do
let(:uri) { URI 'http://google.com' }
let(:http) { Net::HTTP.new(uri) }
before do
allow(Net::HTTP).to receive_messages(new: http)
expect(OpenSSL::PKCS12).not_to receive(:new).with(p12, "password")
expect(http).not_to receive(:cert=)
expect(http).not_to receive(:key=)
end
it "has no PKCS12 certificate " do
expect(subject.cert).to be_nil
expect(subject.key).to be_nil
end
end
end
context "when uri port is not defined" do
context "falls back to 80 port on http" do
let(:uri) { URI 'http://foobar.com' }
before { allow(uri).to receive(:port).and_return(nil) }
it { expect(subject.port).to be 80 }
end
context "falls back to 443 port on https" do
let(:uri) { URI 'https://foobar.com' }
before { allow(uri).to receive(:port).and_return(nil) }
it { expect(subject.port).to be 443 }
end
end
end
end
end