httparty 0.13.6 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +18 -0
  3. data/.github/workflows/ci.yml +26 -0
  4. data/.gitignore +3 -0
  5. data/.rubocop_todo.yml +1 -1
  6. data/{History → Changelog.md} +228 -58
  7. data/Gemfile +10 -3
  8. data/Guardfile +3 -2
  9. data/README.md +8 -7
  10. data/bin/httparty +3 -1
  11. data/docs/README.md +191 -0
  12. data/examples/README.md +34 -12
  13. data/examples/aaws.rb +7 -3
  14. data/examples/body_stream.rb +14 -0
  15. data/examples/crack.rb +1 -1
  16. data/examples/custom_parsers.rb +5 -1
  17. data/examples/delicious.rb +4 -4
  18. data/examples/headers_and_user_agents.rb +7 -3
  19. data/examples/idn.rb +10 -0
  20. data/examples/logging.rb +4 -4
  21. data/examples/microsoft_graph.rb +52 -0
  22. data/examples/multipart.rb +22 -0
  23. data/examples/peer_cert.rb +9 -0
  24. data/examples/stackexchange.rb +1 -1
  25. data/examples/stream_download.rb +26 -0
  26. data/examples/tripit_sign_in.rb +17 -6
  27. data/examples/twitter.rb +2 -2
  28. data/examples/whoismyrep.rb +1 -1
  29. data/httparty.gemspec +8 -6
  30. data/lib/httparty/connection_adapter.rb +86 -20
  31. data/lib/httparty/cookie_hash.rb +10 -8
  32. data/lib/httparty/decompressor.rb +102 -0
  33. data/lib/httparty/exceptions.rb +8 -2
  34. data/lib/httparty/hash_conversions.rb +30 -8
  35. data/lib/httparty/headers_processor.rb +32 -0
  36. data/lib/httparty/logger/apache_formatter.rb +31 -6
  37. data/lib/httparty/logger/curl_formatter.rb +68 -23
  38. data/lib/httparty/logger/logger.rb +5 -1
  39. data/lib/httparty/logger/logstash_formatter.rb +61 -0
  40. data/lib/httparty/module_inheritable_attributes.rb +6 -4
  41. data/lib/httparty/net_digest_auth.rb +23 -21
  42. data/lib/httparty/parser.rb +25 -14
  43. data/lib/httparty/request/body.rb +105 -0
  44. data/lib/httparty/request/multipart_boundary.rb +13 -0
  45. data/lib/httparty/request.rb +160 -106
  46. data/lib/httparty/response/headers.rb +23 -19
  47. data/lib/httparty/response.rb +92 -13
  48. data/lib/httparty/response_fragment.rb +21 -0
  49. data/lib/httparty/text_encoder.rb +72 -0
  50. data/lib/httparty/utils.rb +13 -0
  51. data/lib/httparty/version.rb +3 -1
  52. data/lib/httparty.rb +98 -35
  53. data/website/css/common.css +1 -1
  54. metadata +35 -115
  55. data/.simplecov +0 -1
  56. data/.travis.yml +0 -7
  57. data/features/basic_authentication.feature +0 -20
  58. data/features/command_line.feature +0 -95
  59. data/features/deals_with_http_error_codes.feature +0 -26
  60. data/features/digest_authentication.feature +0 -30
  61. data/features/handles_compressed_responses.feature +0 -27
  62. data/features/handles_multiple_formats.feature +0 -57
  63. data/features/steps/env.rb +0 -27
  64. data/features/steps/httparty_response_steps.rb +0 -52
  65. data/features/steps/httparty_steps.rb +0 -43
  66. data/features/steps/mongrel_helper.rb +0 -127
  67. data/features/steps/remote_service_steps.rb +0 -90
  68. data/features/supports_read_timeout_option.feature +0 -13
  69. data/features/supports_redirection.feature +0 -22
  70. data/features/supports_timeout_option.feature +0 -13
  71. data/spec/fixtures/delicious.xml +0 -23
  72. data/spec/fixtures/empty.xml +0 -0
  73. data/spec/fixtures/google.html +0 -3
  74. data/spec/fixtures/ssl/generate.sh +0 -29
  75. data/spec/fixtures/ssl/generated/1fe462c2.0 +0 -16
  76. data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
  77. data/spec/fixtures/ssl/generated/ca.crt +0 -16
  78. data/spec/fixtures/ssl/generated/ca.key +0 -15
  79. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
  80. data/spec/fixtures/ssl/generated/server.crt +0 -13
  81. data/spec/fixtures/ssl/generated/server.key +0 -15
  82. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  83. data/spec/fixtures/twitter.csv +0 -2
  84. data/spec/fixtures/twitter.json +0 -1
  85. data/spec/fixtures/twitter.xml +0 -403
  86. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  87. data/spec/httparty/connection_adapter_spec.rb +0 -468
  88. data/spec/httparty/cookie_hash_spec.rb +0 -83
  89. data/spec/httparty/exception_spec.rb +0 -38
  90. data/spec/httparty/hash_conversions_spec.rb +0 -41
  91. data/spec/httparty/logger/apache_formatter_spec.rb +0 -41
  92. data/spec/httparty/logger/curl_formatter_spec.rb +0 -18
  93. data/spec/httparty/logger/logger_spec.rb +0 -38
  94. data/spec/httparty/net_digest_auth_spec.rb +0 -230
  95. data/spec/httparty/parser_spec.rb +0 -173
  96. data/spec/httparty/request_spec.rb +0 -1073
  97. data/spec/httparty/response_spec.rb +0 -241
  98. data/spec/httparty/ssl_spec.rb +0 -74
  99. data/spec/httparty_spec.rb +0 -850
  100. data/spec/spec_helper.rb +0 -59
  101. data/spec/support/ssl_test_helper.rb +0 -47
  102. data/spec/support/ssl_test_server.rb +0 -80
  103. data/spec/support/stub_response.rb +0 -49
@@ -1,241 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '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 a Fixnum" do
44
- expect(@response.code).to be_an_instance_of(Fixnum)
45
- end
46
- end
47
-
48
- it "returns response headers" do
49
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
50
- expect(response.headers).to eq({'last-modified' => [@last_modified], 'content-length' => [@content_length]})
51
- end
52
-
53
- it "should send missing methods to delegate" do
54
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
55
- expect(response['foo']).to eq('bar')
56
- end
57
-
58
- it "response to request" do
59
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
60
- expect(response.respond_to?(:request)).to be_truthy
61
- end
62
-
63
- it "responds to response" do
64
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
65
- expect(response.respond_to?(:response)).to be_truthy
66
- end
67
-
68
- it "responds to body" do
69
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
70
- expect(response.respond_to?(:body)).to be_truthy
71
- end
72
-
73
- it "responds to headers" do
74
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
75
- expect(response.respond_to?(:headers)).to be_truthy
76
- end
77
-
78
- it "responds to parsed_response" do
79
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
80
- expect(response.respond_to?(:parsed_response)).to be_truthy
81
- end
82
-
83
- it "responds to anything parsed_response responds to" do
84
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
85
- expect(response.respond_to?(:[])).to be_truthy
86
- end
87
-
88
- it "should be able to iterate if it is array" do
89
- response = HTTParty::Response.new(@request_object, @response_object, lambda { [{'foo' => 'bar'}, {'foo' => 'baz'}] })
90
- expect(response.size).to eq(2)
91
- expect {
92
- response.each { |item| }
93
- }.to_not raise_error
94
- end
95
-
96
- it "allows headers to be accessed by mixed-case names in hash notation" do
97
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
98
- expect(response.headers['Content-LENGTH']).to eq(@content_length)
99
- end
100
-
101
- it "returns a comma-delimited value when multiple values exist" do
102
- @response_object.add_field 'set-cookie', 'csrf_id=12345; path=/'
103
- @response_object.add_field 'set-cookie', '_github_ses=A123CdE; path=/'
104
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
105
- expect(response.headers['set-cookie']).to eq("csrf_id=12345; path=/, _github_ses=A123CdE; path=/")
106
- end
107
-
108
- # Backwards-compatibility - previously, #headers returned a Hash
109
- it "responds to hash methods" do
110
- response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
111
- hash_methods = {}.methods - response.headers.methods
112
- hash_methods.each do |method_name|
113
- expect(response.headers.respond_to?(method_name)).to be_truthy
114
- end
115
- end
116
-
117
- describe "semantic methods for response codes" do
118
- def response_mock(klass)
119
- response = klass.new('', '', '')
120
- allow(response).to receive(:body)
121
- response
122
- end
123
-
124
- context "major codes" do
125
- it "is information" do
126
- net_response = response_mock(Net::HTTPInformation)
127
- response = HTTParty::Response.new(@request_object, net_response, '')
128
- expect(response.information?).to be_truthy
129
- end
130
-
131
- it "is success" do
132
- net_response = response_mock(Net::HTTPSuccess)
133
- response = HTTParty::Response.new(@request_object, net_response, '')
134
- expect(response.success?).to be_truthy
135
- end
136
-
137
- it "is redirection" do
138
- net_response = response_mock(Net::HTTPRedirection)
139
- response = HTTParty::Response.new(@request_object, net_response, '')
140
- expect(response.redirection?).to be_truthy
141
- end
142
-
143
- it "is client error" do
144
- net_response = response_mock(Net::HTTPClientError)
145
- response = HTTParty::Response.new(@request_object, net_response, '')
146
- expect(response.client_error?).to be_truthy
147
- end
148
-
149
- it "is server error" do
150
- net_response = response_mock(Net::HTTPServerError)
151
- response = HTTParty::Response.new(@request_object, net_response, '')
152
- expect(response.server_error?).to be_truthy
153
- end
154
- end
155
-
156
- context "for specific codes" do
157
- SPECIFIC_CODES = {
158
- accepted?: Net::HTTPAccepted,
159
- bad_gateway?: Net::HTTPBadGateway,
160
- bad_request?: Net::HTTPBadRequest,
161
- conflict?: Net::HTTPConflict,
162
- continue?: Net::HTTPContinue,
163
- created?: Net::HTTPCreated,
164
- expectation_failed?: Net::HTTPExpectationFailed,
165
- forbidden?: Net::HTTPForbidden,
166
- found?: Net::HTTPFound,
167
- gateway_time_out?: Net::HTTPGatewayTimeOut,
168
- gone?: Net::HTTPGone,
169
- internal_server_error?: Net::HTTPInternalServerError,
170
- length_required?: Net::HTTPLengthRequired,
171
- method_not_allowed?: Net::HTTPMethodNotAllowed,
172
- moved_permanently?: Net::HTTPMovedPermanently,
173
- multiple_choice?: Net::HTTPMultipleChoice,
174
- no_content?: Net::HTTPNoContent,
175
- non_authoritative_information?: Net::HTTPNonAuthoritativeInformation,
176
- not_acceptable?: Net::HTTPNotAcceptable,
177
- not_found?: Net::HTTPNotFound,
178
- not_implemented?: Net::HTTPNotImplemented,
179
- not_modified?: Net::HTTPNotModified,
180
- ok?: Net::HTTPOK,
181
- partial_content?: Net::HTTPPartialContent,
182
- payment_required?: Net::HTTPPaymentRequired,
183
- precondition_failed?: Net::HTTPPreconditionFailed,
184
- proxy_authentication_required?: Net::HTTPProxyAuthenticationRequired,
185
- request_entity_too_large?: Net::HTTPRequestEntityTooLarge,
186
- request_time_out?: Net::HTTPRequestTimeOut,
187
- request_uri_too_long?: Net::HTTPRequestURITooLong,
188
- requested_range_not_satisfiable?: Net::HTTPRequestedRangeNotSatisfiable,
189
- reset_content?: Net::HTTPResetContent,
190
- see_other?: Net::HTTPSeeOther,
191
- service_unavailable?: Net::HTTPServiceUnavailable,
192
- switch_protocol?: Net::HTTPSwitchProtocol,
193
- temporary_redirect?: Net::HTTPTemporaryRedirect,
194
- unauthorized?: Net::HTTPUnauthorized,
195
- unsupported_media_type?: Net::HTTPUnsupportedMediaType,
196
- use_proxy?: Net::HTTPUseProxy,
197
- version_not_supported?: Net::HTTPVersionNotSupported
198
- }
199
-
200
- # Ruby 2.0, new name for this response.
201
- if RUBY_VERSION >= "2.0.0" && ::RUBY_PLATFORM != "java"
202
- SPECIFIC_CODES[:multiple_choices?] = Net::HTTPMultipleChoices
203
- end
204
-
205
- SPECIFIC_CODES.each do |method, klass|
206
- it "responds to #{method}" do
207
- net_response = response_mock(klass)
208
- response = HTTParty::Response.new(@request_object, net_response, '')
209
- expect(response.__send__(method)).to be_truthy
210
- end
211
- end
212
- end
213
- end
214
-
215
- describe "headers" do
216
- it "can initialize without headers" do
217
- headers = HTTParty::Response::Headers.new
218
- expect(headers).to eq({})
219
- end
220
- end
221
-
222
- describe "#tap" do
223
- it "is possible to tap into a response" do
224
- result = @response.tap(&:code)
225
-
226
- expect(result).to eq @response
227
- end
228
- end
229
-
230
- describe "#inspect" do
231
- it "works" do
232
- inspect = @response.inspect
233
- expect(inspect).to include("HTTParty::Response:0x")
234
- expect(inspect).to include("parsed_response={\"foo\"=>\"bar\"}")
235
- expect(inspect).to include("@response=#<Net::HTTPOK 200 OK readbody=false>")
236
- expect(inspect).to include("@headers={")
237
- expect(inspect).to include("last-modified")
238
- expect(inspect).to include("content-length")
239
- end
240
- end
241
- end
@@ -1,74 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
-
3
- RSpec.describe HTTParty::Request do
4
- context "SSL certificate verification" do
5
- before do
6
- FakeWeb.allow_net_connect = true
7
- end
8
-
9
- after do
10
- FakeWeb.allow_net_connect = false
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
- end
74
- end