faraday 1.0.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +104 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +3 -5
  5. data/examples/client_spec.rb +1 -1
  6. data/lib/faraday.rb +55 -41
  7. data/lib/faraday/adapter.rb +1 -8
  8. data/lib/faraday/autoload.rb +1 -6
  9. data/lib/faraday/connection.rb +11 -4
  10. data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
  11. data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
  12. data/lib/faraday/error.rb +20 -0
  13. data/lib/faraday/methods.rb +6 -0
  14. data/lib/faraday/middleware.rb +14 -4
  15. data/lib/faraday/options.rb +4 -8
  16. data/lib/faraday/options/proxy_options.rb +4 -0
  17. data/lib/faraday/rack_builder.rb +13 -12
  18. data/lib/faraday/request.rb +20 -10
  19. data/lib/faraday/request/multipart.rb +9 -2
  20. data/lib/faraday/request/retry.rb +2 -2
  21. data/lib/faraday/response.rb +0 -6
  22. data/lib/faraday/response/raise_error.rb +12 -1
  23. data/lib/faraday/utils.rb +2 -2
  24. data/lib/faraday/utils/headers.rb +2 -2
  25. data/lib/faraday/version.rb +5 -0
  26. data/spec/faraday/adapter/test_spec.rb +260 -0
  27. data/spec/faraday/connection_spec.rb +38 -0
  28. data/spec/faraday/error_spec.rb +15 -0
  29. data/spec/faraday/middleware_spec.rb +32 -6
  30. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  31. data/spec/faraday/params_encoders/flat_spec.rb +8 -0
  32. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  33. data/spec/faraday/rack_builder_spec.rb +149 -0
  34. data/spec/faraday/request/authorization_spec.rb +2 -2
  35. data/spec/faraday/request/multipart_spec.rb +41 -13
  36. data/spec/faraday/request/retry_spec.rb +1 -1
  37. data/spec/faraday/request_spec.rb +16 -5
  38. data/spec/faraday/response/raise_error_spec.rb +63 -0
  39. data/spec/support/shared_examples/adapter.rb +2 -1
  40. data/spec/support/shared_examples/request_method.rb +39 -11
  41. metadata +92 -13
  42. data/lib/faraday/adapter/em_http.rb +0 -286
  43. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  44. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  45. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  46. data/lib/faraday/adapter/excon.rb +0 -124
  47. data/lib/faraday/adapter/net_http.rb +0 -219
  48. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  49. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
@@ -18,6 +18,13 @@ shared_examples 'initializer with url' do
18
18
  it { expect(subject.path_prefix).to eq('/fish') }
19
19
  it { expect(subject.params).to eq('a' => '1') }
20
20
  end
21
+
22
+ context 'with IPv6 address' do
23
+ let(:address) { 'http://[::1]:85/' }
24
+
25
+ it { expect(subject.host).to eq('[::1]') }
26
+ it { expect(subject.port).to eq(85) }
27
+ end
21
28
  end
22
29
 
23
30
  shared_examples 'default connection options' do
@@ -270,6 +277,29 @@ RSpec.describe Faraday::Connection do
270
277
  expect(uri.to_s).to eq('http://sushi.com/sake/')
271
278
  end
272
279
  end
280
+
281
+ context 'with colon in path' do
282
+ let(:url) { 'http://service.com' }
283
+
284
+ it 'joins url to base when used absolute path' do
285
+ conn = Faraday.new(url: url)
286
+ uri = conn.build_exclusive_url('/service:search?limit=400')
287
+ expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
288
+ end
289
+
290
+ it 'joins url to base when used relative path' do
291
+ conn = Faraday.new(url: url)
292
+ uri = conn.build_exclusive_url('service:search?limit=400')
293
+ expect(uri.to_s).to eq('http://service.com/service%3Asearch?limit=400')
294
+ end
295
+
296
+ it 'joins url to base when used with path prefix' do
297
+ conn = Faraday.new(url: url)
298
+ conn.path_prefix = '/api'
299
+ uri = conn.build_exclusive_url('service:search?limit=400')
300
+ expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400')
301
+ end
302
+ end
273
303
  end
274
304
 
275
305
  describe '#build_url' do
@@ -412,6 +442,14 @@ RSpec.describe Faraday::Connection do
412
442
  end
413
443
  end
414
444
 
445
+ it 'allows when url in no proxy list with url_prefix' do
446
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
447
+ conn = Faraday::Connection.new
448
+ conn.url_prefix = 'http://example.com'
449
+ expect(conn.proxy).to be_nil
450
+ end
451
+ end
452
+
415
453
  it 'allows when prefixed url is not in no proxy list' do
416
454
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
417
455
  conn = Faraday::Connection.new('http://prefixedexample.com')
@@ -13,6 +13,7 @@ RSpec.describe Faraday::ClientError do
13
13
  it { expect(subject.message).to eq(exception.message) }
14
14
  it { expect(subject.backtrace).to eq(exception.backtrace) }
15
15
  it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
16
+ it { expect(subject.response_status).to be_nil }
16
17
  end
17
18
 
18
19
  context 'with response hash' do
@@ -22,6 +23,7 @@ RSpec.describe Faraday::ClientError do
22
23
  it { expect(subject.response).to eq(exception) }
23
24
  it { expect(subject.message).to eq('the server responded with status 400') }
24
25
  it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
26
+ it { expect(subject.response_status).to eq(400) }
25
27
  end
26
28
 
27
29
  context 'with string' do
@@ -31,6 +33,7 @@ RSpec.describe Faraday::ClientError do
31
33
  it { expect(subject.response).to be_nil }
32
34
  it { expect(subject.message).to eq('custom message') }
33
35
  it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
36
+ it { expect(subject.response_status).to be_nil }
34
37
  end
35
38
 
36
39
  context 'with anything else #to_s' do
@@ -40,6 +43,18 @@ RSpec.describe Faraday::ClientError do
40
43
  it { expect(subject.response).to be_nil }
41
44
  it { expect(subject.message).to eq('["error1", "error2"]') }
42
45
  it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
46
+ it { expect(subject.response_status).to be_nil }
47
+ end
48
+
49
+ context 'with exception string and response hash' do
50
+ let(:exception) { 'custom message' }
51
+ let(:response) { { status: 400 } }
52
+
53
+ it { expect(subject.wrapped_exception).to be_nil }
54
+ it { expect(subject.response).to eq(response) }
55
+ it { expect(subject.message).to eq('custom message') }
56
+ it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
57
+ it { expect(subject.response_status).to eq(400) }
43
58
  end
44
59
  end
45
60
  end
@@ -2,23 +2,49 @@
2
2
 
3
3
  RSpec.describe Faraday::Middleware do
4
4
  subject { described_class.new(app) }
5
+ let(:app) { double }
6
+
7
+ describe 'options' do
8
+ context 'when options are passed to the middleware' do
9
+ subject { described_class.new(app, options) }
10
+ let(:options) { { field: 'value' } }
11
+
12
+ it 'accepts options when initialized' do
13
+ expect(subject.options[:field]).to eq('value')
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '#on_request' do
19
+ subject do
20
+ Class.new(described_class) do
21
+ def on_request(env)
22
+ # do nothing
23
+ end
24
+ end.new(app)
25
+ end
26
+
27
+ it 'is called by #call' do
28
+ expect(app).to receive(:call).and_return(app)
29
+ expect(app).to receive(:on_complete)
30
+ is_expected.to receive(:call).and_call_original
31
+ is_expected.to receive(:on_request)
32
+ subject.call(double)
33
+ end
34
+ end
5
35
 
6
36
  describe '#close' do
7
37
  context "with app that doesn't support \#close" do
8
- let(:app) { double }
9
-
10
38
  it 'should issue warning' do
11
- expect(subject).to receive(:warn)
39
+ is_expected.to receive(:warn)
12
40
  subject.close
13
41
  end
14
42
  end
15
43
 
16
44
  context "with app that supports \#close" do
17
- let(:app) { double }
18
-
19
45
  it 'should issue warning' do
20
46
  expect(app).to receive(:close)
21
- expect(subject).to_not receive(:warn)
47
+ is_expected.to_not receive(:warn)
22
48
  subject.close
23
49
  end
24
50
  end
@@ -14,6 +14,13 @@ RSpec.describe Faraday::ProxyOptions do
14
14
  expect(options.inspect).to match('#<Faraday::ProxyOptions uri=')
15
15
  end
16
16
 
17
+ it 'defaults to http' do
18
+ options = Faraday::ProxyOptions.from 'example.org'
19
+ expect(options.port).to eq(80)
20
+ expect(options.host).to eq('example.org')
21
+ expect(options.scheme).to eq('http')
22
+ end
23
+
17
24
  it 'works with nil' do
18
25
  options = Faraday::ProxyOptions.from nil
19
26
  expect(options).to be_a_kind_of(Faraday::ProxyOptions)
@@ -31,4 +31,12 @@ RSpec.describe Faraday::FlatParamsEncoder do
31
31
  params = { a: [] }
32
32
  expect(subject.encode(params)).to eq('a=')
33
33
  end
34
+
35
+ it 'encodes unsorted when asked' do
36
+ params = { b: false, a: true }
37
+ expect(subject.encode(params)).to eq('a=true&b=false')
38
+ Faraday::FlatParamsEncoder.sort_params = false
39
+ expect(subject.encode(params)).to eq('b=false&a=true')
40
+ Faraday::FlatParamsEncoder.sort_params = true
41
+ end
34
42
  end
@@ -94,6 +94,14 @@ RSpec.describe Faraday::NestedParamsEncoder do
94
94
  expect(subject.encode(params)).to eq('a%5B%5D=true&a%5B%5D=false')
95
95
  end
96
96
 
97
+ it 'encodes unsorted when asked' do
98
+ params = { b: false, a: true }
99
+ expect(subject.encode(params)).to eq('a=true&b=false')
100
+ Faraday::NestedParamsEncoder.sort_params = false
101
+ expect(subject.encode(params)).to eq('b=false&a=true')
102
+ Faraday::NestedParamsEncoder.sort_params = true
103
+ end
104
+
97
105
  shared_examples 'a wrong decoding' do
98
106
  it do
99
107
  expect { subject.decode(query) }.to raise_error(TypeError) do |e|
@@ -193,4 +193,153 @@ RSpec.describe Faraday::RackBuilder do
193
193
  end
194
194
  end
195
195
  end
196
+
197
+ context 'when middleware is added with named arguments' do
198
+ let(:conn) { Faraday::Connection.new {} }
199
+
200
+ let(:dog_middleware) do
201
+ Class.new(Faraday::Middleware) do
202
+ attr_accessor :name
203
+
204
+ def initialize(app, name:)
205
+ super(app)
206
+ @name = name
207
+ end
208
+ end
209
+ end
210
+ let(:dog) do
211
+ subject.handlers.find { |handler| handler == dog_middleware }.build
212
+ end
213
+
214
+ it 'adds a handler to construct middleware with options passed to use' do
215
+ subject.use dog_middleware, name: 'Rex'
216
+ expect { dog }.to_not output(
217
+ /warning: Using the last argument as keyword parameters is deprecated/
218
+ ).to_stderr
219
+ expect(dog.name).to eq('Rex')
220
+ end
221
+ end
222
+
223
+ context 'when a request adapter is added with named arguments' do
224
+ let(:conn) { Faraday::Connection.new {} }
225
+
226
+ let(:cat_request) do
227
+ Class.new(Faraday::Middleware) do
228
+ attr_accessor :name
229
+
230
+ def initialize(app, name:)
231
+ super(app)
232
+ @name = name
233
+ end
234
+ end
235
+ end
236
+ let(:cat) do
237
+ subject.handlers.find { |handler| handler == cat_request }.build
238
+ end
239
+
240
+ it 'adds a handler to construct request adapter with options passed to request' do
241
+ Faraday::Request.register_middleware cat_request: cat_request
242
+ subject.request :cat_request, name: 'Felix'
243
+ expect { cat }.to_not output(
244
+ /warning: Using the last argument as keyword parameters is deprecated/
245
+ ).to_stderr
246
+ expect(cat.name).to eq('Felix')
247
+ end
248
+ end
249
+
250
+ context 'when a response adapter is added with named arguments' do
251
+ let(:conn) { Faraday::Connection.new {} }
252
+
253
+ let(:fish_response) do
254
+ Class.new(Faraday::Response::Middleware) do
255
+ attr_accessor :name
256
+
257
+ def initialize(app, name:)
258
+ super(app)
259
+ @name = name
260
+ end
261
+ end
262
+ end
263
+ let(:fish) do
264
+ subject.handlers.find { |handler| handler == fish_response }.build
265
+ end
266
+
267
+ it 'adds a handler to construct response adapter with options passed to response' do
268
+ Faraday::Response.register_middleware fish_response: fish_response
269
+ subject.response :fish_response, name: 'Bubbles'
270
+ expect { fish }.to_not output(
271
+ /warning: Using the last argument as keyword parameters is deprecated/
272
+ ).to_stderr
273
+ expect(fish.name).to eq('Bubbles')
274
+ end
275
+ end
276
+
277
+ context 'when a plain adapter is added with named arguments' do
278
+ let(:conn) { Faraday::Connection.new {} }
279
+
280
+ let(:rabbit_adapter) do
281
+ Class.new(Faraday::Adapter) do
282
+ attr_accessor :name
283
+
284
+ def initialize(app, name:)
285
+ super(app)
286
+ @name = name
287
+ end
288
+ end
289
+ end
290
+ let(:rabbit) do
291
+ subject.adapter.build
292
+ end
293
+
294
+ it 'adds a handler to construct adapter with options passed to adapter' do
295
+ Faraday::Adapter.register_middleware rabbit_adapter: rabbit_adapter
296
+ subject.adapter :rabbit_adapter, name: 'Thumper'
297
+ expect { rabbit }.to_not output(
298
+ /warning: Using the last argument as keyword parameters is deprecated/
299
+ ).to_stderr
300
+ expect(rabbit.name).to eq('Thumper')
301
+ end
302
+ end
303
+
304
+ context 'when handlers are directly added or updated' do
305
+ let(:conn) { Faraday::Connection.new {} }
306
+
307
+ let(:rock_handler) do
308
+ Class.new do
309
+ attr_accessor :name
310
+
311
+ def initialize(_app, name:)
312
+ @name = name
313
+ end
314
+ end
315
+ end
316
+ let(:rock) do
317
+ subject.handlers.find { |handler| handler == rock_handler }.build
318
+ end
319
+
320
+ it 'adds a handler to construct adapter with options passed to insert' do
321
+ subject.insert 0, rock_handler, name: 'Stony'
322
+ expect { rock }.to_not output(
323
+ /warning: Using the last argument as keyword parameters is deprecated/
324
+ ).to_stderr
325
+ expect(rock.name).to eq('Stony')
326
+ end
327
+
328
+ it 'adds a handler with options passed to insert_after' do
329
+ subject.insert_after 0, rock_handler, name: 'Rocky'
330
+ expect { rock }.to_not output(
331
+ /warning: Using the last argument as keyword parameters is deprecated/
332
+ ).to_stderr
333
+ expect(rock.name).to eq('Rocky')
334
+ end
335
+
336
+ it 'adds a handler with options passed to swap' do
337
+ subject.insert 0, rock_handler, name: 'Flint'
338
+ subject.swap 0, rock_handler, name: 'Chert'
339
+ expect { rock }.to_not output(
340
+ /warning: Using the last argument as keyword parameters is deprecated/
341
+ ).to_stderr
342
+ expect(rock.name).to eq('Chert')
343
+ end
344
+ end
196
345
  end
@@ -56,7 +56,7 @@ RSpec.describe Faraday::Request::Authorization do
56
56
  end
57
57
 
58
58
  context 'when other values are provided' do
59
- let(:auth_config) { ['baz', foo: 42] }
59
+ let(:auth_config) { ['baz', { foo: 42 }] }
60
60
 
61
61
  it { expect(response.body).to match(/^Token /) }
62
62
  it { expect(response.body).to match(/token="baz"/) }
@@ -78,7 +78,7 @@ RSpec.describe Faraday::Request::Authorization do
78
78
  end
79
79
 
80
80
  context 'when passed a string and a hash' do
81
- let(:auth_config) { ['baz', foo: 42] }
81
+ let(:auth_config) { ['baz', { foo: 42 }] }
82
82
 
83
83
  it { expect(response.body).to eq('baz foo="42"') }
84
84
 
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe Faraday::Request::Multipart do
4
+ let(:options) { {} }
4
5
  let(:conn) do
5
6
  Faraday.new do |b|
6
- b.request :multipart
7
+ b.request :multipart, options
7
8
  b.request :url_encoded
8
9
  b.adapter :test do |stub|
9
10
  stub.post('/echo') do |env|
@@ -54,9 +55,10 @@ RSpec.describe Faraday::Request::Multipart do
54
55
  part_bc, body_bc = result.part('b[c]')
55
56
  expect(part_bc).to_not be_nil
56
57
  expect(part_bc.filename).to eq('multipart_spec.rb')
57
- expect(part_bc.headers['content-disposition']).to eq(
58
- 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
59
- )
58
+ expect(part_bc.headers['content-disposition'])
59
+ .to eq(
60
+ 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
61
+ )
60
62
  expect(part_bc.headers['content-type']).to eq('text/x-ruby')
61
63
  expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
62
64
  expect(body_bc).to eq(File.read(__FILE__))
@@ -135,9 +137,10 @@ RSpec.describe Faraday::Request::Multipart do
135
137
  part_bc, body_bc = result.part('b[][c]')
136
138
  expect(part_bc).to_not be_nil
137
139
  expect(part_bc.filename).to eq('multipart_spec.rb')
138
- expect(part_bc.headers['content-disposition']).to eq(
139
- 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
140
- )
140
+ expect(part_bc.headers['content-disposition'])
141
+ .to eq(
142
+ 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
143
+ )
141
144
  expect(part_bc.headers['content-type']).to eq('text/x-ruby')
142
145
  expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
143
146
  expect(body_bc).to eq(File.read(__FILE__))
@@ -177,9 +180,10 @@ RSpec.describe Faraday::Request::Multipart do
177
180
  part_bc, body_bc = result.part('b[c]')
178
181
  expect(part_bc).to_not be_nil
179
182
  expect(part_bc.filename).to eq('multipart_spec.rb')
180
- expect(part_bc.headers['content-disposition']).to eq(
181
- 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
182
- )
183
+ expect(part_bc.headers['content-disposition'])
184
+ .to eq(
185
+ 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
186
+ )
183
187
  expect(part_bc.headers['content-type']).to eq('text/x-ruby')
184
188
  expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
185
189
  expect(body_bc).to eq(File.read(__FILE__))
@@ -258,9 +262,10 @@ RSpec.describe Faraday::Request::Multipart do
258
262
  part_bc, body_bc = result.part('b[][c]')
259
263
  expect(part_bc).to_not be_nil
260
264
  expect(part_bc.filename).to eq('multipart_spec.rb')
261
- expect(part_bc.headers['content-disposition']).to eq(
262
- 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
263
- )
265
+ expect(part_bc.headers['content-disposition'])
266
+ .to eq(
267
+ 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
268
+ )
264
269
  expect(part_bc.headers['content-type']).to eq('text/x-ruby')
265
270
  expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
266
271
  expect(body_bc).to eq(File.read(__FILE__))
@@ -271,4 +276,27 @@ RSpec.describe Faraday::Request::Multipart do
271
276
  expect(body_bd).to eq('2')
272
277
  end
273
278
  end
279
+
280
+ context 'when passing flat_encode=true option' do
281
+ let(:options) { { flat_encode: true } }
282
+ let(:io) { StringIO.new('io-content') }
283
+ let(:payload) do
284
+ {
285
+ a: 1,
286
+ b: [
287
+ Faraday::UploadIO.new(io, 'application/pdf'),
288
+ Faraday::UploadIO.new(io, 'application/pdf')
289
+ ]
290
+ }
291
+ end
292
+
293
+ it_behaves_like 'a multipart request'
294
+
295
+ it 'encode params using flat encoder' do
296
+ response = conn.post('/echo', payload)
297
+
298
+ expect(response.body).to include('name="b"')
299
+ expect(response.body).not_to include('name="b[]"')
300
+ end
301
+ end
274
302
  end