faraday 2.14.0 → 2.15.1

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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +1 -1
  3. data/README.md +1 -1
  4. data/Rakefile +11 -5
  5. data/examples/client_test.rb +1 -1
  6. data/lib/faraday/adapter/test.rb +8 -0
  7. data/lib/faraday/adapter.rb +7 -6
  8. data/lib/faraday/connection.rb +28 -18
  9. data/lib/faraday/encoders/flat_params_encoder.rb +1 -0
  10. data/lib/faraday/encoders/nested_params_encoder.rb +12 -2
  11. data/lib/faraday/error.rb +30 -3
  12. data/lib/faraday/logging/formatter.rb +2 -2
  13. data/lib/faraday/middleware.rb +1 -1
  14. data/lib/faraday/options/connection_options.rb +27 -0
  15. data/lib/faraday/options/env.rb +3 -3
  16. data/lib/faraday/options/proxy_options.rb +13 -3
  17. data/lib/faraday/options/request_options.rb +36 -1
  18. data/lib/faraday/options.rb +4 -3
  19. data/lib/faraday/rack_builder.rb +9 -9
  20. data/lib/faraday/response/json.rb +2 -2
  21. data/lib/faraday/response/raise_error.rb +7 -1
  22. data/lib/faraday/response.rb +5 -2
  23. data/lib/faraday/utils/headers.rb +46 -12
  24. data/lib/faraday/utils.rb +15 -3
  25. data/lib/faraday/version.rb +1 -1
  26. data/lib/faraday.rb +32 -0
  27. data/spec/external_adapters/faraday_specs_setup.rb +2 -0
  28. data/spec/support/integration/test_pki.rb +77 -0
  29. data/spec/support/integration/tls_server.rb +92 -0
  30. data/spec/support/integration_helper.rb +71 -0
  31. metadata +7 -31
  32. data/spec/faraday/adapter/test_spec.rb +0 -442
  33. data/spec/faraday/adapter_registry_spec.rb +0 -28
  34. data/spec/faraday/adapter_spec.rb +0 -55
  35. data/spec/faraday/connection_spec.rb +0 -808
  36. data/spec/faraday/error_spec.rb +0 -175
  37. data/spec/faraday/middleware_registry_spec.rb +0 -31
  38. data/spec/faraday/middleware_spec.rb +0 -213
  39. data/spec/faraday/options/env_spec.rb +0 -76
  40. data/spec/faraday/options/options_spec.rb +0 -297
  41. data/spec/faraday/options/proxy_options_spec.rb +0 -79
  42. data/spec/faraday/options/request_options_spec.rb +0 -19
  43. data/spec/faraday/params_encoders/flat_spec.rb +0 -42
  44. data/spec/faraday/params_encoders/nested_spec.rb +0 -151
  45. data/spec/faraday/rack_builder_spec.rb +0 -317
  46. data/spec/faraday/request/authorization_spec.rb +0 -118
  47. data/spec/faraday/request/instrumentation_spec.rb +0 -74
  48. data/spec/faraday/request/json_spec.rb +0 -199
  49. data/spec/faraday/request/url_encoded_spec.rb +0 -93
  50. data/spec/faraday/request_spec.rb +0 -110
  51. data/spec/faraday/response/json_spec.rb +0 -206
  52. data/spec/faraday/response/logger_spec.rb +0 -299
  53. data/spec/faraday/response/raise_error_spec.rb +0 -286
  54. data/spec/faraday/response_spec.rb +0 -84
  55. data/spec/faraday/utils/headers_spec.rb +0 -109
  56. data/spec/faraday/utils_spec.rb +0 -120
  57. data/spec/faraday_spec.rb +0 -43
  58. data/spec/spec_helper.rb +0 -133
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fc5e44f27171827ea964dee68999e7d9ae6cd4d952a0a8ad0600e155cdf3892
4
- data.tar.gz: 88c85684e7d9acb7978b36d20f2041145441f99e7705e6cf8f41483ed620dadc
3
+ metadata.gz: a700341d7d0844ede1248c479375ec0b3cb7fd51526c7c4abbaf9ce3db6c8a44
4
+ data.tar.gz: 76de6bace7c8995002b54ce7f19610a311418ef253c19a2fb354dee8ea986866
5
5
  SHA512:
6
- metadata.gz: 972d764f70f42d46e01ef8c8a33aab692619b8adc1793f07ed031cc7cd09ad6d749d5948d25f4e9e8c6b83377bfa2a25eda1e80eb79d086bae93d90959e321bd
7
- data.tar.gz: b9b7b80f59d02b813ae99ba5afd3cbaf517953f208f2cfc91347bdbec1f4112d4b508c22721f65ac607b45a47507d7adc97cbe7cf08b3d0c56ad0c7dc1b0af5e
6
+ metadata.gz: 5bc51dff2b4420115e15f36532fa2f9c2a31aa60d28d9acd864dcd4a50185e28fe5ac4dbeab648915d6a605e3606d06e207c083b3387a3253040bc9c108d6264
7
+ data.tar.gz: 82893f3f9d2152459d2a7b9b271601926e723a0723b7c3d20dc615795ce3d210473f2b6f359ec68fb4cd0f0a590ecae3121c325a257040c33701094cf56966fb
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2023 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2026 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -58,7 +58,7 @@ But before you start coding, please read our [Contributing Guide][contributing]
58
58
 
59
59
  ## Copyright
60
60
 
61
- © 2009 - 2023, the Faraday Team. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
61
+ © 2009 - 2026, the Faraday Team. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
62
62
 
63
63
  [awesome]: https://github.com/lostisland/awesome-faraday/#adapters
64
64
  [website]: https://lostisland.github.io/faraday
data/Rakefile CHANGED
@@ -1,12 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rspec/core/rake_task'
4
3
  require 'bundler'
5
4
 
6
5
  Bundler::GemHelper.install_tasks
7
6
 
8
- RSpec::Core::RakeTask.new(:spec) do |task|
9
- task.ruby_opts = %w[-W]
10
- end
7
+ # The publish workflow sets FARADAY_RELEASE because its bundle (release/Gemfile) doesn't include RSpec.
8
+ unless ENV['FARADAY_RELEASE']
9
+ require 'rspec/core/rake_task'
10
+
11
+ RSpec::Core::RakeTask.new(:spec) do |task|
12
+ task.ruby_opts = %w[-W]
13
+ # Integration specs need service containers, see .github/workflows/integration-tests.yml
14
+ task.exclude_pattern = 'spec/integration/**/*_spec.rb'
15
+ end
11
16
 
12
- task default: :spec
17
+ task default: :spec
18
+ end
@@ -26,7 +26,7 @@ class Client
26
26
  end
27
27
 
28
28
  # Example API client test
29
- class ClientTest < Test::Unit::TestCase
29
+ class ClientTest < Test::Unit::TestCase # rubocop:disable Style/OneClassPerFile
30
30
  def test_httpbingo_name
31
31
  stubs = Faraday::Adapter::Test::Stubs.new
32
32
  stubs.get('/api') do |env|
@@ -127,6 +127,14 @@ module Faraday
127
127
  new_stub(:options, path, headers, &block)
128
128
  end
129
129
 
130
+ # Removes all stubs, including the ones that have already been consumed.
131
+ def clear
132
+ @stubs_mutex.synchronize do
133
+ @stack.clear
134
+ @consumed.clear
135
+ end
136
+ end
137
+
130
138
  # Raises an error if any of the stubbed calls have not been made.
131
139
  def verify_stubbed_calls
132
140
  failed_stubs = []
@@ -8,6 +8,12 @@ module Faraday
8
8
 
9
9
  CONTENT_LENGTH = 'Content-Length'
10
10
 
11
+ TIMEOUT_KEYS = {
12
+ read: :read_timeout,
13
+ open: :open_timeout,
14
+ write: :write_timeout
15
+ }.freeze
16
+
11
17
  # This module marks an Adapter as supporting parallel requests.
12
18
  module Parallelism
13
19
  attr_writer :supports_parallel
@@ -23,6 +29,7 @@ module Faraday
23
29
  end
24
30
 
25
31
  extend Parallelism
32
+
26
33
  self.supports_parallel = false
27
34
 
28
35
  def initialize(_app = nil, opts = {}, &block)
@@ -89,12 +96,6 @@ module Faraday
89
96
  end
90
97
  options[key] || options[:timeout]
91
98
  end
92
-
93
- TIMEOUT_KEYS = {
94
- read: :read_timeout,
95
- open: :open_timeout,
96
- write: :write_timeout
97
- }.freeze
98
99
  end
99
100
  end
100
101
 
@@ -210,7 +210,7 @@ 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, URI, nil] String base URL to sue as a prefix for all requests.
213
+ # @param url [String, URI, nil] String base URL to use as a prefix for all requests.
214
214
  # @param params [Hash, nil] Hash of URI query unencoded key/value pairs.
215
215
  # @param headers [Hash, nil] unencoded HTTP header key/value pairs.
216
216
  #
@@ -300,7 +300,10 @@ module Faraday
300
300
  end
301
301
  end
302
302
 
303
- # Determine if this Faraday::Connection can make parallel requests.
303
+ # Determine if this Faraday::Connection is currently making parallel
304
+ # requests, i.e. if it is called inside an {#in_parallel} block.
305
+ # Use {#support_parallel?} to check if the adapter can make parallel
306
+ # requests at all.
304
307
  #
305
308
  # @return [Boolean]
306
309
  def in_parallel?
@@ -315,13 +318,13 @@ module Faraday
315
318
  # @yield a block to execute multiple requests.
316
319
  # @return [void]
317
320
  def in_parallel(manager = nil, &block)
318
- @parallel_manager = manager || default_parallel_manager do
319
- warn 'Warning: `in_parallel` called but no parallel-capable adapter ' \
320
- 'on Faraday stack'
321
- warn caller[2, 10].join("\n")
321
+ previous_manager = @parallel_manager
322
+ @parallel_manager = manager || previous_manager || default_parallel_manager do
323
+ warn('Warning: `in_parallel` called but no parallel-capable adapter ' \
324
+ 'on Faraday stack', caller[2, 10].join("\n"))
322
325
  nil
323
326
  end
324
- return yield unless @parallel_manager
327
+ return yield unless @parallel_manager && !@parallel_manager.equal?(previous_manager)
325
328
 
326
329
  if @parallel_manager.respond_to?(:execute)
327
330
  # Execute is the new method that is responsible for executing the block.
@@ -332,7 +335,7 @@ module Faraday
332
335
  @parallel_manager.run
333
336
  end
334
337
  ensure
335
- @parallel_manager = nil
338
+ @parallel_manager = previous_manager
336
339
  end
337
340
 
338
341
  # Sets the Hash proxy options.
@@ -362,7 +365,7 @@ module Faraday
362
365
  #
363
366
  # conn.get("nigiri?page=2") # accesses https://httpbingo.org/api/nigiri
364
367
  def url_prefix=(url, encoder = nil)
365
- uri = @url_prefix = Utils.URI(url)
368
+ uri = @url_prefix = Utils.URI(url.to_s)
366
369
  self.path_prefix = uri.path
367
370
 
368
371
  params.merge_query(uri.query, encoder)
@@ -445,7 +448,7 @@ module Faraday
445
448
  req.options.proxy = proxy_for_request(url)
446
449
  req.url(url) if url
447
450
  req.headers.update(headers) if headers
448
- req.body = body if body
451
+ req.body = body unless body.nil?
449
452
  yield(req) if block_given?
450
453
  end
451
454
 
@@ -481,8 +484,11 @@ module Faraday
481
484
  if url && !base.path.end_with?('/')
482
485
  base.path = "#{base.path}/" # ensure trailing slash
483
486
  end
484
- # Ensure relative url will be parsed correctly (such as `service:search` )
485
- url = "./#{url}" if url.respond_to?(:start_with?) && !url.start_with?('http://', 'https://', '/', './', '../')
487
+ url = url.to_s if url.respond_to?(:host)
488
+ # Ensure relative url will be parsed correctly (such as `service:search` or `//evil.com`)
489
+ url = "./#{url}" if url.respond_to?(:start_with?) &&
490
+ (url.start_with?('//') ||
491
+ !url.start_with?('http://', 'https://', '/', './', '../'))
486
492
  uri = url ? base + url : base
487
493
  if params
488
494
  uri.query = params.to_query(params_encoder || options.params_encoder)
@@ -493,8 +499,6 @@ module Faraday
493
499
 
494
500
  # Creates a duplicate of this Faraday::Connection.
495
501
  #
496
- # @api private
497
- #
498
502
  # @return [Faraday::Connection]
499
503
  def dup
500
504
  self.class.new(build_exclusive_url,
@@ -502,7 +506,7 @@ module Faraday
502
506
  params: params.dup,
503
507
  builder: builder.dup,
504
508
  ssl: ssl.dup,
505
- request: options.dup)
509
+ request: options.dup).tap { |copy| copy.proxy = proxy if @manual_proxy }
506
510
  end
507
511
 
508
512
  # Yields username and password extracted from a URI if they both exist.
@@ -516,7 +520,7 @@ module Faraday
516
520
  def with_uri_credentials(uri)
517
521
  return unless uri.user && uri.password
518
522
 
519
- yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
523
+ yield(URI::DEFAULT_PARSER.unescape(uri.user), URI::DEFAULT_PARSER.unescape(uri.password))
520
524
  end
521
525
 
522
526
  def proxy_from_env(url)
@@ -529,7 +533,7 @@ module Faraday
529
533
  uri = if uri.host.nil?
530
534
  find_default_proxy
531
535
  else
532
- URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
536
+ URI.parse(uri.to_s).find_proxy
533
537
  end
534
538
  when URI
535
539
  uri = url.find_proxy
@@ -557,7 +561,13 @@ module Faraday
557
561
  end
558
562
  end
559
563
 
560
- def support_parallel?(adapter)
564
+ # Determine if an adapter can make parallel requests.
565
+ #
566
+ # @param adapter [Class, nil] the adapter class to check. Defaults to the
567
+ # adapter of this Faraday::Connection.
568
+ #
569
+ # @return [Boolean]
570
+ def support_parallel?(adapter = @builder.adapter&.klass)
561
571
  adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
562
572
  end
563
573
  end
@@ -6,6 +6,7 @@ module Faraday
6
6
  module FlatParamsEncoder
7
7
  class << self
8
8
  extend Forwardable
9
+
9
10
  def_delegators :'Faraday::Utils', :escape, :unescape
10
11
  end
11
12
 
@@ -106,6 +106,8 @@ module Faraday
106
106
 
107
107
  def decode_pair(key, value, context)
108
108
  subkeys = key.scan(SUBKEYS_REGEX)
109
+ validate_params_depth!(subkeys.length)
110
+
109
111
  subkeys.each_with_index do |subkey, i|
110
112
  is_array = subkey =~ /[\[\]]+\Z/
111
113
  subkey = Regexp.last_match.pre_match if is_array
@@ -145,6 +147,12 @@ module Faraday
145
147
  is_array ? context << value : context[subkey] = value
146
148
  end
147
149
 
150
+ def validate_params_depth!(depth)
151
+ return unless @param_depth_limit && depth > @param_depth_limit
152
+
153
+ raise Faraday::Error, "exceeded nested parameter depth limit of #{@param_depth_limit}"
154
+ end
155
+
148
156
  # Internal: convert a nested hash with purely numeric keys into an array.
149
157
  # FIXME: this is not compatible with Rack::Utils.parse_nested_query
150
158
  # @!visibility private
@@ -154,7 +162,7 @@ module Faraday
154
162
  end
155
163
 
156
164
  if depth.positive? && !hash.empty? && hash.keys.all? { |k| k =~ /^\d+$/ }
157
- hash.sort.map(&:last)
165
+ hash.sort_by { |key, _| key.to_i }.map(&:last)
158
166
  else
159
167
  hash
160
168
  end
@@ -167,15 +175,17 @@ module Faraday
167
175
  # for your requests.
168
176
  module NestedParamsEncoder
169
177
  class << self
170
- attr_accessor :sort_params, :array_indices
178
+ attr_accessor :sort_params, :array_indices, :param_depth_limit
171
179
 
172
180
  extend Forwardable
181
+
173
182
  def_delegators :'Faraday::Utils', :escape, :unescape
174
183
  end
175
184
 
176
185
  # Useful default for OAuth and caching.
177
186
  @sort_params = true
178
187
  @array_indices = false
188
+ @param_depth_limit = 100
179
189
 
180
190
  extend EncodeMethods
181
191
  extend DecodeMethods
data/lib/faraday/error.rb CHANGED
@@ -70,7 +70,7 @@ module Faraday
70
70
  # to `super`. See NilStatusError.
71
71
  def exc_msg_and_response!(exc, response = nil)
72
72
  if @response.nil? && @wrapped_exception.nil?
73
- @wrapped_exception, msg, @response = exc_msg_and_response(exc, response)
73
+ @wrapped_exception, msg, @response = exc_msg_and_response(exc, omit_request_data(response))
74
74
  return msg
75
75
  end
76
76
 
@@ -85,7 +85,14 @@ module Faraday
85
85
  when Hash
86
86
  [nil, build_error_message_from_hash(exc), exc]
87
87
  when Faraday::Env
88
- [nil, build_error_message_from_env(exc), exc]
88
+ sanitized = omit_request_data(exc)
89
+ message =
90
+ if sanitized.equal?(exc)
91
+ build_error_message_from_env(exc)
92
+ else
93
+ build_error_message_from_hash(sanitized)
94
+ end
95
+ [nil, message, sanitized]
89
96
  else
90
97
  [nil, exc.to_s, response]
91
98
  end
@@ -93,6 +100,26 @@ module Faraday
93
100
 
94
101
  private
95
102
 
103
+ # When the RaiseError middleware opted this request out of request data
104
+ # (`include_request: false`), it marks the Env. Replace env-carrying
105
+ # responses (the Response object raised by the Json middleware, or an Env
106
+ # passed directly) with a request-less hash so that errors raised by other
107
+ # middlewares (e.g. ParsingError) cannot expose request headers and body.
108
+ # The body reads the explicit `response_body` member because `Env#body`
109
+ # aliases to the request body while the status is nil.
110
+ def omit_request_data(response)
111
+ env =
112
+ case response
113
+ when Faraday::Response
114
+ response.env if response.finished?
115
+ when Faraday::Env
116
+ response
117
+ end
118
+ return response unless env.is_a?(Faraday::Env) && env[:include_request] == false
119
+
120
+ { status: env.status, headers: env.response_headers, body: env.response_body }
121
+ end
122
+
96
123
  def build_error_message_from_hash(hash)
97
124
  # Be defensive with external Hash objects - they might be missing keys
98
125
  status = hash.fetch(:status, nil)
@@ -172,7 +199,7 @@ module Faraday
172
199
  # A unified client error for timeouts.
173
200
  class TimeoutError < ServerError
174
201
  def initialize(exc = 'timeout', response = nil)
175
- super(exc, response)
202
+ super
176
203
  end
177
204
  end
178
205
 
@@ -41,7 +41,7 @@ module Faraday
41
41
  def exception(exc)
42
42
  return unless log_errors?
43
43
 
44
- public_send(log_level) { "error: #{exc.full_message}" }
44
+ public_send(log_level) { "error: #{apply_filters(exc.full_message)}" }
45
45
 
46
46
  log_headers('error', exc.response_headers) if exc.respond_to?(:response_headers) && log_headers?(:error)
47
47
  return unless exc.respond_to?(:response_body) && exc.response_body && log_body?(:error)
@@ -63,7 +63,7 @@ module Faraday
63
63
 
64
64
  def dump_body(body)
65
65
  if body.respond_to?(:to_str)
66
- body.to_str.encode(Encoding::UTF_8, undef: :replace, invalid: :replace)
66
+ body.to_str.encode(::Encoding::UTF_8, undef: :replace, invalid: :replace)
67
67
  else
68
68
  pretty_inspect(body)
69
69
  end
@@ -65,7 +65,7 @@ module Faraday
65
65
  if app.respond_to?(:close)
66
66
  app.close
67
67
  else
68
- warn "#{app} does not implement \#close!"
68
+ warn "#{app} does not implement #close!"
69
69
  end
70
70
  end
71
71
  end
@@ -4,6 +4,33 @@ module Faraday
4
4
  # @!parse
5
5
  # # ConnectionOptions contains the configurable properties for a Faraday
6
6
  # # connection object.
7
+ # #
8
+ # # @!attribute request
9
+ # # @return [RequestOptions] default request options for the connection
10
+ # #
11
+ # # @!attribute proxy
12
+ # # @return [ProxyOptions] proxy options for the connection
13
+ # #
14
+ # # @!attribute ssl
15
+ # # @return [SSLOptions] SSL options for the connection
16
+ # #
17
+ # # @!attribute builder
18
+ # # @return [RackBuilder] the middleware stack builder for the connection
19
+ # #
20
+ # # @!attribute url
21
+ # # @return [String, URI] the base URL for the connection
22
+ # #
23
+ # # @!attribute parallel_manager
24
+ # # @return [Object] manager used when the connection runs in parallel mode
25
+ # #
26
+ # # @!attribute params
27
+ # # @return [Hash] default query parameters for the connection
28
+ # #
29
+ # # @!attribute headers
30
+ # # @return [Hash] default headers for the connection
31
+ # #
32
+ # # @!attribute builder_class
33
+ # # @return [Class] the class used to build the middleware stack
7
34
  # class ConnectionOptions < Options; end
8
35
  ConnectionOptions = Options.new(:request, :proxy, :ssl, :builder, :url,
9
36
  :parallel_manager, :params, :headers,
@@ -78,7 +78,7 @@ module Faraday
78
78
  # @param value [Object] a value fitting Option.from(v).
79
79
  # @return [Env] from given value
80
80
  def self.from(value)
81
- env = super(value)
81
+ env = super
82
82
  if value.respond_to?(:custom_members)
83
83
  env.custom_members.update(value.custom_members)
84
84
  end
@@ -90,7 +90,7 @@ module Faraday
90
90
  return self[current_body] if key == :body
91
91
 
92
92
  if in_member_set?(key)
93
- super(key)
93
+ super
94
94
  else
95
95
  custom_members[key]
96
96
  end
@@ -105,7 +105,7 @@ module Faraday
105
105
  end
106
106
 
107
107
  if in_member_set?(key)
108
- super(key, value)
108
+ super
109
109
  else
110
110
  custom_members[key] = value
111
111
  end
@@ -4,9 +4,19 @@ module Faraday
4
4
  # @!parse
5
5
  # # ProxyOptions contains the configurable properties for the proxy
6
6
  # # configuration used when making an HTTP request.
7
+ # #
8
+ # # @!attribute uri
9
+ # # @return [URI] the proxy server URI
10
+ # #
11
+ # # @!attribute user
12
+ # # @return [String] the proxy server username
13
+ # #
14
+ # # @!attribute password
15
+ # # @return [String] the proxy server password
7
16
  # class ProxyOptions < Options; end
8
17
  ProxyOptions = Options.new(:uri, :user, :password) do
9
18
  extend Forwardable
19
+
10
20
  def_delegators :uri, :scheme, :scheme=, :host, :host=, :port, :port=,
11
21
  :path, :path=
12
22
 
@@ -29,10 +39,10 @@ module Faraday
29
39
  end
30
40
  end
31
41
 
32
- super(value)
42
+ super
33
43
  end
34
44
 
35
- memoized(:user) { uri&.user && Utils.unescape(uri.user) }
36
- memoized(:password) { uri&.password && Utils.unescape(uri.password) }
45
+ memoized(:user) { uri&.user && URI::DEFAULT_PARSER.unescape(uri.user) }
46
+ memoized(:password) { uri&.password && URI::DEFAULT_PARSER.unescape(uri.password) }
37
47
  end
38
48
  end
@@ -3,6 +3,41 @@
3
3
  module Faraday
4
4
  # @!parse
5
5
  # # RequestOptions contains the configurable properties for a Faraday request.
6
+ # #
7
+ # # @!attribute params_encoder
8
+ # # @return [Object] the params encoder used to serialize the request params
9
+ # #
10
+ # # @!attribute proxy
11
+ # # @return [ProxyOptions] proxy options for this request
12
+ # #
13
+ # # @!attribute bind
14
+ # # @return [Hash] the local host and port to bind the connection to
15
+ # #
16
+ # # @!attribute timeout
17
+ # # @return [Integer] time limit for the entire request (in seconds)
18
+ # #
19
+ # # @!attribute open_timeout
20
+ # # @return [Integer] time limit for just the connection phase (in seconds)
21
+ # #
22
+ # # @!attribute read_timeout
23
+ # # @return [Integer] time limit for the first response byte received from
24
+ # # the server (in seconds)
25
+ # #
26
+ # # @!attribute write_timeout
27
+ # # @return [Integer] time limit for the client to send the request to the
28
+ # # server (in seconds)
29
+ # #
30
+ # # @!attribute boundary
31
+ # # @return [String] the boundary used for multipart request bodies
32
+ # #
33
+ # # @!attribute oauth
34
+ # # @return [Hash] OAuth options used by the OAuth middleware
35
+ # #
36
+ # # @!attribute context
37
+ # # @return [Hash] free-form storage carried alongside the request
38
+ # #
39
+ # # @!attribute on_data
40
+ # # @return [Proc] callback invoked with each chunk when streaming the response
6
41
  # class RequestOptions < Options; end
7
42
  RequestOptions = Options.new(:params_encoder, :proxy, :bind,
8
43
  :timeout, :open_timeout, :read_timeout,
@@ -12,7 +47,7 @@ module Faraday
12
47
  if key && key.to_sym == :proxy
13
48
  super(key, value ? ProxyOptions.from(value) : nil)
14
49
  else
15
- super(key, value)
50
+ super
16
51
  end
17
52
  end
18
53
 
@@ -74,7 +74,7 @@ module Faraday
74
74
 
75
75
  # Public
76
76
  def fetch(key, *args)
77
- unless symbolized_key_set.include?(key.to_sym)
77
+ unless members.include?(key.to_sym) && !send(key).nil?
78
78
  key_setter = "#{key}="
79
79
  if !args.empty?
80
80
  send(key_setter, args.first)
@@ -186,14 +186,15 @@ module Faraday
186
186
  def [](key)
187
187
  key = key.to_sym
188
188
  if (method = self.class.memoized_attributes[key])
189
- super(key) || (self[key] = instance_eval(&method))
189
+ value = super
190
+ value.nil? ? (self[key] = instance_eval(&method)) : value
190
191
  else
191
192
  super
192
193
  end
193
194
  end
194
195
 
195
196
  def symbolized_key_set
196
- @symbolized_key_set ||= Set.new(keys.map(&:to_sym))
197
+ Set.new(keys.map(&:to_sym))
197
198
  end
198
199
 
199
200
  def self.inherited(subclass)
@@ -15,6 +15,11 @@ module Faraday
15
15
  # Used to detect missing arguments
16
16
  NO_ARGUMENT = Object.new
17
17
 
18
+ LOCK_ERR = "can't modify middleware stack after making a request"
19
+ MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
20
+ "Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
21
+ 'For more info, check https://lostisland.github.io/faraday/usage/.'
22
+
18
23
  attr_accessor :handlers
19
24
 
20
25
  # Error raised when trying to modify the stack after calling `lock!`
@@ -68,6 +73,9 @@ module Faraday
68
73
  super
69
74
  @adapter = original.adapter
70
75
  @handlers = original.handlers.dup
76
+ # @app is memoized on the first request. Drop it so the duplicate
77
+ # rebuilds from the copied handlers and middleware added after dup runs.
78
+ @app = nil
71
79
  end
72
80
 
73
81
  def build
@@ -110,6 +118,7 @@ module Faraday
110
118
  def adapter(klass = NO_ARGUMENT, *args, **kwargs, &block)
111
119
  return @adapter if klass == NO_ARGUMENT || klass.nil?
112
120
 
121
+ raise_if_locked
113
122
  klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
114
123
  @adapter = self.class::Handler.new(klass, *args, **kwargs, &block)
115
124
  end
@@ -211,11 +220,6 @@ module Faraday
211
220
 
212
221
  private
213
222
 
214
- LOCK_ERR = "can't modify middleware stack after making a request"
215
- MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
216
- "Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
217
- 'For more info, check https://lostisland.github.io/faraday/usage/.'
218
-
219
223
  def raise_if_locked
220
224
  raise StackLocked, LOCK_ERR if locked?
221
225
  end
@@ -230,10 +234,6 @@ module Faraday
230
234
  raise MISSING_ADAPTER_ERROR unless @adapter
231
235
  end
232
236
 
233
- def adapter_set?
234
- !@adapter.nil?
235
- end
236
-
237
237
  def use_symbol(mod, key, ...)
238
238
  use(mod.lookup_middleware(key), ...)
239
239
  end
@@ -8,7 +8,7 @@ module Faraday
8
8
  class Json < Middleware
9
9
  def initialize(app = nil, parser_options: nil, content_type: /\bjson$/, preserve_raw: false)
10
10
  super(app)
11
- @parser_options = parser_options
11
+ @parser_options = parser_options&.dup
12
12
  @content_types = Array(content_type)
13
13
  @preserve_raw = preserve_raw
14
14
 
@@ -33,7 +33,7 @@ module Faraday
33
33
 
34
34
  decoder, method_name = @decoder_options
35
35
 
36
- decoder.public_send(method_name, body, @parser_options || {})
36
+ decoder.public_send(method_name, body, **(@parser_options || {}))
37
37
  end
38
38
 
39
39
  def parse_response?(env)
@@ -20,7 +20,13 @@ module Faraday
20
20
  }.freeze
21
21
  # rubocop:enable Naming/ConstantName
22
22
 
23
- DEFAULT_OPTIONS = { include_request: true, allowed_statuses: [] }.freeze
23
+ DEFAULT_OPTIONS = { include_request: true, allowed_statuses: [].freeze }.freeze
24
+
25
+ def on_request(env)
26
+ # Announce the opt-out on the shared Env so that errors raised by other
27
+ # middlewares (e.g. ParsingError from the Json middleware) honor it too.
28
+ env[:include_request] = false unless options[:include_request]
29
+ end
24
30
 
25
31
  def on_complete(env)
26
32
  return if Array(options[:allowed_statuses]).include?(env[:status])
@@ -54,7 +54,9 @@ module Faraday
54
54
  raise 'response already finished' if finished?
55
55
 
56
56
  @env = env.is_a?(Env) ? env : Env.from(env)
57
- @on_complete_callbacks.each { |callback| callback.call(@env) }
57
+ callbacks = @on_complete_callbacks
58
+ @on_complete_callbacks = []
59
+ callbacks.each { |callback| callback.call(@env) }
58
60
  self
59
61
  end
60
62
 
@@ -76,7 +78,8 @@ module Faraday
76
78
  end
77
79
 
78
80
  def marshal_load(env)
79
- @env = Env.from(env)
81
+ @env = Env.from(env) if env
82
+ @on_complete_callbacks = []
80
83
  end
81
84
 
82
85
  # Expand the env with more properties, without overriding existing ones.