httparty 0.15.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) 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.md → Changelog.md} +137 -0
  7. data/Gemfile +8 -1
  8. data/Guardfile +3 -2
  9. data/README.md +6 -6
  10. data/docs/README.md +120 -38
  11. data/examples/README.md +28 -11
  12. data/examples/aaws.rb +6 -2
  13. data/examples/body_stream.rb +14 -0
  14. data/examples/custom_parsers.rb +4 -0
  15. data/examples/headers_and_user_agents.rb +7 -3
  16. data/examples/idn.rb +10 -0
  17. data/examples/logging.rb +3 -3
  18. data/examples/microsoft_graph.rb +52 -0
  19. data/examples/multipart.rb +22 -0
  20. data/examples/peer_cert.rb +9 -0
  21. data/examples/stream_download.rb +8 -2
  22. data/httparty.gemspec +7 -4
  23. data/lib/httparty/connection_adapter.rb +59 -16
  24. data/lib/httparty/cookie_hash.rb +10 -8
  25. data/lib/httparty/decompressor.rb +102 -0
  26. data/lib/httparty/exceptions.rb +4 -1
  27. data/lib/httparty/hash_conversions.rb +28 -12
  28. data/lib/httparty/headers_processor.rb +32 -0
  29. data/lib/httparty/logger/apache_formatter.rb +31 -6
  30. data/lib/httparty/logger/curl_formatter.rb +9 -7
  31. data/lib/httparty/logger/logger.rb +5 -1
  32. data/lib/httparty/logger/logstash_formatter.rb +61 -0
  33. data/lib/httparty/module_inheritable_attributes.rb +6 -4
  34. data/lib/httparty/net_digest_auth.rb +15 -15
  35. data/lib/httparty/parser.rb +25 -16
  36. data/lib/httparty/request/body.rb +105 -0
  37. data/lib/httparty/request/multipart_boundary.rb +13 -0
  38. data/lib/httparty/request.rb +96 -105
  39. data/lib/httparty/response/headers.rb +6 -2
  40. data/lib/httparty/response.rb +59 -8
  41. data/lib/httparty/response_fragment.rb +21 -0
  42. data/lib/httparty/text_encoder.rb +72 -0
  43. data/lib/httparty/utils.rb +13 -0
  44. data/lib/httparty/version.rb +3 -1
  45. data/lib/httparty.rb +70 -25
  46. data/website/css/common.css +1 -1
  47. metadata +38 -106
  48. data/.simplecov +0 -1
  49. data/.travis.yml +0 -8
  50. data/features/basic_authentication.feature +0 -20
  51. data/features/command_line.feature +0 -95
  52. data/features/deals_with_http_error_codes.feature +0 -26
  53. data/features/digest_authentication.feature +0 -30
  54. data/features/handles_compressed_responses.feature +0 -27
  55. data/features/handles_multiple_formats.feature +0 -57
  56. data/features/steps/env.rb +0 -27
  57. data/features/steps/httparty_response_steps.rb +0 -56
  58. data/features/steps/httparty_steps.rb +0 -43
  59. data/features/steps/mongrel_helper.rb +0 -127
  60. data/features/steps/remote_service_steps.rb +0 -92
  61. data/features/supports_read_timeout_option.feature +0 -13
  62. data/features/supports_redirection.feature +0 -22
  63. data/features/supports_timeout_option.feature +0 -13
  64. data/spec/fixtures/delicious.xml +0 -23
  65. data/spec/fixtures/empty.xml +0 -0
  66. data/spec/fixtures/google.html +0 -3
  67. data/spec/fixtures/ssl/generate.sh +0 -29
  68. data/spec/fixtures/ssl/generated/1fe462c2.0 +0 -1
  69. data/spec/fixtures/ssl/generated/bogushost.crt +0 -13
  70. data/spec/fixtures/ssl/generated/ca.crt +0 -16
  71. data/spec/fixtures/ssl/generated/ca.key +0 -15
  72. data/spec/fixtures/ssl/generated/selfsigned.crt +0 -14
  73. data/spec/fixtures/ssl/generated/server.crt +0 -13
  74. data/spec/fixtures/ssl/generated/server.key +0 -15
  75. data/spec/fixtures/ssl/openssl-exts.cnf +0 -9
  76. data/spec/fixtures/twitter.csv +0 -2
  77. data/spec/fixtures/twitter.json +0 -1
  78. data/spec/fixtures/twitter.xml +0 -403
  79. data/spec/fixtures/undefined_method_add_node_for_nil.xml +0 -2
  80. data/spec/httparty/connection_adapter_spec.rb +0 -495
  81. data/spec/httparty/cookie_hash_spec.rb +0 -100
  82. data/spec/httparty/exception_spec.rb +0 -45
  83. data/spec/httparty/hash_conversions_spec.rb +0 -49
  84. data/spec/httparty/logger/apache_formatter_spec.rb +0 -41
  85. data/spec/httparty/logger/curl_formatter_spec.rb +0 -119
  86. data/spec/httparty/logger/logger_spec.rb +0 -38
  87. data/spec/httparty/net_digest_auth_spec.rb +0 -268
  88. data/spec/httparty/parser_spec.rb +0 -178
  89. data/spec/httparty/request_spec.rb +0 -1244
  90. data/spec/httparty/response_spec.rb +0 -347
  91. data/spec/httparty/ssl_spec.rb +0 -74
  92. data/spec/httparty_spec.rb +0 -877
  93. data/spec/spec_helper.rb +0 -59
  94. data/spec/support/ssl_test_helper.rb +0 -47
  95. data/spec/support/ssl_test_server.rb +0 -80
  96. data/spec/support/stub_response.rb +0 -49
@@ -1,877 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
-
3
- RSpec.describe HTTParty do
4
- before(:each) do
5
- @klass = Class.new
6
- @klass.instance_eval { include HTTParty }
7
- end
8
-
9
- describe "pem" do
10
- it 'should set the pem content' do
11
- @klass.pem 'PEM-CONTENT'
12
- expect(@klass.default_options[:pem]).to eq('PEM-CONTENT')
13
- end
14
-
15
- it "should set the password to nil if it's not provided" do
16
- @klass.pem 'PEM-CONTENT'
17
- expect(@klass.default_options[:pem_password]).to be_nil
18
- end
19
-
20
- it 'should set the password' do
21
- @klass.pem 'PEM-CONTENT', 'PASSWORD'
22
- expect(@klass.default_options[:pem_password]).to eq('PASSWORD')
23
- end
24
- end
25
-
26
- describe "pkcs12" do
27
- it 'should set the p12 content' do
28
- @klass.pkcs12 'P12-CONTENT', 'PASSWORD'
29
- expect(@klass.default_options[:p12]).to eq('P12-CONTENT')
30
- end
31
-
32
- it 'should set the password' do
33
- @klass.pkcs12 'P12-CONTENT', 'PASSWORD'
34
- expect(@klass.default_options[:p12_password]).to eq('PASSWORD')
35
- end
36
- end
37
-
38
- describe 'ssl_version' do
39
- it 'should set the ssl_version content' do
40
- @klass.ssl_version :SSLv3
41
- expect(@klass.default_options[:ssl_version]).to eq(:SSLv3)
42
- end
43
- end
44
-
45
- describe 'ciphers' do
46
- it 'should set the ciphers content' do
47
- expect(@klass.default_options[:ciphers]).to be_nil
48
- @klass.ciphers 'RC4-SHA'
49
- expect(@klass.default_options[:ciphers]).to eq('RC4-SHA')
50
- end
51
- end
52
-
53
- describe 'http_proxy' do
54
- it 'should set the address' do
55
- @klass.http_proxy 'proxy.foo.com', 80
56
- options = @klass.default_options
57
- expect(options[:http_proxyaddr]).to eq('proxy.foo.com')
58
- expect(options[:http_proxyport]).to eq(80)
59
- end
60
-
61
- it 'should set the proxy user and pass when they are provided' do
62
- @klass.http_proxy 'proxy.foo.com', 80, 'user', 'pass'
63
- options = @klass.default_options
64
- expect(options[:http_proxyuser]).to eq('user')
65
- expect(options[:http_proxypass]).to eq('pass')
66
- end
67
- end
68
-
69
- describe "base uri" do
70
- before(:each) do
71
- @klass.base_uri('api.foo.com/v1')
72
- end
73
-
74
- it "should have reader" do
75
- expect(@klass.base_uri).to eq('http://api.foo.com/v1')
76
- end
77
-
78
- it 'should have writer' do
79
- @klass.base_uri('http://api.foobar.com')
80
- expect(@klass.base_uri).to eq('http://api.foobar.com')
81
- end
82
-
83
- it 'should not modify the parameter during assignment' do
84
- uri = 'http://api.foobar.com'
85
- @klass.base_uri(uri)
86
- expect(uri).to eq('http://api.foobar.com')
87
- end
88
- end
89
-
90
- describe ".disable_rails_query_string_format" do
91
- it "sets the query string normalizer to HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER" do
92
- @klass.disable_rails_query_string_format
93
- expect(@klass.default_options[:query_string_normalizer]).to eq(HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER)
94
- end
95
- end
96
-
97
- describe ".normalize_base_uri" do
98
- it "should add http if not present for non ssl requests" do
99
- uri = HTTParty.normalize_base_uri('api.foobar.com')
100
- expect(uri).to eq('http://api.foobar.com')
101
- end
102
-
103
- it "should add https if not present for ssl requests" do
104
- uri = HTTParty.normalize_base_uri('api.foo.com/v1:443')
105
- expect(uri).to eq('https://api.foo.com/v1:443')
106
- end
107
-
108
- it "should not remove https for ssl requests" do
109
- uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
110
- expect(uri).to eq('https://api.foo.com/v1:443')
111
- end
112
-
113
- it 'should not modify the parameter' do
114
- uri = 'http://api.foobar.com'
115
- HTTParty.normalize_base_uri(uri)
116
- expect(uri).to eq('http://api.foobar.com')
117
- end
118
-
119
- it "should not treat uri's with a port of 4430 as ssl" do
120
- uri = HTTParty.normalize_base_uri('http://api.foo.com:4430/v1')
121
- expect(uri).to eq('http://api.foo.com:4430/v1')
122
- end
123
- end
124
-
125
- describe "headers" do
126
- def expect_headers(header = {})
127
- expect(HTTParty::Request).to receive(:new) \
128
- .with(anything, anything, hash_including({ headers: header })) \
129
- .and_return(double("mock response", perform: nil))
130
- end
131
-
132
- it "does not modify default_options when no arguments are passed" do
133
- @klass.headers
134
- expect(@klass.default_options[:headers]).to eq(nil)
135
- end
136
-
137
- it "should default to empty hash" do
138
- expect(@klass.headers).to eq({})
139
- end
140
-
141
- it "should be able to be updated" do
142
- init_headers = {foo: 'bar', baz: 'spax'}
143
- @klass.headers init_headers
144
- expect(@klass.headers).to eq(init_headers)
145
- end
146
-
147
- it "uses the class headers when sending a request" do
148
- expect_headers(foo: 'bar')
149
- @klass.headers(foo: 'bar')
150
- @klass.get('')
151
- end
152
-
153
- it "merges class headers with request headers" do
154
- expect_headers(baz: 'spax', foo: 'bar')
155
- @klass.headers(foo: 'bar')
156
- @klass.get('', headers: {baz: 'spax'})
157
- end
158
-
159
- it 'overrides class headers with request headers' do
160
- expect_headers(baz: 'spax', foo: 'baz')
161
- @klass.headers(foo: 'bar')
162
- @klass.get('', headers: {baz: 'spax', foo: 'baz'})
163
- end
164
-
165
- context "with cookies" do
166
- it 'utilizes the class-level cookies' do
167
- expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle')
168
- @klass.headers(foo: 'bar')
169
- @klass.cookies(type: 'snickerdoodle')
170
- @klass.get('')
171
- end
172
-
173
- it 'adds cookies to the headers' do
174
- expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle')
175
- @klass.headers(foo: 'bar')
176
- @klass.get('', cookies: {type: 'snickerdoodle'})
177
- end
178
-
179
- it 'doesnt modify default headers' do
180
- expect(@klass.headers).to eq({})
181
- expect_headers('cookie' => 'type=snickerdoodle')
182
- @klass.get('', cookies: {type: 'snickerdoodle'})
183
- expect(@klass.headers).to eq({})
184
- end
185
-
186
- it 'adds optional cookies to the optional headers' do
187
- expect_headers(baz: 'spax', 'cookie' => 'type=snickerdoodle')
188
- @klass.get('', cookies: {type: 'snickerdoodle'}, headers: {baz: 'spax'})
189
- end
190
- end
191
- end
192
-
193
- describe "cookies" do
194
- def expect_cookie_header(s)
195
- expect(HTTParty::Request).to receive(:new) \
196
- .with(anything, anything, hash_including({ headers: { "cookie" => s } })) \
197
- .and_return(double("mock response", perform: nil))
198
- end
199
-
200
- it "should not be in the headers by default" do
201
- allow(HTTParty::Request).to receive(:new).and_return(double(nil, perform: nil))
202
- @klass.get("")
203
- expect(@klass.headers.keys).not_to include("cookie")
204
- end
205
-
206
- it "should raise an ArgumentError if passed a non-Hash" do
207
- expect do
208
- @klass.cookies("nonsense")
209
- end.to raise_error(ArgumentError)
210
- end
211
-
212
- it "should allow a cookie to be specified with a one-off request" do
213
- expect_cookie_header "type=snickerdoodle"
214
- @klass.get("", cookies: { type: "snickerdoodle" })
215
- end
216
-
217
- describe "when a cookie is set at the class level" do
218
- before(:each) do
219
- @klass.cookies({ type: "snickerdoodle" })
220
- end
221
-
222
- it "should include that cookie in the request" do
223
- expect_cookie_header "type=snickerdoodle"
224
- @klass.get("")
225
- end
226
-
227
- it "should pass the proper cookies when requested multiple times" do
228
- 2.times do
229
- expect_cookie_header "type=snickerdoodle"
230
- @klass.get("")
231
- end
232
- end
233
-
234
- it "should allow the class defaults to be overridden" do
235
- expect_cookie_header "type=chocolate_chip"
236
-
237
- @klass.get("", cookies: { type: "chocolate_chip" })
238
- end
239
- end
240
-
241
- describe "in a class with multiple methods that use different cookies" do
242
- before(:each) do
243
- @klass.instance_eval do
244
- def first_method
245
- get("first_method", cookies: { first_method_cookie: "foo" })
246
- end
247
-
248
- def second_method
249
- get("second_method", cookies: { second_method_cookie: "foo" })
250
- end
251
- end
252
- end
253
-
254
- it "should not allow cookies used in one method to carry over into other methods" do
255
- expect_cookie_header "first_method_cookie=foo"
256
- @klass.first_method
257
-
258
- expect_cookie_header "second_method_cookie=foo"
259
- @klass.second_method
260
- end
261
- end
262
- end
263
-
264
- describe "default params" do
265
- it "should default to empty hash" do
266
- expect(@klass.default_params).to eq({})
267
- end
268
-
269
- it "should be able to be updated" do
270
- new_defaults = {foo: 'bar', baz: 'spax'}
271
- @klass.default_params new_defaults
272
- expect(@klass.default_params).to eq(new_defaults)
273
- end
274
- end
275
-
276
- describe "default timeout" do
277
- it "should default to nil" do
278
- expect(@klass.default_options[:timeout]).to eq(nil)
279
- end
280
-
281
- it "should support updating" do
282
- @klass.default_timeout 10
283
- expect(@klass.default_options[:timeout]).to eq(10)
284
- end
285
-
286
- it "should support floats" do
287
- @klass.default_timeout 0.5
288
- expect(@klass.default_options[:timeout]).to eq(0.5)
289
- end
290
- end
291
-
292
- describe "debug_output" do
293
- it "stores the given stream as a default_option" do
294
- @klass.debug_output $stdout
295
- expect(@klass.default_options[:debug_output]).to eq($stdout)
296
- end
297
-
298
- it "stores the $stderr stream by default" do
299
- @klass.debug_output
300
- expect(@klass.default_options[:debug_output]).to eq($stderr)
301
- end
302
- end
303
-
304
- describe "basic http authentication" do
305
- it "should work" do
306
- @klass.basic_auth 'foobar', 'secret'
307
- expect(@klass.default_options[:basic_auth]).to eq({username: 'foobar', password: 'secret'})
308
- end
309
- end
310
-
311
- describe "digest http authentication" do
312
- it "should work" do
313
- @klass.digest_auth 'foobar', 'secret'
314
- expect(@klass.default_options[:digest_auth]).to eq({username: 'foobar', password: 'secret'})
315
- end
316
- end
317
-
318
- describe "parser" do
319
- class CustomParser
320
- def self.parse(body)
321
- {sexy: true}
322
- end
323
- end
324
-
325
- let(:parser) do
326
- proc { |data, format| CustomParser.parse(data) }
327
- end
328
-
329
- it "should set parser options" do
330
- @klass.parser parser
331
- expect(@klass.default_options[:parser]).to eq(parser)
332
- end
333
-
334
- it "should be able parse response with custom parser" do
335
- @klass.parser parser
336
- FakeWeb.register_uri(:get, 'http://twitter.com/statuses/public_timeline.xml', body: 'tweets')
337
- custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
338
- expect(custom_parsed_response[:sexy]).to eq(true)
339
- end
340
-
341
- it "raises UnsupportedFormat when the parser cannot handle the format" do
342
- @klass.format :json
343
- class MyParser < HTTParty::Parser
344
- SupportedFormats = {}
345
- end unless defined?(MyParser)
346
- expect do
347
- @klass.parser MyParser
348
- end.to raise_error(HTTParty::UnsupportedFormat)
349
- end
350
-
351
- it 'does not validate format whe custom parser is a proc' do
352
- expect do
353
- @klass.format :json
354
- @klass.parser lambda {|body, format|}
355
- end.to_not raise_error
356
- end
357
- end
358
-
359
- describe "uri_adapter" do
360
-
361
- require 'forwardable'
362
- class CustomURIAdaptor
363
- extend Forwardable
364
- def_delegators :@uri, :userinfo, :relative?, :query, :query=, :scheme, :path, :host, :port
365
-
366
- def initialize uri
367
- @uri = uri
368
- end
369
-
370
- def self.parse uri
371
- new URI.parse uri
372
- end
373
- end
374
-
375
- let(:uri_adapter) { CustomURIAdaptor }
376
-
377
- it "should set the uri_adapter" do
378
- @klass.uri_adapter uri_adapter
379
- expect(@klass.default_options[:uri_adapter]).to be uri_adapter
380
- end
381
-
382
- it "should raise an ArgumentError if uri_adapter doesn't implement parse method" do
383
- expect do
384
- @klass.uri_adapter double()
385
- end.to raise_error(ArgumentError)
386
- end
387
-
388
-
389
- it "should process a request with a uri instance parsed from the uri_adapter" do
390
- uri = 'http://foo.com/bar'
391
- FakeWeb.register_uri(:get, uri, body: 'stuff')
392
- @klass.uri_adapter uri_adapter
393
- expect(@klass.get(uri).parsed_response).to eq('stuff')
394
- end
395
-
396
- end
397
-
398
- describe "connection_adapter" do
399
- let(:uri) { 'http://google.com/api.json' }
400
- let(:connection_adapter) { double('CustomConnectionAdapter') }
401
-
402
- it "should set the connection_adapter" do
403
- @klass.connection_adapter connection_adapter
404
- expect(@klass.default_options[:connection_adapter]).to be connection_adapter
405
- end
406
-
407
- it "should set the connection_adapter_options when provided" do
408
- options = {foo: :bar}
409
- @klass.connection_adapter connection_adapter, options
410
- expect(@klass.default_options[:connection_adapter_options]).to be options
411
- end
412
-
413
- it "should not set the connection_adapter_options when not provided" do
414
- @klass.connection_adapter connection_adapter
415
- expect(@klass.default_options[:connection_adapter_options]).to be_nil
416
- end
417
-
418
- it "should process a request with a connection from the adapter" do
419
- connection_adapter_options = {foo: :bar}
420
- expect(connection_adapter).to receive(:call) { |u, o|
421
- expect(o[:connection_adapter_options]).to eq(connection_adapter_options)
422
- HTTParty::ConnectionAdapter.call(u, o)
423
- }.with(URI.parse(uri), kind_of(Hash))
424
- FakeWeb.register_uri(:get, uri, body: 'stuff')
425
- @klass.connection_adapter connection_adapter, connection_adapter_options
426
- expect(@klass.get(uri).parsed_response).to eq('stuff')
427
- end
428
- end
429
-
430
- describe "format" do
431
- it "should allow xml" do
432
- @klass.format :xml
433
- expect(@klass.default_options[:format]).to eq(:xml)
434
- end
435
-
436
- it "should allow csv" do
437
- @klass.format :csv
438
- expect(@klass.default_options[:format]).to eq(:csv)
439
- end
440
-
441
- it "should allow json" do
442
- @klass.format :json
443
- expect(@klass.default_options[:format]).to eq(:json)
444
- end
445
-
446
- it "should allow plain" do
447
- @klass.format :plain
448
- expect(@klass.default_options[:format]).to eq(:plain)
449
- end
450
-
451
- it 'should not allow funky format' do
452
- expect do
453
- @klass.format :foobar
454
- end.to raise_error(HTTParty::UnsupportedFormat)
455
- end
456
-
457
- it 'should only print each format once with an exception' do
458
- expect do
459
- @klass.format :foobar
460
- end.to raise_error(HTTParty::UnsupportedFormat, "':foobar' Must be one of: csv, html, json, plain, xml")
461
- end
462
-
463
- it 'sets the default parser' do
464
- expect(@klass.default_options[:parser]).to be_nil
465
- @klass.format :json
466
- expect(@klass.default_options[:parser]).to eq(HTTParty::Parser)
467
- end
468
-
469
- it 'does not reset parser to the default parser' do
470
- my_parser = lambda {}
471
- @klass.parser my_parser
472
- @klass.format :json
473
- expect(@klass.parser).to eq(my_parser)
474
- end
475
- end
476
-
477
- describe "#no_follow" do
478
- it "sets no_follow to false by default" do
479
- @klass.no_follow
480
- expect(@klass.default_options[:no_follow]).to be_falsey
481
- end
482
-
483
- it "sets the no_follow option to true" do
484
- @klass.no_follow true
485
- expect(@klass.default_options[:no_follow]).to be_truthy
486
- end
487
- end
488
-
489
- describe "#maintain_method_across_redirects" do
490
- it "sets maintain_method_across_redirects to true by default" do
491
- @klass.maintain_method_across_redirects
492
- expect(@klass.default_options[:maintain_method_across_redirects]).to be_truthy
493
- end
494
-
495
- it "sets the maintain_method_across_redirects option to false" do
496
- @klass.maintain_method_across_redirects false
497
- expect(@klass.default_options[:maintain_method_across_redirects]).to be_falsey
498
- end
499
- end
500
-
501
- describe "#resend_on_redirect" do
502
- it "sets resend_on_redirect to true by default" do
503
- @klass.resend_on_redirect
504
- expect(@klass.default_options[:resend_on_redirect]).to be_truthy
505
- end
506
-
507
- it "sets resend_on_redirect option to false" do
508
- @klass.resend_on_redirect false
509
- expect(@klass.default_options[:resend_on_redirect]).to be_falsey
510
- end
511
- end
512
-
513
- describe ".follow_redirects" do
514
- it "sets follow redirects to true by default" do
515
- @klass.follow_redirects
516
- expect(@klass.default_options[:follow_redirects]).to be_truthy
517
- end
518
-
519
- it "sets the follow_redirects option to false" do
520
- @klass.follow_redirects false
521
- expect(@klass.default_options[:follow_redirects]).to be_falsey
522
- end
523
- end
524
-
525
- describe ".query_string_normalizer" do
526
- it "sets the query_string_normalizer option" do
527
- normalizer = proc {}
528
- @klass.query_string_normalizer normalizer
529
- expect(@klass.default_options[:query_string_normalizer]).to eq(normalizer)
530
- end
531
- end
532
-
533
- describe ".raise_on" do
534
- context 'when parameters is an array' do
535
- it 'sets raise_on option' do
536
- @klass.raise_on [500, 404]
537
- expect(@klass.default_options[:raise_on]).to contain_exactly(404, 500)
538
- end
539
- end
540
-
541
- context 'when parameters is a fixnum' do
542
- it 'sets raise_on option' do
543
- @klass.raise_on 404
544
- expect(@klass.default_options[:raise_on]).to contain_exactly(404)
545
- end
546
- end
547
- end
548
-
549
- describe "with explicit override of automatic redirect handling" do
550
- before do
551
- @request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', format: :xml, no_follow: true)
552
- @redirect = stub_response 'first redirect', 302
553
- @redirect['location'] = 'http://foo.com/bar'
554
- allow(HTTParty::Request).to receive_messages(new: @request)
555
- end
556
-
557
- it "should fail with redirected GET" do
558
- expect do
559
- @error = @klass.get('/foo', no_follow: true)
560
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
561
- end
562
-
563
- it "should fail with redirected POST" do
564
- expect do
565
- @klass.post('/foo', no_follow: true)
566
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
567
- end
568
-
569
- it "should fail with redirected PATCH" do
570
- expect do
571
- @klass.patch('/foo', no_follow: true)
572
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
573
- end
574
-
575
- it "should fail with redirected DELETE" do
576
- expect do
577
- @klass.delete('/foo', no_follow: true)
578
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
579
- end
580
-
581
- it "should fail with redirected MOVE" do
582
- expect do
583
- @klass.move('/foo', no_follow: true)
584
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
585
- end
586
-
587
- it "should fail with redirected COPY" do
588
- expect do
589
- @klass.copy('/foo', no_follow: true)
590
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
591
- end
592
-
593
- it "should fail with redirected PUT" do
594
- expect do
595
- @klass.put('/foo', no_follow: true)
596
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
597
- end
598
-
599
- it "should fail with redirected HEAD" do
600
- expect do
601
- @klass.head('/foo', no_follow: true)
602
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
603
- end
604
-
605
- it "should fail with redirected OPTIONS" do
606
- expect do
607
- @klass.options('/foo', no_follow: true)
608
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
609
- end
610
-
611
- it "should fail with redirected MKCOL" do
612
- expect do
613
- @klass.mkcol('/foo', no_follow: true)
614
- end.to raise_error(HTTParty::RedirectionTooDeep) {|e| expect(e.response.body).to eq('first redirect')}
615
- end
616
- end
617
-
618
- describe "head requests should follow redirects requesting HEAD only" do
619
- before do
620
- allow(HTTParty::Request).to receive(:new).
621
- and_return(double("mock response", perform: nil))
622
- end
623
-
624
- it "should remain HEAD request across redirects, unless specified otherwise" do
625
- expect(@klass).to receive(:ensure_method_maintained_across_redirects).with({})
626
- @klass.head('/foo')
627
- end
628
-
629
- end
630
-
631
- describe "#ensure_method_maintained_across_redirects" do
632
- it "should set maintain_method_across_redirects option if unspecified" do
633
- options = {}
634
- @klass.send(:ensure_method_maintained_across_redirects, options)
635
- expect(options[:maintain_method_across_redirects]).to be_truthy
636
- end
637
-
638
- it "should not set maintain_method_across_redirects option if value is present" do
639
- options = { maintain_method_across_redirects: false }
640
- @klass.send(:ensure_method_maintained_across_redirects, options)
641
- expect(options[:maintain_method_across_redirects]).to be_falsey
642
- end
643
- end
644
-
645
- describe "with multiple class definitions" do
646
- before(:each) do
647
- @klass.instance_eval do
648
- base_uri "http://first.com"
649
- default_params one: 1
650
- end
651
-
652
- @additional_klass = Class.new
653
- @additional_klass.instance_eval do
654
- include HTTParty
655
- base_uri "http://second.com"
656
- default_params two: 2
657
- end
658
- end
659
-
660
- it "should not run over each others options" do
661
- expect(@klass.default_options).to eq({ base_uri: 'http://first.com', default_params: { one: 1 } })
662
- expect(@additional_klass.default_options).to eq({ base_uri: 'http://second.com', default_params: { two: 2 } })
663
- end
664
- end
665
-
666
- describe "two child classes inheriting from one parent" do
667
- before(:each) do
668
- @parent = Class.new do
669
- include HTTParty
670
- def self.name
671
- "Parent"
672
- end
673
- end
674
-
675
- @child1 = Class.new(@parent)
676
- @child2 = Class.new(@parent)
677
- end
678
-
679
- it "does not modify each others inherited attributes" do
680
- @child1.default_params joe: "alive"
681
- @child2.default_params joe: "dead"
682
-
683
- expect(@child1.default_options).to eq({ default_params: {joe: "alive"} })
684
- expect(@child2.default_options).to eq({ default_params: {joe: "dead"} })
685
-
686
- expect(@parent.default_options).to eq({ })
687
- end
688
-
689
- it "inherits default_options from the superclass" do
690
- @parent.basic_auth 'user', 'password'
691
- expect(@child1.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
692
- @child1.basic_auth 'u', 'p' # modifying child1 has no effect on child2
693
- expect(@child2.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
694
- end
695
-
696
- it "doesn't modify the parent's default options" do
697
- @parent.basic_auth 'user', 'password'
698
-
699
- @child1.basic_auth 'u', 'p'
700
- expect(@child1.default_options).to eq({basic_auth: {username: 'u', password: 'p'}})
701
-
702
- @child1.basic_auth 'email', 'token'
703
- expect(@child1.default_options).to eq({basic_auth: {username: 'email', password: 'token'}})
704
-
705
- expect(@parent.default_options).to eq({basic_auth: {username: 'user', password: 'password'}})
706
- end
707
-
708
- it "doesn't modify hashes in the parent's default options" do
709
- @parent.headers 'Accept' => 'application/json'
710
- @child1.headers 'Accept' => 'application/xml'
711
-
712
- expect(@parent.default_options[:headers]).to eq({'Accept' => 'application/json'})
713
- expect(@child1.default_options[:headers]).to eq({'Accept' => 'application/xml'})
714
- end
715
-
716
- it "works with lambda values" do
717
- @child1.default_options[:imaginary_option] = lambda { "This is a new lambda "}
718
- expect(@child1.default_options[:imaginary_option]).to be_a Proc
719
- end
720
-
721
- it 'should dup the proc on the child class' do
722
- imaginary_option = lambda { 2 * 3.14 }
723
- @parent.default_options[:imaginary_option] = imaginary_option
724
- expect(@parent.default_options[:imaginary_option].call).to eq(imaginary_option.call)
725
- @child1.default_options[:imaginary_option]
726
- expect(@child1.default_options[:imaginary_option].call).to eq(imaginary_option.call)
727
- expect(@child1.default_options[:imaginary_option]).not_to be_equal imaginary_option
728
- end
729
-
730
- it "inherits default_cookies from the parent class" do
731
- @parent.cookies 'type' => 'chocolate_chip'
732
- expect(@child1.default_cookies).to eq({"type" => "chocolate_chip"})
733
- @child1.cookies 'type' => 'snickerdoodle'
734
- expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
735
- expect(@child2.default_cookies).to eq({"type" => "chocolate_chip"})
736
- end
737
-
738
- it "doesn't modify the parent's default cookies" do
739
- @parent.cookies 'type' => 'chocolate_chip'
740
-
741
- @child1.cookies 'type' => 'snickerdoodle'
742
- expect(@child1.default_cookies).to eq({"type" => "snickerdoodle"})
743
-
744
- expect(@parent.default_cookies).to eq({"type" => "chocolate_chip"})
745
- end
746
- end
747
-
748
- describe "grand parent with inherited callback" do
749
- before do
750
- @grand_parent = Class.new do
751
- def self.inherited(subclass)
752
- subclass.instance_variable_set(:@grand_parent, true)
753
- end
754
- end
755
- @parent = Class.new(@grand_parent) do
756
- include HTTParty
757
- end
758
- end
759
- it "continues running the #inherited on the parent" do
760
- child = Class.new(@parent)
761
- expect(child.instance_variable_get(:@grand_parent)).to be_truthy
762
- end
763
- end
764
-
765
- describe "#get" do
766
- it "should be able to get html" do
767
- stub_http_response_with('google.html')
768
- expect(HTTParty.get('http://www.google.com').parsed_response).to eq(file_fixture('google.html'))
769
- end
770
-
771
- it "should be able to get chunked html" do
772
- chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
773
- stub_chunked_http_response_with(chunks)
774
-
775
- expect(
776
- HTTParty.get('http://www.google.com') do |fragment|
777
- expect(chunks).to include(fragment)
778
- end.parsed_response
779
- ).to eq(chunks.join)
780
- end
781
-
782
- it "should return an empty body if stream_body option is turned on" do
783
- chunks = %w(Chunk1 Chunk2 Chunk3 Chunk4)
784
- options = {stream_body: true, format: 'html'}
785
- stub_chunked_http_response_with(chunks, options)
786
-
787
- expect(
788
- HTTParty.get('http://www.google.com', options) do |fragment|
789
- expect(chunks).to include(fragment)
790
- end.parsed_response
791
- ).to eq(nil)
792
- end
793
-
794
- it "should be able parse response type json automatically" do
795
- stub_http_response_with('twitter.json')
796
- tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
797
- expect(tweets.size).to eq(20)
798
- expect(tweets.first['user']).to eq({
799
- "name" => "Pyk",
800
- "url" => nil,
801
- "id" => "7694602",
802
- "description" => nil,
803
- "protected" => false,
804
- "screen_name" => "Pyk",
805
- "followers_count" => 1,
806
- "location" => "Opera Plaza, California",
807
- "profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
808
- })
809
- end
810
-
811
- it "should be able parse response type xml automatically" do
812
- stub_http_response_with('twitter.xml')
813
- tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
814
- expect(tweets['statuses'].size).to eq(20)
815
- expect(tweets['statuses'].first['user']).to eq({
816
- "name" => "Magic 8 Bot",
817
- "url" => nil,
818
- "id" => "17656026",
819
- "description" => "ask me a question",
820
- "protected" => "false",
821
- "screen_name" => "magic8bot",
822
- "followers_count" => "90",
823
- "profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
824
- "location" => nil
825
- })
826
- end
827
-
828
- it "should be able parse response type csv automatically" do
829
- stub_http_response_with('twitter.csv')
830
- profile = HTTParty.get('http://twitter.com/statuses/profile.csv')
831
- expect(profile.size).to eq(2)
832
- expect(profile[0]).to eq(%w(name url id description protected screen_name followers_count profile_image_url location))
833
- expect(profile[1]).to eq(["Magic 8 Bot", nil, "17656026", "ask me a question", "false", "magic8bot", "90", "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg", nil])
834
- end
835
-
836
- it "should not get undefined method add_node for nil class for the following xml" do
837
- stub_http_response_with('undefined_method_add_node_for_nil.xml')
838
- result = HTTParty.get('http://foobar.com')
839
- expect(result.parsed_response).to eq({"Entities" => {"href" => "https://s3-sandbox.parature.com/api/v1/5578/5633/Account", "results" => "0", "total" => "0", "page_size" => "25", "page" => "1"}})
840
- end
841
-
842
- it "should parse empty response fine" do
843
- stub_http_response_with('empty.xml')
844
- result = HTTParty.get('http://foobar.com')
845
- expect(result).to be_nil
846
- end
847
-
848
- it "should accept http URIs" do
849
- stub_http_response_with('google.html')
850
- expect do
851
- HTTParty.get('http://google.com')
852
- end.not_to raise_error
853
- end
854
-
855
- it "should accept https URIs" do
856
- stub_http_response_with('google.html')
857
- expect do
858
- HTTParty.get('https://google.com')
859
- end.not_to raise_error
860
- end
861
-
862
- it "should accept webcal URIs" do
863
- uri = 'http://google.com/'
864
- FakeWeb.register_uri(:get, uri, body: 'stuff')
865
- uri = 'webcal://google.com/'
866
- expect do
867
- HTTParty.get(uri)
868
- end.not_to raise_error
869
- end
870
-
871
- it "should raise an InvalidURIError on URIs that can't be parsed at all" do
872
- expect do
873
- HTTParty.get("It's the one that says 'Bad URI'")
874
- end.to raise_error(URI::InvalidURIError)
875
- end
876
- end
877
- end