faraday 1.4.1 → 2.7.4

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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +197 -3
  3. data/LICENSE.md +1 -1
  4. data/README.md +18 -16
  5. data/examples/client_spec.rb +67 -13
  6. data/examples/client_test.rb +80 -15
  7. data/lib/faraday/adapter/test.rb +117 -52
  8. data/lib/faraday/adapter.rb +5 -14
  9. data/lib/faraday/connection.rb +64 -119
  10. data/lib/faraday/encoders/nested_params_encoder.rb +13 -6
  11. data/lib/faraday/error.rb +3 -8
  12. data/lib/faraday/logging/formatter.rb +19 -2
  13. data/lib/faraday/middleware.rb +3 -1
  14. data/lib/faraday/middleware_registry.rb +17 -63
  15. data/lib/faraday/options/env.rb +31 -7
  16. data/lib/faraday/options/proxy_options.rb +4 -0
  17. data/lib/faraday/options/ssl_options.rb +11 -1
  18. data/lib/faraday/options.rb +3 -3
  19. data/lib/faraday/rack_builder.rb +23 -20
  20. data/lib/faraday/request/authorization.rb +37 -38
  21. data/lib/faraday/request/instrumentation.rb +2 -0
  22. data/lib/faraday/request/json.rb +55 -0
  23. data/lib/faraday/request/url_encoded.rb +5 -1
  24. data/lib/faraday/request.rb +12 -32
  25. data/lib/faraday/response/json.rb +54 -0
  26. data/lib/faraday/response/logger.rb +8 -4
  27. data/lib/faraday/response/raise_error.rb +9 -1
  28. data/lib/faraday/response.rb +10 -20
  29. data/lib/faraday/utils/headers.rb +7 -2
  30. data/lib/faraday/utils.rb +10 -5
  31. data/lib/faraday/version.rb +1 -1
  32. data/lib/faraday.rb +10 -31
  33. data/spec/faraday/adapter/test_spec.rb +182 -0
  34. data/spec/faraday/connection_spec.rb +177 -90
  35. data/spec/faraday/middleware_registry_spec.rb +31 -0
  36. data/spec/faraday/middleware_spec.rb +18 -0
  37. data/spec/faraday/options/env_spec.rb +8 -2
  38. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  39. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  40. data/spec/faraday/rack_builder_spec.rb +26 -54
  41. data/spec/faraday/request/authorization_spec.rb +54 -24
  42. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  43. data/spec/faraday/request/json_spec.rb +111 -0
  44. data/spec/faraday/request/url_encoded_spec.rb +12 -2
  45. data/spec/faraday/request_spec.rb +5 -15
  46. data/spec/faraday/response/json_spec.rb +117 -0
  47. data/spec/faraday/response/logger_spec.rb +28 -0
  48. data/spec/faraday/response/raise_error_spec.rb +7 -4
  49. data/spec/faraday/response_spec.rb +3 -1
  50. data/spec/faraday/utils/headers_spec.rb +22 -4
  51. data/spec/faraday/utils_spec.rb +63 -1
  52. data/spec/support/fake_safe_buffer.rb +1 -1
  53. data/spec/support/helper_methods.rb +0 -37
  54. data/spec/support/shared_examples/adapter.rb +2 -2
  55. data/spec/support/shared_examples/request_method.rb +22 -21
  56. metadata +14 -80
  57. data/lib/faraday/adapter/em_http.rb +0 -289
  58. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  59. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  60. data/lib/faraday/adapter/em_synchrony.rb +0 -153
  61. data/lib/faraday/adapter/httpclient.rb +0 -152
  62. data/lib/faraday/adapter/patron.rb +0 -132
  63. data/lib/faraday/adapter/rack.rb +0 -75
  64. data/lib/faraday/adapter/typhoeus.rb +0 -15
  65. data/lib/faraday/autoload.rb +0 -92
  66. data/lib/faraday/dependency_loader.rb +0 -37
  67. data/lib/faraday/file_part.rb +0 -128
  68. data/lib/faraday/param_part.rb +0 -53
  69. data/lib/faraday/request/basic_authentication.rb +0 -20
  70. data/lib/faraday/request/multipart.rb +0 -106
  71. data/lib/faraday/request/retry.rb +0 -239
  72. data/lib/faraday/request/token_authentication.rb +0 -20
  73. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  74. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  75. data/spec/faraday/adapter/excon_spec.rb +0 -49
  76. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  77. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  78. data/spec/faraday/adapter/patron_spec.rb +0 -18
  79. data/spec/faraday/adapter/rack_spec.rb +0 -8
  80. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  81. data/spec/faraday/composite_read_io_spec.rb +0 -80
  82. data/spec/faraday/request/multipart_spec.rb +0 -302
  83. data/spec/faraday/request/retry_spec.rb +0 -242
  84. data/spec/faraday/response/middleware_spec.rb +0 -68
  85. data/spec/support/webmock_rack_app.rb +0 -68
data/lib/faraday/utils.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'base64'
4
+ require 'uri'
3
5
  require 'faraday/utils/headers'
4
6
  require 'faraday/utils/params_hash'
5
7
 
@@ -51,6 +53,12 @@ module Faraday
51
53
  @default_params_encoder ||= NestedParamsEncoder
52
54
  end
53
55
 
56
+ def basic_header_from(login, pass)
57
+ value = Base64.encode64("#{login}:#{pass}")
58
+ value.delete!("\n")
59
+ "Basic #{value}"
60
+ end
61
+
54
62
  class << self
55
63
  attr_writer :default_params_encoder
56
64
  end
@@ -71,10 +79,7 @@ module Faraday
71
79
  end
72
80
 
73
81
  def default_uri_parser
74
- @default_uri_parser ||= begin
75
- require 'uri'
76
- Kernel.method(:URI)
77
- end
82
+ @default_uri_parser ||= Kernel.method(:URI)
78
83
  end
79
84
 
80
85
  def default_uri_parser=(parser)
@@ -96,7 +101,7 @@ module Faraday
96
101
  # Recursive hash update
97
102
  def deep_merge!(target, hash)
98
103
  hash.each do |key, value|
99
- target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
104
+ target[key] = if value.is_a?(Hash) && (target[key].is_a?(Hash) || target[key].is_a?(Options))
100
105
  deep_merge(target[key], value)
101
106
  else
102
107
  value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '1.4.1'
4
+ VERSION = '2.7.4'
5
5
  end
data/lib/faraday.rb CHANGED
@@ -4,16 +4,10 @@ require 'cgi'
4
4
  require 'date'
5
5
  require 'set'
6
6
  require 'forwardable'
7
- require 'faraday/middleware_registry'
8
- require 'faraday/dependency_loader'
9
-
10
- unless defined?(::Faraday::Timer)
11
- require 'timeout'
12
- ::Faraday::Timer = Timeout
13
- end
14
-
15
7
  require 'faraday/version'
16
8
  require 'faraday/methods'
9
+ require 'faraday/error'
10
+ require 'faraday/middleware_registry'
17
11
  require 'faraday/utils'
18
12
  require 'faraday/options'
19
13
  require 'faraday/connection'
@@ -23,14 +17,7 @@ require 'faraday/middleware'
23
17
  require 'faraday/adapter'
24
18
  require 'faraday/request'
25
19
  require 'faraday/response'
26
- require 'faraday/error'
27
- require 'faraday/file_part'
28
- require 'faraday/param_part'
29
-
30
20
  require 'faraday/net_http'
31
- require 'faraday/net_http_persistent'
32
- require 'faraday/excon'
33
-
34
21
  # This is the main namespace for Faraday.
35
22
  #
36
23
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -44,6 +31,8 @@ require 'faraday/excon'
44
31
  # conn.get '/'
45
32
  #
46
33
  module Faraday
34
+ CONTENT_TYPE = 'Content-Type'
35
+
47
36
  class << self
48
37
  # The root path that Faraday is being loaded from.
49
38
  #
@@ -65,6 +54,10 @@ module Faraday
65
54
  # @return [Symbol] the new default_adapter.
66
55
  attr_reader :default_adapter
67
56
 
57
+ # Option for the default_adapter
58
+ # @return [Hash] default_adapter options
59
+ attr_accessor :default_adapter_options
60
+
68
61
  # Documented below, see default_connection
69
62
  attr_writer :default_connection
70
63
 
@@ -101,23 +94,10 @@ module Faraday
101
94
  # params: { page: 1 }
102
95
  # # => Faraday::Connection to http://faraday.com?page=1
103
96
  def new(url = nil, options = {}, &block)
104
- options = default_connection_options.merge(options)
97
+ options = Utils.deep_merge(default_connection_options, options)
105
98
  Faraday::Connection.new(url, options, &block)
106
99
  end
107
100
 
108
- # @private
109
- # Internal: Requires internal Faraday libraries.
110
- #
111
- # @param libs [Array] one or more relative String names to Faraday classes.
112
- # @return [void]
113
- def require_libs(*libs)
114
- libs.each do |lib|
115
- require "#{lib_path}/#{lib}"
116
- end
117
- end
118
-
119
- alias require_lib require_libs
120
-
121
101
  # Documented elsewhere, see default_adapter reader
122
102
  def default_adapter=(adapter)
123
103
  @default_connection = nil
@@ -173,6 +153,5 @@ module Faraday
173
153
  self.root_path = File.expand_path __dir__
174
154
  self.lib_path = File.expand_path 'faraday', __dir__
175
155
  self.default_adapter = :net_http
176
-
177
- require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
156
+ self.default_adapter_options = {}
178
157
  end
@@ -257,4 +257,186 @@ RSpec.describe Faraday::Adapter::Test do
257
257
  it { expect { request }.to raise_error described_class::Stubs::NotFound }
258
258
  end
259
259
  end
260
+
261
+ describe 'for request with non default params encoder' do
262
+ let(:connection) do
263
+ Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
264
+ builder.adapter :test, stubs
265
+ end
266
+ end
267
+ let(:stubs) do
268
+ described_class::Stubs.new do |stubs|
269
+ stubs.get('/path?a=x&a=y&a=z') { [200, {}, 'a'] }
270
+ end
271
+ end
272
+
273
+ context 'when all flat param values are correctly set' do
274
+ subject(:request) { connection.get('/path?a=x&a=y&a=z') }
275
+
276
+ it { expect(request.status).to eq 200 }
277
+ end
278
+
279
+ shared_examples 'raise NotFound when params do not satisfy the flat param values' do |params|
280
+ subject(:request) { connection.get('/path', params) }
281
+
282
+ context "with #{params.inspect}" do
283
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
284
+ end
285
+ end
286
+
287
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x] }
288
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x y] }
289
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x z y] } # NOTE: The order of the value is also compared.
290
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { b: %w[x y z] }
291
+ end
292
+
293
+ describe 'strict_mode' do
294
+ let(:stubs) do
295
+ described_class::Stubs.new(strict_mode: true) do |stubs|
296
+ stubs.get('/strict?a=12&b=xy', 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello') { [200, {}, 'a'] }
297
+ stubs.get('/with_user_agent?a=12&b=xy', authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent') { [200, {}, 'a'] }
298
+ end
299
+ end
300
+
301
+ context 'when params and headers are exactly set' do
302
+ subject(:request) { connection.get('/strict', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', x_c: 'hello' }) }
303
+
304
+ it { expect(request.status).to eq 200 }
305
+ end
306
+
307
+ context 'when params and headers are exactly set with a custom user agent' do
308
+ subject(:request) { connection.get('/with_user_agent', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent' }) }
309
+
310
+ it { expect(request.status).to eq 200 }
311
+ end
312
+
313
+ shared_examples 'raise NotFound when params do not satisfy the strict check' do |params|
314
+ subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
315
+
316
+ context "with #{params.inspect}" do
317
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
318
+ end
319
+ end
320
+
321
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12' }
322
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { b: 'xy' }
323
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '123', b: 'xy' }
324
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xyz' }
325
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
326
+ it_behaves_like 'raise NotFound when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
327
+
328
+ shared_examples 'raise NotFound when headers do not satisfy the strict check' do |path, headers|
329
+ subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
330
+
331
+ context "with #{headers.inspect}" do
332
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
333
+ end
334
+ end
335
+
336
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck' }
337
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { 'X-C' => 'hello' }
338
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'Hi' }
339
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Basic m_ck', 'x-c': 'hello' }
340
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
341
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck' }
342
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'Unknown' }
343
+ it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
344
+
345
+ context 'when strict_mode is disabled' do
346
+ before do
347
+ stubs.strict_mode = false
348
+ end
349
+
350
+ shared_examples 'does not raise NotFound even when params do not satisfy the strict check' do |params|
351
+ subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
352
+
353
+ context "with #{params.inspect}" do
354
+ it { expect(request.status).to eq 200 }
355
+ end
356
+ end
357
+
358
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy' }
359
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
360
+ it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
361
+
362
+ shared_examples 'does not raise NotFound even when headers do not satisfy the strict check' do |path, headers|
363
+ subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
364
+
365
+ context "with #{headers.inspect}" do
366
+ it { expect(request.status).to eq 200 }
367
+ end
368
+ end
369
+
370
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello' }
371
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
372
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', user_agent: 'Special Agent' }
373
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent' }
374
+ it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
375
+ end
376
+
377
+ describe 'body_match?' do
378
+ let(:stubs) do
379
+ described_class::Stubs.new do |stubs|
380
+ stubs.post('/no_check') { [200, {}, 'ok'] }
381
+ stubs.post('/with_string', 'abc') { [200, {}, 'ok'] }
382
+ stubs.post(
383
+ '/with_proc',
384
+ ->(request_body) { JSON.parse(request_body, symbolize_names: true) == { x: '!', a: [{ m: [{ a: true }], n: 123 }] } },
385
+ { content_type: 'application/json' }
386
+ ) do
387
+ [200, {}, 'ok']
388
+ end
389
+ end
390
+ end
391
+
392
+ context 'when trying without any args for body' do
393
+ subject(:without_body) { connection.post('/no_check') }
394
+
395
+ it { expect(without_body.status).to eq 200 }
396
+ end
397
+
398
+ context 'when trying with string body stubs' do
399
+ subject(:with_string) { connection.post('/with_string', 'abc') }
400
+
401
+ it { expect(with_string.status).to eq 200 }
402
+ end
403
+
404
+ context 'when trying with proc body stubs' do
405
+ subject(:with_proc) do
406
+ connection.post('/with_proc', JSON.dump(a: [{ n: 123, m: [{ a: true }] }], x: '!'), { 'Content-Type' => 'application/json' })
407
+ end
408
+
409
+ it { expect(with_proc.status).to eq 200 }
410
+ end
411
+ end
412
+ end
413
+
414
+ describe 'request timeout' do
415
+ subject(:request) do
416
+ connection.get('/sleep') do |req|
417
+ req.options.timeout = timeout
418
+ end
419
+ end
420
+
421
+ before do
422
+ stubs.get('/sleep') do
423
+ sleep(0.01)
424
+ [200, {}, '']
425
+ end
426
+ end
427
+
428
+ context 'when request is within timeout' do
429
+ let(:timeout) { 1 }
430
+
431
+ it { expect(request.status).to eq 200 }
432
+ end
433
+
434
+ context 'when request is too slow' do
435
+ let(:timeout) { 0.001 }
436
+
437
+ it 'raises an exception' do
438
+ expect { request }.to raise_error(Faraday::TimeoutError)
439
+ end
440
+ end
441
+ end
260
442
  end