httparty 0.17.0 → 0.17.1

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 (60) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +7 -0
  3. data/Gemfile +1 -0
  4. data/httparty.gemspec +0 -1
  5. data/lib/httparty.rb +2 -18
  6. data/lib/httparty/connection_adapter.rb +10 -0
  7. data/lib/httparty/headers_processor.rb +30 -0
  8. data/lib/httparty/request.rb +4 -4
  9. data/lib/httparty/version.rb +1 -1
  10. metadata +4 -103
  11. data/features/basic_authentication.feature +0 -20
  12. data/features/command_line.feature +0 -95
  13. data/features/deals_with_http_error_codes.feature +0 -26
  14. data/features/digest_authentication.feature +0 -30
  15. data/features/handles_compressed_responses.feature +0 -27
  16. data/features/handles_multiple_formats.feature +0 -57
  17. data/features/steps/env.rb +0 -27
  18. data/features/steps/httparty_response_steps.rb +0 -56
  19. data/features/steps/httparty_steps.rb +0 -43
  20. data/features/steps/mongrel_helper.rb +0 -127
  21. data/features/steps/remote_service_steps.rb +0 -92
  22. data/features/supports_read_timeout_option.feature +0 -13
  23. data/features/supports_redirection.feature +0 -22
  24. data/features/supports_timeout_option.feature +0 -13
  25. data/spec/fixtures/delicious.xml +0 -23
  26. data/spec/fixtures/empty.xml +0 -0
  27. data/spec/fixtures/example.html +0 -10
  28. data/spec/fixtures/ssl/generate.sh +0 -29
  29. data/spec/fixtures/ssl/generated/bogushost.crt +0 -29
  30. data/spec/fixtures/ssl/generated/ca.crt +0 -31
  31. data/spec/fixtures/ssl/generated/ca.key +0 -52
  32. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -29
  33. data/spec/fixtures/ssl/generated/server.crt +0 -30
  34. data/spec/fixtures/ssl/generated/server.key +0 -52
  35. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  36. data/spec/fixtures/tiny.gif +0 -0
  37. data/spec/fixtures/twitter.csv +0 -2
  38. data/spec/fixtures/twitter.json +0 -1
  39. data/spec/fixtures/twitter.xml +0 -403
  40. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  41. data/spec/httparty/connection_adapter_spec.rb +0 -628
  42. data/spec/httparty/cookie_hash_spec.rb +0 -100
  43. data/spec/httparty/exception_spec.rb +0 -45
  44. data/spec/httparty/hash_conversions_spec.rb +0 -58
  45. data/spec/httparty/logger/apache_formatter_spec.rb +0 -40
  46. data/spec/httparty/logger/curl_formatter_spec.rb +0 -119
  47. data/spec/httparty/logger/logger_spec.rb +0 -43
  48. data/spec/httparty/logger/logstash_formatter_spec.rb +0 -44
  49. data/spec/httparty/net_digest_auth_spec.rb +0 -270
  50. data/spec/httparty/parser_spec.rb +0 -190
  51. data/spec/httparty/request/body_spec.rb +0 -165
  52. data/spec/httparty/request_spec.rb +0 -1389
  53. data/spec/httparty/response_fragment_spec.rb +0 -17
  54. data/spec/httparty/response_spec.rb +0 -374
  55. data/spec/httparty/ssl_spec.rb +0 -82
  56. data/spec/httparty_spec.rb +0 -943
  57. data/spec/spec_helper.rb +0 -57
  58. data/spec/support/ssl_test_helper.rb +0 -47
  59. data/spec/support/ssl_test_server.rb +0 -80
  60. data/spec/support/stub_response.rb +0 -50
@@ -1,17 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
-
3
- RSpec.describe HTTParty::ResponseFragment do
4
- it "access to fragment" do
5
- fragment = HTTParty::ResponseFragment.new("chunk", nil, nil)
6
- expect(fragment).to eq("chunk")
7
- end
8
-
9
- it "has access to delegators" do
10
- response = double(code: '200')
11
- connection = double
12
- fragment = HTTParty::ResponseFragment.new("chunk", response, connection)
13
- expect(fragment.code).to eq(200)
14
- expect(fragment.http_response).to eq response
15
- expect(fragment.connection).to eq connection
16
- end
17
- end
@@ -1,374 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe HTTParty::Response do
4
- before do
5
- @last_modified = Date.new(2010, 1, 15).to_s
6
- @content_length = '1024'
7
- @request_object = HTTParty::Request.new Net::HTTP::Get, '/'
8
- @response_object = Net::HTTPOK.new('1.1', 200, 'OK')
9
- allow(@response_object).to receive_messages(body: "{foo:'bar'}")
10
- @response_object['last-modified'] = @last_modified
11
- @response_object['content-length'] = @content_length
12
- @parsed_response = lambda { {"foo" => "bar"} }
13
- @response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
14
- end
15
-
16
- describe ".underscore" do
17
- it "works with one capitalized word" do
18
- expect(HTTParty::Response.underscore("Accepted")).to eq("accepted")
19
- end
20
-
21
- it "works with titlecase" do
22
- expect(HTTParty::Response.underscore("BadGateway")).to eq("bad_gateway")
23
- end
24
-
25
- it "works with all caps" do
26
- expect(HTTParty::Response.underscore("OK")).to eq("ok")
27
- end
28
- end
29
-
30
- describe "initialization" do
31
- it "should set the Net::HTTP Response" do
32
- expect(@response.response).to eq(@response_object)
33
- end
34
-
35
- it "should set body" do
36
- expect(@response.body).to eq(@response_object.body)
37
- end
38
-
39
- it "should set code" do
40
- expect(@response.code).to eq(@response_object.code)
41
- end
42
-
43
- it "should set code as an Integer" do
44
- expect(@response.code).to be_a(Integer)
45
- end
46
-
47
- it "should set http_version" do
48
- unparseable_body = lambda { raise "Unparseable" }
49
- unparseable_response = HTTParty::Response.new(@request_object, @response_object, unparseable_body)
50
- expect(unparseable_response.http_version).to eq(@response_object.http_version)
51
- end
52
-
53
- context 'when raise_on is supplied' do
54
- let(:request) { HTTParty::Request.new(Net::HTTP::Get, '/', raise_on: [404]) }
55
-
56
- context "and response's status code is in range" do
57
- let(:body) { 'Not Found' }
58
- let(:response) { Net::HTTPNotFound.new('1.1', 404, body) }
59
-
60
- before do
61
- allow(response).to receive(:body).and_return(body)
62
- end
63
-
64
- subject { described_class.new(request, response, @parsed_response) }
65
-
66
- it 'throws exception' do
67
- expect{ subject }.to raise_error(HTTParty::ResponseError, "Code 404 - #{body}")
68
- end
69
- end
70
-
71
- context "and response's status code is not in range" do
72
- subject { described_class.new(request, @response_object, @parsed_response) }
73
-
74
- it 'does not throw exception' do
75
- expect{ subject }.not_to raise_error
76
- end
77
- end
78
- end
79
- end
80
-
81
- it 'does raise an error about itself when using #method' do
82
- expect {
83
- HTTParty::Response.new(@request_object, @response_object, @parsed_response).method(:qux)
84
- }.to raise_error(NameError, /HTTParty\:\:Response/)
85
- end
86
-
87
- it 'does raise an error about itself when invoking a method that does not exist' do
88
- expect {
89
- HTTParty::Response.new(@request_object, @response_object, @parsed_response).qux
90
- }.to raise_error(NoMethodError, /HTTParty\:\:Response/)
91
- end
92
-
93
- it "returns response headers" do
94
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
95
- expect(response.headers).to eq({'last-modified' => [@last_modified], 'content-length' => [@content_length]})
96
- end
97
-
98
- it "should send missing methods to delegate" do
99
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
100
- expect(response['foo']).to eq('bar')
101
- end
102
-
103
- it "response to request" do
104
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
105
- expect(response.respond_to?(:request)).to be_truthy
106
- end
107
-
108
- it "responds to response" do
109
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
110
- expect(response.respond_to?(:response)).to be_truthy
111
- end
112
-
113
- it "responds to body" do
114
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
115
- expect(response.respond_to?(:body)).to be_truthy
116
- end
117
-
118
- it "responds to headers" do
119
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
120
- expect(response.respond_to?(:headers)).to be_truthy
121
- end
122
-
123
- it "responds to parsed_response" do
124
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
125
- expect(response.respond_to?(:parsed_response)).to be_truthy
126
- end
127
-
128
- it "responds to predicates" do
129
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
130
- expect(response.respond_to?(:success?)).to be_truthy
131
- end
132
-
133
- it "responds to anything parsed_response responds to" do
134
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
135
- expect(response.respond_to?(:[])).to be_truthy
136
- end
137
-
138
- context 'response is array' do
139
- let(:response_value) { [{'foo' => 'bar'}, {'foo' => 'baz'}] }
140
- let(:response) { HTTParty::Response.new(@request_object, @response_object, lambda { response_value }) }
141
- it "should be able to iterate" do
142
- expect(response.size).to eq(2)
143
- expect {
144
- response.each { |item| }
145
- }.to_not raise_error
146
- end
147
-
148
- it 'should respond to array methods' do
149
- expect(response).to respond_to(:bsearch, :compact, :cycle, :delete, :each, :flatten, :flatten!, :compact, :join)
150
- end
151
-
152
- it 'should equal the string response object body' do
153
- expect(response.to_s).to eq(@response_object.body.to_s)
154
- end
155
-
156
- it 'should display the same as an array' do
157
- a = StringIO.new
158
- b = StringIO.new
159
- response_value.display(b)
160
- response.display(a)
161
-
162
- expect(a.string).to eq(b.string)
163
- end
164
- end
165
-
166
- it "allows headers to be accessed by mixed-case names in hash notation" do
167
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
168
- expect(response.headers['Content-LENGTH']).to eq(@content_length)
169
- end
170
-
171
- it "returns a comma-delimited value when multiple values exist" do
172
- @response_object.add_field 'set-cookie', 'csrf_id=12345; path=/'
173
- @response_object.add_field 'set-cookie', '_github_ses=A123CdE; path=/'
174
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
175
- expect(response.headers['set-cookie']).to eq("csrf_id=12345; path=/, _github_ses=A123CdE; path=/")
176
- end
177
-
178
- # Backwards-compatibility - previously, #headers returned a Hash
179
- it "responds to hash methods" do
180
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
181
- hash_methods = {}.methods - response.headers.methods
182
- hash_methods.each do |method_name|
183
- expect(response.headers.respond_to?(method_name)).to be_truthy
184
- end
185
- end
186
-
187
- describe "#is_a?" do
188
- subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) }
189
-
190
- it { is_expected.to respond_to(:is_a?).with(1).arguments }
191
- it { expect(subject.is_a?(HTTParty::Response)).to be_truthy }
192
- it { expect(subject.is_a?(Object)).to be_truthy }
193
- end
194
-
195
- describe "#kind_of?" do
196
- subject { HTTParty::Response.new(@request_object, @response_object, @parsed_response) }
197
-
198
- it { is_expected.to respond_to(:kind_of?).with(1).arguments }
199
- it { expect(subject.kind_of?(HTTParty::Response)).to be_truthy }
200
- it { expect(subject.kind_of?(Object)).to be_truthy }
201
- end
202
-
203
- describe "semantic methods for response codes" do
204
- def response_mock(klass)
205
- response = klass.new('', '', '')
206
- allow(response).to receive(:body)
207
- response
208
- end
209
-
210
- context "major codes" do
211
- it "is information" do
212
- net_response = response_mock(Net::HTTPInformation)
213
- response = HTTParty::Response.new(@request_object, net_response, '')
214
- expect(response.information?).to be_truthy
215
- end
216
-
217
- it "is success" do
218
- net_response = response_mock(Net::HTTPSuccess)
219
- response = HTTParty::Response.new(@request_object, net_response, '')
220
- expect(response.success?).to be_truthy
221
- end
222
-
223
- it "is redirection" do
224
- net_response = response_mock(Net::HTTPRedirection)
225
- response = HTTParty::Response.new(@request_object, net_response, '')
226
- expect(response.redirection?).to be_truthy
227
- end
228
-
229
- it "is client error" do
230
- net_response = response_mock(Net::HTTPClientError)
231
- response = HTTParty::Response.new(@request_object, net_response, '')
232
- expect(response.client_error?).to be_truthy
233
- end
234
-
235
- it "is server error" do
236
- net_response = response_mock(Net::HTTPServerError)
237
- response = HTTParty::Response.new(@request_object, net_response, '')
238
- expect(response.server_error?).to be_truthy
239
- end
240
- end
241
-
242
- context "for specific codes" do
243
- SPECIFIC_CODES = {
244
- accepted?: Net::HTTPAccepted,
245
- bad_gateway?: Net::HTTPBadGateway,
246
- bad_request?: Net::HTTPBadRequest,
247
- conflict?: Net::HTTPConflict,
248
- continue?: Net::HTTPContinue,
249
- created?: Net::HTTPCreated,
250
- expectation_failed?: Net::HTTPExpectationFailed,
251
- forbidden?: Net::HTTPForbidden,
252
- found?: Net::HTTPFound,
253
- gateway_time_out?: Net::HTTPGatewayTimeOut,
254
- gone?: Net::HTTPGone,
255
- internal_server_error?: Net::HTTPInternalServerError,
256
- length_required?: Net::HTTPLengthRequired,
257
- method_not_allowed?: Net::HTTPMethodNotAllowed,
258
- moved_permanently?: Net::HTTPMovedPermanently,
259
- multiple_choice?: Net::HTTPMultipleChoice,
260
- no_content?: Net::HTTPNoContent,
261
- non_authoritative_information?: Net::HTTPNonAuthoritativeInformation,
262
- not_acceptable?: Net::HTTPNotAcceptable,
263
- not_found?: Net::HTTPNotFound,
264
- not_implemented?: Net::HTTPNotImplemented,
265
- not_modified?: Net::HTTPNotModified,
266
- ok?: Net::HTTPOK,
267
- partial_content?: Net::HTTPPartialContent,
268
- payment_required?: Net::HTTPPaymentRequired,
269
- precondition_failed?: Net::HTTPPreconditionFailed,
270
- proxy_authentication_required?: Net::HTTPProxyAuthenticationRequired,
271
- request_entity_too_large?: Net::HTTPRequestEntityTooLarge,
272
- request_time_out?: Net::HTTPRequestTimeOut,
273
- request_uri_too_long?: Net::HTTPRequestURITooLong,
274
- requested_range_not_satisfiable?: Net::HTTPRequestedRangeNotSatisfiable,
275
- reset_content?: Net::HTTPResetContent,
276
- see_other?: Net::HTTPSeeOther,
277
- service_unavailable?: Net::HTTPServiceUnavailable,
278
- switch_protocol?: Net::HTTPSwitchProtocol,
279
- temporary_redirect?: Net::HTTPTemporaryRedirect,
280
- unauthorized?: Net::HTTPUnauthorized,
281
- unsupported_media_type?: Net::HTTPUnsupportedMediaType,
282
- use_proxy?: Net::HTTPUseProxy,
283
- version_not_supported?: Net::HTTPVersionNotSupported
284
- }
285
-
286
- # Ruby 2.0, new name for this response.
287
- if RUBY_VERSION >= "2.0.0" && ::RUBY_PLATFORM != "java"
288
- SPECIFIC_CODES[:multiple_choices?] = Net::HTTPMultipleChoices
289
- end
290
-
291
- # Ruby 2.6, those status codes have been updated.
292
- if RUBY_VERSION >= "2.6.0" && ::RUBY_PLATFORM != "java"
293
- SPECIFIC_CODES[:gateway_timeout?] = Net::HTTPGatewayTimeout
294
- SPECIFIC_CODES[:payload_too_large?] = Net::HTTPPayloadTooLarge
295
- SPECIFIC_CODES[:request_timeout?] = Net::HTTPRequestTimeout
296
- SPECIFIC_CODES[:uri_too_long?] = Net::HTTPURITooLong
297
- SPECIFIC_CODES[:range_not_satisfiable?] = Net::HTTPRangeNotSatisfiable
298
- end
299
-
300
- SPECIFIC_CODES.each do |method, klass|
301
- it "responds to #{method}" do
302
- net_response = response_mock(klass)
303
- response = HTTParty::Response.new(@request_object, net_response, '')
304
- expect(response.__send__(method)).to be_truthy
305
- end
306
- end
307
- end
308
- end
309
-
310
- describe "headers" do
311
- let (:empty_headers) { HTTParty::Response::Headers.new }
312
- let (:some_headers_hash) do
313
- {'Cookie' => 'bob',
314
- 'Content-Encoding' => 'meow'}
315
- end
316
- let (:some_headers) do
317
- HTTParty::Response::Headers.new.tap do |h|
318
- some_headers_hash.each_pair do |k,v|
319
- h[k] = v
320
- end
321
- end
322
- end
323
- it "can initialize without headers" do
324
- expect(empty_headers).to eq({})
325
- end
326
-
327
- it 'always equals itself' do
328
- expect(empty_headers).to eq(empty_headers)
329
- expect(some_headers).to eq(some_headers)
330
- end
331
-
332
- it 'does not equal itself when not equivalent' do
333
- expect(empty_headers).to_not eq(some_headers)
334
- end
335
-
336
- it 'does equal a hash' do
337
- expect(empty_headers).to eq({})
338
-
339
- expect(some_headers).to eq(some_headers_hash)
340
- end
341
- end
342
-
343
- describe "#tap" do
344
- it "is possible to tap into a response" do
345
- result = @response.tap(&:code)
346
-
347
- expect(result).to eq @response
348
- end
349
- end
350
-
351
- describe "#inspect" do
352
- it "works" do
353
- inspect = @response.inspect
354
- expect(inspect).to include("HTTParty::Response:0x")
355
- expect(inspect).to include("parsed_response={\"foo\"=>\"bar\"}")
356
- expect(inspect).to include("@response=#<Net::HTTPOK 200 OK readbody=false>")
357
- expect(inspect).to include("@headers={")
358
- expect(inspect).to include("last-modified")
359
- expect(inspect).to include("content-length")
360
- end
361
- end
362
-
363
- describe 'marshalling' do
364
- before { RSpec::Mocks.space.proxy_for(@response_object).remove_stub(:body) }
365
-
366
- specify do
367
- marshalled = Marshal.load(Marshal.dump(@response))
368
-
369
- expect(marshalled.headers).to eq @response.headers
370
- expect(marshalled.body).to eq @response.body
371
- expect(marshalled.code).to eq @response.code
372
- end
373
- end
374
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe HTTParty::Request do
4
- context "SSL certificate verification" do
5
- before do
6
- WebMock.disable!
7
- end
8
-
9
- after do
10
- WebMock.enable!
11
- end
12
-
13
- it "should fail when no trusted CA list is specified, by default" do
14
- expect do
15
- ssl_verify_test(nil, nil, "selfsigned.crt")
16
- end.to raise_error OpenSSL::SSL::SSLError
17
- end
18
-
19
- it "should work when no trusted CA list is specified, when the verify option is set to false" do
20
- expect(ssl_verify_test(nil, nil, "selfsigned.crt", verify: false).parsed_response).to eq({'success' => true})
21
- end
22
-
23
- it "should fail when no trusted CA list is specified, with a bogus hostname, by default" do
24
- expect do
25
- ssl_verify_test(nil, nil, "bogushost.crt")
26
- end.to raise_error OpenSSL::SSL::SSLError
27
- end
28
-
29
- it "should work when no trusted CA list is specified, even with a bogus hostname, when the verify option is set to true" do
30
- expect(ssl_verify_test(nil, nil, "bogushost.crt", verify: false).parsed_response).to eq({'success' => true})
31
- end
32
-
33
- it "should work when using ssl_ca_file with a self-signed CA" do
34
- expect(ssl_verify_test(:ssl_ca_file, "selfsigned.crt", "selfsigned.crt").parsed_response).to eq({'success' => true})
35
- end
36
-
37
- it "should work when using ssl_ca_file with a certificate authority" do
38
- expect(ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").parsed_response).to eq({'success' => true})
39
- end
40
-
41
- it "should work when using ssl_ca_path with a certificate authority" do
42
- http = Net::HTTP.new('www.google.com', 443)
43
- response = double(Net::HTTPResponse, :[] => '', body: '', to_hash: {})
44
- allow(http).to receive(:request).and_return(response)
45
- expect(Net::HTTP).to receive(:new).with('www.google.com', 443).and_return(http)
46
- expect(http).to receive(:ca_path=).with('/foo/bar')
47
- HTTParty.get('https://www.google.com', ssl_ca_path: '/foo/bar')
48
- end
49
-
50
- it "should fail when using ssl_ca_file and the server uses an unrecognized certificate authority" do
51
- expect do
52
- ssl_verify_test(:ssl_ca_file, "ca.crt", "selfsigned.crt")
53
- end.to raise_error(OpenSSL::SSL::SSLError)
54
- end
55
-
56
- it "should fail when using ssl_ca_path and the server uses an unrecognized certificate authority" do
57
- expect do
58
- ssl_verify_test(:ssl_ca_path, ".", "selfsigned.crt")
59
- end.to raise_error(OpenSSL::SSL::SSLError)
60
- end
61
-
62
- it "should fail when using ssl_ca_file and the server uses a bogus hostname" do
63
- expect do
64
- ssl_verify_test(:ssl_ca_file, "ca.crt", "bogushost.crt")
65
- end.to raise_error(OpenSSL::SSL::SSLError)
66
- end
67
-
68
- it "should fail when using ssl_ca_path and the server uses a bogus hostname" do
69
- expect do
70
- ssl_verify_test(:ssl_ca_path, ".", "bogushost.crt")
71
- end.to raise_error(OpenSSL::SSL::SSLError)
72
- end
73
-
74
- it "should provide the certificate used by the server via peer_cert" do
75
- peer_cert = nil
76
- ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt") do |response|
77
- peer_cert ||= response.connection.peer_cert
78
- end
79
- expect(peer_cert).to be_a OpenSSL::X509::Certificate
80
- end
81
- end
82
- end