httpserious 0.13.5.lstoll1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rubocop.yml +92 -0
  4. data/.rubocop_todo.yml +124 -0
  5. data/.simplecov +1 -0
  6. data/.travis.yml +7 -0
  7. data/CONTRIBUTING.md +23 -0
  8. data/Gemfile +19 -0
  9. data/Guardfile +16 -0
  10. data/History +370 -0
  11. data/MIT-LICENSE +20 -0
  12. data/README.md +78 -0
  13. data/Rakefile +10 -0
  14. data/bin/httparty +116 -0
  15. data/cucumber.yml +1 -0
  16. data/examples/README.md +67 -0
  17. data/examples/aaws.rb +32 -0
  18. data/examples/basic.rb +28 -0
  19. data/examples/crack.rb +19 -0
  20. data/examples/custom_parsers.rb +64 -0
  21. data/examples/delicious.rb +37 -0
  22. data/examples/google.rb +16 -0
  23. data/examples/headers_and_user_agents.rb +6 -0
  24. data/examples/logging.rb +36 -0
  25. data/examples/nokogiri_html_parser.rb +19 -0
  26. data/examples/rescue_json.rb +17 -0
  27. data/examples/rubyurl.rb +14 -0
  28. data/examples/stackexchange.rb +24 -0
  29. data/examples/tripit_sign_in.rb +33 -0
  30. data/examples/twitter.rb +31 -0
  31. data/examples/whoismyrep.rb +10 -0
  32. data/features/basic_authentication.feature +20 -0
  33. data/features/command_line.feature +90 -0
  34. data/features/deals_with_http_error_codes.feature +26 -0
  35. data/features/digest_authentication.feature +20 -0
  36. data/features/handles_compressed_responses.feature +27 -0
  37. data/features/handles_multiple_formats.feature +57 -0
  38. data/features/steps/env.rb +27 -0
  39. data/features/steps/httparty_response_steps.rb +52 -0
  40. data/features/steps/httparty_steps.rb +43 -0
  41. data/features/steps/mongrel_helper.rb +94 -0
  42. data/features/steps/remote_service_steps.rb +86 -0
  43. data/features/supports_read_timeout_option.feature +13 -0
  44. data/features/supports_redirection.feature +22 -0
  45. data/features/supports_timeout_option.feature +13 -0
  46. data/httparty.gemspec +28 -0
  47. data/httpserious.gemspec +25 -0
  48. data/lib/httparty.rb +612 -0
  49. data/lib/httparty/connection_adapter.rb +190 -0
  50. data/lib/httparty/cookie_hash.rb +21 -0
  51. data/lib/httparty/exceptions.rb +29 -0
  52. data/lib/httparty/hash_conversions.rb +49 -0
  53. data/lib/httparty/logger/apache_formatter.rb +22 -0
  54. data/lib/httparty/logger/curl_formatter.rb +48 -0
  55. data/lib/httparty/logger/logger.rb +26 -0
  56. data/lib/httparty/module_inheritable_attributes.rb +56 -0
  57. data/lib/httparty/net_digest_auth.rb +117 -0
  58. data/lib/httparty/parser.rb +141 -0
  59. data/lib/httparty/request.rb +361 -0
  60. data/lib/httparty/response.rb +77 -0
  61. data/lib/httparty/response/headers.rb +31 -0
  62. data/lib/httparty/version.rb +3 -0
  63. data/lib/httpserious.rb +1 -0
  64. data/script/release +42 -0
  65. data/spec/fixtures/delicious.xml +23 -0
  66. data/spec/fixtures/empty.xml +0 -0
  67. data/spec/fixtures/google.html +3 -0
  68. data/spec/fixtures/ssl/generate.sh +29 -0
  69. data/spec/fixtures/ssl/generated/1fe462c2.0 +16 -0
  70. data/spec/fixtures/ssl/generated/bogushost.crt +13 -0
  71. data/spec/fixtures/ssl/generated/ca.crt +16 -0
  72. data/spec/fixtures/ssl/generated/ca.key +15 -0
  73. data/spec/fixtures/ssl/generated/selfsigned.crt +14 -0
  74. data/spec/fixtures/ssl/generated/server.crt +13 -0
  75. data/spec/fixtures/ssl/generated/server.key +15 -0
  76. data/spec/fixtures/ssl/openssl-exts.cnf +9 -0
  77. data/spec/fixtures/twitter.csv +2 -0
  78. data/spec/fixtures/twitter.json +1 -0
  79. data/spec/fixtures/twitter.xml +403 -0
  80. data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
  81. data/spec/httparty/connection_adapter_spec.rb +468 -0
  82. data/spec/httparty/cookie_hash_spec.rb +83 -0
  83. data/spec/httparty/exception_spec.rb +38 -0
  84. data/spec/httparty/hash_conversions_spec.rb +41 -0
  85. data/spec/httparty/logger/apache_formatter_spec.rb +41 -0
  86. data/spec/httparty/logger/curl_formatter_spec.rb +18 -0
  87. data/spec/httparty/logger/logger_spec.rb +38 -0
  88. data/spec/httparty/net_digest_auth_spec.rb +191 -0
  89. data/spec/httparty/parser_spec.rb +167 -0
  90. data/spec/httparty/request_spec.rb +872 -0
  91. data/spec/httparty/response_spec.rb +241 -0
  92. data/spec/httparty/ssl_spec.rb +74 -0
  93. data/spec/httparty_spec.rb +823 -0
  94. data/spec/spec_helper.rb +59 -0
  95. data/spec/support/ssl_test_helper.rb +47 -0
  96. data/spec/support/ssl_test_server.rb +80 -0
  97. data/spec/support/stub_response.rb +43 -0
  98. data/website/css/common.css +47 -0
  99. data/website/index.html +73 -0
  100. metadata +219 -0
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Entities total="0" results="0" page="1" page-size="25" href="https://s3-sandbox.parature.com/api/v1/5578/5633/Account" />
@@ -0,0 +1,468 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ RSpec.describe HTTParty::ConnectionAdapter do
4
+ describe "initialization" do
5
+ let(:uri) { URI 'http://www.google.com' }
6
+ it "takes a URI as input" do
7
+ HTTParty::ConnectionAdapter.new(uri)
8
+ end
9
+
10
+ it "raises an ArgumentError if the uri is nil" do
11
+ expect { HTTParty::ConnectionAdapter.new(nil) }.to raise_error ArgumentError
12
+ end
13
+
14
+ it "raises an ArgumentError if the uri is a String" do
15
+ expect { HTTParty::ConnectionAdapter.new('http://www.google.com') }.to raise_error ArgumentError
16
+ end
17
+
18
+ it "sets the uri" do
19
+ adapter = HTTParty::ConnectionAdapter.new(uri)
20
+ expect(adapter.uri).to be uri
21
+ end
22
+
23
+ it "also accepts an optional options hash" do
24
+ HTTParty::ConnectionAdapter.new(uri, {})
25
+ end
26
+
27
+ it "sets the options" do
28
+ options = {foo: :bar}
29
+ adapter = HTTParty::ConnectionAdapter.new(uri, options)
30
+ expect(adapter.options).to be options
31
+ end
32
+ end
33
+
34
+ describe ".call" do
35
+ it "generates an HTTParty::ConnectionAdapter instance with the given uri and options" do
36
+ expect(HTTParty::ConnectionAdapter).to receive(:new).with(@uri, @options).and_return(double(connection: nil))
37
+ HTTParty::ConnectionAdapter.call(@uri, @options)
38
+ end
39
+
40
+ it "calls #connection on the connection adapter" do
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
46
+ end
47
+ end
48
+
49
+ describe '#connection' do
50
+ let(:uri) { URI 'http://www.google.com' }
51
+ let(:options) { Hash.new }
52
+ let(:adapter) { HTTParty::ConnectionAdapter.new(uri, options) }
53
+
54
+ describe "the resulting connection" do
55
+ subject { adapter.connection }
56
+ it { is_expected.to be_an_instance_of Net::HTTP }
57
+
58
+ context "using port 80" do
59
+ let(:uri) { URI 'http://foobar.com' }
60
+ it { is_expected.not_to use_ssl }
61
+ end
62
+
63
+ context "when dealing with ssl" do
64
+ let(:uri) { URI 'https://foobar.com' }
65
+
66
+ context "uses the system cert_store, by default" do
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)
71
+ system_cert_store
72
+ end
73
+ it { is_expected.to use_cert_store(system_cert_store) }
74
+ end
75
+
76
+ context "should use the specified cert store, when one is given" do
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) }
80
+ end
81
+
82
+ context "using port 443 for ssl" do
83
+ let(:uri) { URI 'https://api.foo.com/v1:443' }
84
+ it { is_expected.to use_ssl }
85
+ end
86
+
87
+ context "https scheme with default port" do
88
+ it { is_expected.to use_ssl }
89
+ end
90
+
91
+ context "https scheme with non-standard port" do
92
+ let(:uri) { URI 'https://foobar.com:123456' }
93
+ it { is_expected.to use_ssl }
94
+ end
95
+
96
+ context "when ssl version is set" do
97
+ let(:options) { {ssl_version: :TLSv1} }
98
+
99
+ it "sets ssl version" do
100
+ expect(subject.ssl_version).to eq(:TLSv1)
101
+ end
102
+ end if RUBY_VERSION > '1.9'
103
+ end
104
+
105
+ context "when dealing with IPv6" do
106
+ let(:uri) { URI 'http://[fd00::1]' }
107
+
108
+ it "strips brackets from the address" do
109
+ expect(subject.address).to eq('fd00::1')
110
+ end
111
+ end
112
+
113
+ context "specifying ciphers" do
114
+ let(:options) { {ciphers: 'RC4-SHA' } }
115
+
116
+ it "should set the ciphers on the connection" do
117
+ expect(subject.ciphers).to eq('RC4-SHA')
118
+ end
119
+ end if RUBY_VERSION > '1.9'
120
+
121
+ context "when timeout is not set" do
122
+ it "doesn't set the timeout" do
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)
132
+
133
+ adapter.connection
134
+ end
135
+ end
136
+
137
+ context "when setting timeout" do
138
+ context "to 5 seconds" do
139
+ let(:options) { {timeout: 5} }
140
+
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
150
+ end
151
+
152
+ context "and timeout is a string" do
153
+ let(:options) { {timeout: "five seconds"} }
154
+
155
+ it "doesn't set the timeout" do
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)
165
+
166
+ adapter.connection
167
+ end
168
+ end
169
+ end
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
+
273
+ context "when debug_output" do
274
+ let(:http) { Net::HTTP.new(uri) }
275
+ before do
276
+ allow(Net::HTTP).to receive_messages(new: http)
277
+ end
278
+
279
+ context "is set to $stderr" do
280
+ let(:options) { {debug_output: $stderr} }
281
+ it "has debug output set" do
282
+ expect(http).to receive(:set_debug_output).with($stderr)
283
+ adapter.connection
284
+ end
285
+ end
286
+
287
+ context "is not provided" do
288
+ it "does not set_debug_output" do
289
+ expect(http).not_to receive(:set_debug_output)
290
+ adapter.connection
291
+ end
292
+ end
293
+ end
294
+
295
+ context 'when providing proxy address and port' do
296
+ let(:options) { {http_proxyaddr: '1.2.3.4', http_proxyport: 8080} }
297
+
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
309
+
310
+ context 'as well as proxy user and password' do
311
+ let(:options) do
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') }
319
+ end
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 not providing a proxy address' do
329
+ let(:uri) { URI 'http://proxytest.com' }
330
+
331
+ it "does not pass any proxy parameters to the connection" do
332
+ http = Net::HTTP.new("proxytest.com")
333
+ expect(Net::HTTP).to receive(:new).once.with("proxytest.com", 80).and_return(http)
334
+ adapter.connection
335
+ end
336
+ end
337
+
338
+ context 'when providing a local bind address and port' do
339
+ let(:options) { {local_host: "127.0.0.1", local_port: 12345 } }
340
+
341
+ describe '#local_host' do
342
+ subject { super().local_host }
343
+ it { is_expected.to eq('127.0.0.1') }
344
+ end
345
+
346
+ describe '#local_port' do
347
+ subject { super().local_port }
348
+ it { is_expected.to eq(12345) }
349
+ end
350
+ end if RUBY_VERSION >= '2.0'
351
+
352
+ context "when providing PEM certificates" do
353
+ let(:pem) { :pem_contents }
354
+ let(:options) { {pem: pem, pem_password: "password"} }
355
+
356
+ context "when scheme is https" do
357
+ let(:uri) { URI 'https://google.com' }
358
+ let(:cert) { double("OpenSSL::X509::Certificate") }
359
+ let(:key) { double("OpenSSL::PKey::RSA") }
360
+
361
+ before do
362
+ expect(OpenSSL::X509::Certificate).to receive(:new).with(pem).and_return(cert)
363
+ expect(OpenSSL::PKey::RSA).to receive(:new).with(pem, "password").and_return(key)
364
+ end
365
+
366
+ it "uses the provided PEM certificate" do
367
+ expect(subject.cert).to eq(cert)
368
+ expect(subject.key).to eq(key)
369
+ end
370
+
371
+ it "will verify the certificate" do
372
+ expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
373
+ end
374
+
375
+ context "when options include verify_peer=false" do
376
+ let(:options) { {pem: pem, pem_password: "password", verify_peer: false} }
377
+
378
+ it "should not verify the certificate" do
379
+ expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
380
+ end
381
+ end
382
+ end
383
+
384
+ context "when scheme is not https" do
385
+ let(:uri) { URI 'http://google.com' }
386
+ let(:http) { Net::HTTP.new(uri) }
387
+
388
+ before do
389
+ allow(Net::HTTP).to receive_messages(new: http)
390
+ expect(OpenSSL::X509::Certificate).not_to receive(:new).with(pem)
391
+ expect(OpenSSL::PKey::RSA).not_to receive(:new).with(pem, "password")
392
+ expect(http).not_to receive(:cert=)
393
+ expect(http).not_to receive(:key=)
394
+ end
395
+
396
+ it "has no PEM certificate " do
397
+ expect(subject.cert).to be_nil
398
+ expect(subject.key).to be_nil
399
+ end
400
+ end
401
+ end
402
+
403
+ context "when providing PKCS12 certificates" do
404
+ let(:p12) { :p12_contents }
405
+ let(:options) { {p12: p12, p12_password: "password"} }
406
+
407
+ context "when scheme is https" do
408
+ let(:uri) { URI 'https://google.com' }
409
+ let(:pkcs12) { double("OpenSSL::PKCS12", certificate: cert, key: key) }
410
+ let(:cert) { double("OpenSSL::X509::Certificate") }
411
+ let(:key) { double("OpenSSL::PKey::RSA") }
412
+
413
+ before do
414
+ expect(OpenSSL::PKCS12).to receive(:new).with(p12, "password").and_return(pkcs12)
415
+ end
416
+
417
+ it "uses the provided P12 certificate " do
418
+ expect(subject.cert).to eq(cert)
419
+ expect(subject.key).to eq(key)
420
+ end
421
+
422
+ it "will verify the certificate" do
423
+ expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
424
+ end
425
+
426
+ context "when options include verify_peer=false" do
427
+ let(:options) { {p12: p12, p12_password: "password", verify_peer: false} }
428
+
429
+ it "should not verify the certificate" do
430
+ expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
431
+ end
432
+ end
433
+ end
434
+
435
+ context "when scheme is not https" do
436
+ let(:uri) { URI 'http://google.com' }
437
+ let(:http) { Net::HTTP.new(uri) }
438
+
439
+ before do
440
+ allow(Net::HTTP).to receive_messages(new: http)
441
+ expect(OpenSSL::PKCS12).not_to receive(:new).with(p12, "password")
442
+ expect(http).not_to receive(:cert=)
443
+ expect(http).not_to receive(:key=)
444
+ end
445
+
446
+ it "has no PKCS12 certificate " do
447
+ expect(subject.cert).to be_nil
448
+ expect(subject.key).to be_nil
449
+ end
450
+ end
451
+ end
452
+
453
+ context "when uri port is not defined" do
454
+ context "falls back to 80 port on http" do
455
+ let(:uri) { URI 'http://foobar.com' }
456
+ before { allow(uri).to receive(:port).and_return(nil) }
457
+ it { expect(subject.port).to be 80 }
458
+ end
459
+
460
+ context "falls back to 443 port on https" do
461
+ let(:uri) { URI 'https://foobar.com' }
462
+ before { allow(uri).to receive(:port).and_return(nil) }
463
+ it { expect(subject.port).to be 443 }
464
+ end
465
+ end
466
+ end
467
+ end
468
+ end