faraday 2.4.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b9b61d836844815a4d9a5a1e9986c9a60416b8a9e9159d61f959e0c74ed52cc
4
- data.tar.gz: 34fb0b4f2d4a8b22a72d56d0f3d4bfa884b2290c5012b1cdfe6de4b6886420a6
3
+ metadata.gz: d9ba15c64f037b4f2c0194ae0d6340af38e79ded9d85b37a36f2ca3cde44d2f0
4
+ data.tar.gz: b4ae3fe514db4cba5db21446e4b50d2d8b58ae3f3c077c7e288b4eb0a64637e1
5
5
  SHA512:
6
- metadata.gz: 9f0db04b8a845215b109b3491985dc6ce6ac60ced07874bcdcdd5bab5d935a40e51bda4feab6f93375889597185c295785e5a6d1119576762d006ca696033794
7
- data.tar.gz: 45debf037f0d1d02cccc70e33042c68d76146bb6b58bc8de839a7745404cd58a4171b0e209c75b1384475f6432531317a9145959ebf3ffe60837b65469f20e2b
6
+ metadata.gz: 20feaa462add0a309d25ceefbd6e5c4481f08a0e1f201d15a2f7459ea83611363002bfc40ba35b143b99d9ef29455edf10e40557455f2d7b057a8440d2f4e3ba
7
+ data.tar.gz: f82680b983f1059eba7191b4ac7f17bc21f8b33736c6e1130c684c695d32fae3f3f278ea7e7b13adb92f89208befaf35fe7dc485dc5e5eb3f582943bde5a2b37
@@ -272,7 +272,7 @@ module Faraday
272
272
  stub, meta = stubs.match(env)
273
273
 
274
274
  unless stub
275
- raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
275
+ raise Stubs::NotFound, "no stubbed request for #{env[:method]} " \
276
276
  "#{env[:url]} #{env[:body]}"
277
277
  end
278
278
 
@@ -283,7 +283,11 @@ module Faraday
283
283
  else
284
284
  stub.block.call(env, meta)
285
285
  end
286
- save_response(env, status, body, headers)
286
+
287
+ # We need to explicitly pass `reason_phrase = nil` here to avoid keyword args conflicts.
288
+ # See https://github.com/lostisland/faraday/issues/1444
289
+ # TODO: remove `nil` explicit reason_phrase once Ruby 3.0 becomes minimum req. version
290
+ save_response(env, status, body, headers, nil)
287
291
 
288
292
  @app.call(env)
289
293
  end
@@ -59,7 +59,7 @@ module Faraday
59
59
 
60
60
  private
61
61
 
62
- def save_response(env, status, body, headers = nil, reason_phrase = nil)
62
+ def save_response(env, status, body, headers = nil, reason_phrase = nil, finished: true)
63
63
  env.status = status
64
64
  env.body = body
65
65
  env.reason_phrase = reason_phrase&.to_s&.strip
@@ -68,7 +68,7 @@ module Faraday
68
68
  yield(response_headers) if block_given?
69
69
  end
70
70
 
71
- env.response.finish(env) unless env.parallel?
71
+ env.response.finish(env) unless env.parallel? || !finished
72
72
  env.response
73
73
  end
74
74
 
@@ -130,10 +130,10 @@ module Faraday
130
130
  # Makes a GET HTTP request without a body.
131
131
  # @!scope class
132
132
  #
133
- # @param url [String] The optional String base URL to use as a prefix for
133
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
134
134
  # all requests. Can also be the options Hash.
135
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
136
- # @param headers [Hash] unencoded HTTP header key/value pairs.
135
+ # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
136
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
137
137
  #
138
138
  # @example
139
139
  # conn.get '/items', { page: 1 }, :accept => 'application/json'
@@ -152,10 +152,10 @@ module Faraday
152
152
  # Makes a HEAD HTTP request without a body.
153
153
  # @!scope class
154
154
  #
155
- # @param url [String] The optional String base URL to use as a prefix for
155
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
156
156
  # all requests. Can also be the options Hash.
157
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
158
- # @param headers [Hash] unencoded HTTP header key/value pairs.
157
+ # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
158
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
159
159
  #
160
160
  # @example
161
161
  # conn.head '/items/1'
@@ -167,10 +167,10 @@ module Faraday
167
167
  # Makes a DELETE HTTP request without a body.
168
168
  # @!scope class
169
169
  #
170
- # @param url [String] The optional String base URL to use as a prefix for
170
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
171
171
  # all requests. Can also be the options Hash.
172
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
173
- # @param headers [Hash] unencoded HTTP header key/value pairs.
172
+ # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
173
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
174
174
  #
175
175
  # @example
176
176
  # conn.delete '/items/1'
@@ -182,10 +182,10 @@ module Faraday
182
182
  # Makes a TRACE HTTP request without a body.
183
183
  # @!scope class
184
184
  #
185
- # @param url [String] The optional String base URL to use as a prefix for
185
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
186
186
  # all requests. Can also be the options Hash.
187
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
188
- # @param headers [Hash] unencoded HTTP header key/value pairs.
187
+ # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
188
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
189
189
  #
190
190
  # @example
191
191
  # conn.connect '/items/1'
@@ -210,9 +210,9 @@ module Faraday
210
210
  #
211
211
  # @overload options(url, params = nil, headers = nil)
212
212
  # Makes an OPTIONS HTTP request to the given URL.
213
- # @param url [String] String base URL to sue as a prefix for all requests.
214
- # @param params [Hash] Hash of URI query unencoded key/value pairs.
215
- # @param headers [Hash] unencoded HTTP header key/value pairs.
213
+ # @param url [String, URI, nil] String base URL to sue as a prefix for all requests.
214
+ # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
215
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
216
216
  #
217
217
  # @example
218
218
  # conn.options '/items/1'
@@ -233,10 +233,10 @@ module Faraday
233
233
  # Makes a POST HTTP request with a body.
234
234
  # @!scope class
235
235
  #
236
- # @param url [String] The optional String base URL to use as a prefix for
236
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
237
237
  # all requests. Can also be the options Hash.
238
- # @param body [String] body for the request.
239
- # @param headers [Hash] unencoded HTTP header key/value pairs.
238
+ # @param body [String, nil] body for the request.
239
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
240
240
  #
241
241
  # @example
242
242
  # conn.post '/items', data, content_type: 'application/json'
@@ -255,10 +255,10 @@ module Faraday
255
255
  # Makes a PUT HTTP request with a body.
256
256
  # @!scope class
257
257
  #
258
- # @param url [String] The optional String base URL to use as a prefix for
258
+ # @param url [String, URI, nil] The optional String base URL to use as a prefix for
259
259
  # all requests. Can also be the options Hash.
260
- # @param body [String] body for the request.
261
- # @param headers [Hash] unencoded HTTP header key/value pairs.
260
+ # @param body [String, nil] body for the request.
261
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
262
262
  #
263
263
  # @example
264
264
  # # TODO: Make it a PUT example
@@ -390,7 +390,7 @@ module Faraday
390
390
  # Takes a relative url for a request and combines it with the defaults
391
391
  # set on the connection instance.
392
392
  #
393
- # @param url [String]
393
+ # @param url [String, URI, nil]
394
394
  # @param extra_params [Hash]
395
395
  #
396
396
  # @example
@@ -423,10 +423,10 @@ module Faraday
423
423
  # Builds and runs the Faraday::Request.
424
424
  #
425
425
  # @param method [Symbol] HTTP method.
426
- # @param url [String, URI] String or URI to access.
427
- # @param body [Object] The request body that will eventually be converted to
426
+ # @param url [String, URI, nil] String or URI to access.
427
+ # @param body [String, nil] The request body that will eventually be converted to
428
428
  # a string.
429
- # @param headers [Hash] unencoded HTTP header key/value pairs.
429
+ # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
430
430
  #
431
431
  # @return [Faraday::Response]
432
432
  def run_request(method, url, body, headers)
@@ -462,7 +462,7 @@ module Faraday
462
462
 
463
463
  # Build an absolute URL based on url_prefix.
464
464
  #
465
- # @param url [String, URI]
465
+ # @param url [String, URI, nil]
466
466
  # @param params [Faraday::Utils::ParamsHash] A Faraday::Utils::ParamsHash to
467
467
  # replace the query values
468
468
  # of the resulting url (default: nil).
@@ -537,7 +537,7 @@ module Faraday
537
537
  end
538
538
 
539
539
  def find_default_proxy
540
- uri = ENV['http_proxy']
540
+ uri = ENV.fetch('http_proxy', nil)
541
541
  return unless uri && !uri.empty?
542
542
 
543
543
  uri = "http://#{uri}" unless uri.match?(/^http/i)
@@ -157,6 +157,24 @@ module Faraday
157
157
  %(#<#{self.class}#{attrs.join(' ')}>)
158
158
  end
159
159
 
160
+ def stream_response?
161
+ request.stream_response?
162
+ end
163
+
164
+ def stream_response(&block)
165
+ size = 0
166
+ yielded = false
167
+ block_result = block.call do |chunk| # rubocop:disable Performance/RedundantBlockCall
168
+ if chunk.bytesize.positive? || size.positive?
169
+ yielded = true
170
+ size += chunk.bytesize
171
+ request.on_data.call(chunk, size, self)
172
+ end
173
+ end
174
+ request.on_data.call(+'', 0, self) unless yielded
175
+ block_result
176
+ end
177
+
160
178
  # @private
161
179
  def custom_members
162
180
  @custom_members ||= {}
@@ -23,22 +23,27 @@ module Faraday
23
23
  def on_request(env)
24
24
  return if env.request_headers[KEY]
25
25
 
26
- env.request_headers[KEY] = header_from(@type, *@params)
26
+ env.request_headers[KEY] = header_from(@type, env, *@params)
27
27
  end
28
28
 
29
29
  private
30
30
 
31
31
  # @param type [String, Symbol]
32
+ # @param env [Faraday::Env]
32
33
  # @param params [Array]
33
34
  # @return [String] a header value
34
- def header_from(type, *params)
35
+ def header_from(type, env, *params)
35
36
  if type.to_s.casecmp('basic').zero? && params.size == 2
36
37
  Utils.basic_header_from(*params)
37
38
  elsif params.size != 1
38
39
  raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
39
40
  else
40
41
  value = params.first
41
- value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
42
+ if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
43
+ value = value.call(env)
44
+ elsif value.is_a?(Proc) || value.respond_to?(:call)
45
+ value = value.call
46
+ end
42
47
  "#{type} #{value}"
43
48
  end
44
49
  end
@@ -21,7 +21,7 @@ module Faraday
21
21
  # @!attribute headers
22
22
  # @return [Faraday::Utils::Headers] headers
23
23
  # @!attribute body
24
- # @return [Hash] body
24
+ # @return [String] body
25
25
  # @!attribute options
26
26
  # @return [RequestOptions] options
27
27
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.4.0'
4
+ VERSION = '2.6.0'
5
5
  end
@@ -6,7 +6,7 @@ class CustomEncoder
6
6
  end
7
7
 
8
8
  def decode(params)
9
- params.split(',').map { |pair| pair.split('-') }.to_h
9
+ params.split(',').to_h { |pair| pair.split('-') }
10
10
  end
11
11
  end
12
12
 
@@ -511,7 +511,7 @@ RSpec.describe Faraday::Connection do
511
511
  it 'uses env http_proxy' do
512
512
  with_env 'http_proxy' => 'http://proxy.com' do
513
513
  conn = Faraday.new
514
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
514
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
515
515
  expect(conn.proxy_for_request('http://google.co.uk').host).to eq('proxy.com')
516
516
  end
517
517
  end
@@ -519,7 +519,7 @@ RSpec.describe Faraday::Connection do
519
519
  it 'uses processes no_proxy before http_proxy' do
520
520
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
521
521
  conn = Faraday.new
522
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
522
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
523
523
  expect(conn.proxy_for_request('http://google.co.uk')).to be_nil
524
524
  end
525
525
  end
@@ -527,7 +527,7 @@ RSpec.describe Faraday::Connection do
527
527
  it 'uses env https_proxy' do
528
528
  with_env 'https_proxy' => 'https://proxy.com' do
529
529
  conn = Faraday.new
530
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
530
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
531
531
  expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy.com')
532
532
  end
533
533
  end
@@ -535,7 +535,7 @@ RSpec.describe Faraday::Connection do
535
535
  it 'uses processes no_proxy before https_proxy' do
536
536
  with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
537
537
  conn = Faraday.new
538
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
538
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
539
539
  expect(conn.proxy_for_request('https://google.co.uk')).to be_nil
540
540
  end
541
541
  end
@@ -545,7 +545,7 @@ RSpec.describe Faraday::Connection do
545
545
  conn = Faraday.new
546
546
  conn.proxy = 'http://proxy2.com'
547
547
 
548
- expect(conn.instance_variable_get('@manual_proxy')).to be_truthy
548
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_truthy
549
549
  expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy2.com')
550
550
  end
551
551
  end
@@ -580,7 +580,7 @@ RSpec.describe Faraday::Connection do
580
580
  end
581
581
 
582
582
  conn.get(url)
583
- expect(conn.instance_variable_get('@temp_proxy')).to be_nil
583
+ expect(conn.instance_variable_get(:@temp_proxy)).to be_nil
584
584
  end
585
585
 
586
586
  it 'dynamically check no proxy' do
@@ -72,6 +72,41 @@ RSpec.describe Faraday::Request::Authorization do
72
72
  include_examples 'does not interfere with existing authentication'
73
73
  end
74
74
 
75
+ context 'with an argument' do
76
+ let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
77
+
78
+ context 'when passed a proc' do
79
+ let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
80
+
81
+ it { expect(response.body).to eq('Bearer proc crunchy surprise') }
82
+
83
+ include_examples 'does not interfere with existing authentication'
84
+ end
85
+
86
+ context 'when passed a lambda' do
87
+ let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }
88
+
89
+ it { expect(response.body).to eq('Bearer lambda crunchy surprise') }
90
+
91
+ include_examples 'does not interfere with existing authentication'
92
+ end
93
+
94
+ context 'when passed a callable with an argument' do
95
+ let(:callable) do
96
+ Class.new do
97
+ def call(env)
98
+ "callable #{env.request_headers['middle']}"
99
+ end
100
+ end.new
101
+ end
102
+ let(:auth_config) { [callable] }
103
+
104
+ it { expect(response.body).to eq('Bearer callable crunchy surprise') }
105
+
106
+ include_examples 'does not interfere with existing authentication'
107
+ end
108
+ end
109
+
75
110
  context 'when passed too many arguments' do
76
111
  let(:auth_config) { %w[baz foo] }
77
112
 
@@ -37,7 +37,7 @@ shared_examples 'adapter examples' do |**options|
37
37
 
38
38
  let(:conn) do
39
39
  conn_options[:ssl] ||= {}
40
- conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
40
+ conn_options[:ssl][:ca_file] ||= ENV.fetch('SSL_FILE', nil)
41
41
  conn_options[:ssl][:verify_hostname] ||= ENV['SSL_VERIFY_HOSTNAME'] == 'yes'
42
42
 
43
43
  Faraday.new(remote, conn_options) do |conn|
@@ -153,12 +153,19 @@ shared_examples 'a request method' do |http_method|
153
153
  let(:streamed) { [] }
154
154
 
155
155
  context 'when response is empty' do
156
- it do
156
+ it 'handles streaming' do
157
+ env = nil
157
158
  conn.public_send(http_method, '/') do |req|
158
- req.options.on_data = proc { |*args| streamed << args }
159
+ req.options.on_data = proc do |chunk, size, block_env|
160
+ streamed << [chunk, size]
161
+ env ||= block_env
162
+ end
159
163
  end
160
164
 
161
165
  expect(streamed).to eq([['', 0]])
166
+ # TODO: enable this after updating all existing adapters to the new streaming API
167
+ # expect(env).to be_a(Faraday::Env)
168
+ # expect(env.status).to eq(200)
162
169
  end
163
170
  end
164
171
 
@@ -166,12 +173,19 @@ shared_examples 'a request method' do |http_method|
166
173
  before { request_stub.to_return(body: big_string) }
167
174
 
168
175
  it 'handles streaming' do
176
+ env = nil
169
177
  response = conn.public_send(http_method, '/') do |req|
170
- req.options.on_data = proc { |*args| streamed << args }
178
+ req.options.on_data = proc do |chunk, size, block_env|
179
+ streamed << [chunk, size]
180
+ env ||= block_env
181
+ end
171
182
  end
172
183
 
173
184
  expect(response.body).to eq('')
174
185
  check_streaming_response(streamed, chunk_size: 16 * 1024)
186
+ # TODO: enable this after updating all existing adapters to the new streaming API
187
+ # expect(env).to be_a(Faraday::Env)
188
+ # expect(env.status).to eq(200)
175
189
  end
176
190
  end
177
191
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,22 +10,28 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-07-28 00:00:00.000000000 Z
13
+ date: 2022-10-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday-net_http
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - "~>"
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: '2.0'
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '3.1'
22
25
  type: :runtime
23
26
  prerelease: false
24
27
  version_requirements: !ruby/object:Gem::Requirement
25
28
  requirements:
26
- - - "~>"
29
+ - - ">="
27
30
  - !ruby/object:Gem::Version
28
31
  version: '2.0'
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.1'
29
35
  - !ruby/object:Gem::Dependency
30
36
  name: ruby2_keywords
31
37
  requirement: !ruby/object:Gem::Requirement
@@ -125,7 +131,7 @@ licenses:
125
131
  - MIT
126
132
  metadata:
127
133
  homepage_uri: https://lostisland.github.io/faraday
128
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.4.0
134
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.6.0
129
135
  source_code_uri: https://github.com/lostisland/faraday
130
136
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
131
137
  post_install_message: