httparty 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of httparty might be problematic. Click here for more details.

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