faraday 1.10.6 → 2.0.0.alpha.pre.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -1
  3. data/README.md +16 -9
  4. data/examples/client_test.rb +1 -1
  5. data/lib/faraday/adapter/test.rb +2 -0
  6. data/lib/faraday/adapter.rb +0 -5
  7. data/lib/faraday/connection.rb +3 -86
  8. data/lib/faraday/encoders/nested_params_encoder.rb +3 -12
  9. data/lib/faraday/error.rb +7 -0
  10. data/lib/faraday/file_part.rb +122 -0
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +15 -79
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/param_part.rb +53 -0
  16. data/lib/faraday/rack_builder.rb +1 -1
  17. data/lib/faraday/request/authorization.rb +26 -40
  18. data/lib/faraday/request/instrumentation.rb +2 -0
  19. data/lib/faraday/request/multipart.rb +108 -0
  20. data/lib/faraday/request/retry.rb +241 -0
  21. data/lib/faraday/request/url_encoded.rb +2 -0
  22. data/lib/faraday/request.rb +8 -24
  23. data/lib/faraday/response/json.rb +4 -4
  24. data/lib/faraday/response/logger.rb +2 -0
  25. data/lib/faraday/response/raise_error.rb +9 -1
  26. data/lib/faraday/response.rb +7 -20
  27. data/lib/faraday/utils/headers.rb +1 -1
  28. data/lib/faraday/utils.rb +9 -4
  29. data/lib/faraday/version.rb +1 -1
  30. data/lib/faraday.rb +6 -45
  31. data/spec/faraday/connection_spec.rb +78 -97
  32. data/spec/faraday/options/env_spec.rb +2 -2
  33. data/spec/faraday/params_encoders/nested_spec.rb +0 -30
  34. data/spec/faraday/rack_builder_spec.rb +5 -43
  35. data/spec/faraday/request/authorization_spec.rb +14 -36
  36. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  37. data/spec/faraday/request/multipart_spec.rb +302 -0
  38. data/spec/faraday/request/retry_spec.rb +254 -0
  39. data/spec/faraday/request_spec.rb +0 -11
  40. data/spec/faraday/response/json_spec.rb +4 -6
  41. data/spec/faraday/response/raise_error_spec.rb +7 -4
  42. data/spec/faraday/utils_spec.rb +1 -1
  43. data/spec/faraday_spec.rb +4 -8
  44. data/spec/spec_helper.rb +0 -2
  45. data/spec/support/fake_safe_buffer.rb +1 -1
  46. data/spec/support/shared_examples/request_method.rb +5 -5
  47. metadata +25 -156
  48. data/lib/faraday/adapter/typhoeus.rb +0 -15
  49. data/lib/faraday/autoload.rb +0 -89
  50. data/lib/faraday/dependency_loader.rb +0 -39
  51. data/lib/faraday/deprecate.rb +0 -110
  52. data/lib/faraday/request/basic_authentication.rb +0 -20
  53. data/lib/faraday/request/token_authentication.rb +0 -20
  54. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  55. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  56. data/spec/faraday/adapter/excon_spec.rb +0 -49
  57. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  58. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  59. data/spec/faraday/adapter/patron_spec.rb +0 -18
  60. data/spec/faraday/adapter/rack_spec.rb +0 -8
  61. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  62. data/spec/faraday/deprecate_spec.rb +0 -147
  63. data/spec/faraday/response/middleware_spec.rb +0 -68
  64. data/spec/support/webmock_rack_app.rb +0 -68
@@ -26,27 +26,11 @@ module Faraday
26
26
  # @return [RequestOptions] options
27
27
  #
28
28
  # rubocop:disable Style/StructInheritance
29
- class Request < Struct.new(
30
- :http_method, :path, :params, :headers, :body, :options
31
- )
29
+ class Request < Struct.new(:http_method, :path, :params, :headers, :body, :options)
32
30
  # rubocop:enable Style/StructInheritance
33
31
 
34
32
  extend MiddlewareRegistry
35
33
 
36
- register_middleware File.expand_path('request', __dir__),
37
- url_encoded: [:UrlEncoded, 'url_encoded'],
38
- authorization: [:Authorization, 'authorization'],
39
- basic_auth: [
40
- :BasicAuthentication,
41
- 'basic_authentication'
42
- ],
43
- token_auth: [
44
- :TokenAuthentication,
45
- 'token_authentication'
46
- ],
47
- instrumentation: [:Instrumentation, 'instrumentation'],
48
- json: [:Json, 'json']
49
-
50
34
  # @param request_method [String]
51
35
  # @yield [request] for block customization, if block given
52
36
  # @yieldparam request [Request]
@@ -57,13 +41,6 @@ module Faraday
57
41
  end
58
42
  end
59
43
 
60
- def method
61
- http_method
62
- end
63
-
64
- extend Faraday::Deprecate
65
- deprecate :method, :http_method, '2.0'
66
-
67
44
  # Replace params, preserving the existing hash type.
68
45
  #
69
46
  # @param hash [Hash] new params
@@ -152,3 +129,10 @@ module Faraday
152
129
  end
153
130
  end
154
131
  end
132
+
133
+ require 'faraday/request/authorization'
134
+ require 'faraday/request/instrumentation'
135
+ require 'faraday/request/json'
136
+ require 'faraday/request/multipart'
137
+ require 'faraday/request/retry'
138
+ require 'faraday/request/url_encoded'
@@ -6,11 +6,11 @@ module Faraday
6
6
  class Response
7
7
  # Parse response bodies as JSON.
8
8
  class Json < Middleware
9
- def initialize(app = nil, options = {})
9
+ def initialize(app = nil, parser_options: nil, content_type: /\bjson$/, preserve_raw: false)
10
10
  super(app)
11
- @parser_options = options[:parser_options]
12
- @content_types = Array(options[:content_type] || /\bjson$/)
13
- @preserve_raw = options[:preserve_raw]
11
+ @parser_options = parser_options
12
+ @content_types = Array(content_type)
13
+ @preserve_raw = preserve_raw
14
14
  end
15
15
 
16
16
  def on_complete(env)
@@ -29,3 +29,5 @@ module Faraday
29
29
  end
30
30
  end
31
31
  end
32
+
33
+ Faraday::Response.register_middleware(logger: Faraday::Response::Logger)
@@ -44,13 +44,21 @@ module Faraday
44
44
  body: env.body,
45
45
  request: {
46
46
  method: env.method,
47
+ url: env.url,
47
48
  url_path: env.url.path,
48
- params: env.params,
49
+ params: query_params(env),
49
50
  headers: env.request_headers,
50
51
  body: env.request_body
51
52
  }
52
53
  }
53
54
  end
55
+
56
+ def query_params(env)
57
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
58
+ env.params_encoder.decode(env.url.query)
59
+ end
54
60
  end
55
61
  end
56
62
  end
63
+
64
+ Faraday::Response.register_middleware(raise_error: Faraday::Response::RaiseError)
@@ -5,26 +5,9 @@ require 'forwardable'
5
5
  module Faraday
6
6
  # Response represents an HTTP response from making an HTTP request.
7
7
  class Response
8
- # Used for simple response middleware.
9
- class Middleware < Faraday::Middleware
10
- # Override this to modify the environment after the response has finished.
11
- # Calls the `parse` method if defined
12
- # `parse` method can be defined as private, public and protected
13
- def on_complete(env)
14
- return unless respond_to?(:parse, true) && env.parse_body?
15
-
16
- env.body = parse(env.body)
17
- end
18
- end
19
-
20
8
  extend Forwardable
21
9
  extend MiddlewareRegistry
22
10
 
23
- register_middleware File.expand_path('response', __dir__),
24
- raise_error: [:RaiseError, 'raise_error'],
25
- logger: [:Logger, 'logger'],
26
- json: [:Json, 'json']
27
-
28
11
  def initialize(env = nil)
29
12
  @env = Env.from(env) if env
30
13
  @on_complete_callbacks = []
@@ -55,10 +38,10 @@ module Faraday
55
38
  end
56
39
 
57
40
  def on_complete(&block)
58
- if !finished?
59
- @on_complete_callbacks << block
60
- else
41
+ if finished?
61
42
  yield(env)
43
+ else
44
+ @on_complete_callbacks << block
62
45
  end
63
46
  self
64
47
  end
@@ -101,3 +84,7 @@ module Faraday
101
84
  end
102
85
  end
103
86
  end
87
+
88
+ require 'faraday/response/json'
89
+ require 'faraday/response/logger'
90
+ require 'faraday/response/raise_error'
@@ -111,7 +111,7 @@ module Faraday
111
111
  def parse(header_string)
112
112
  return unless header_string && !header_string.empty?
113
113
 
114
- headers = header_string.split(/\r\n/)
114
+ headers = header_string.split("\r\n")
115
115
 
116
116
  # Find the last set of response headers.
117
117
  start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '1.10.6'
4
+ VERSION = '2.0.0.alpha-1'
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,26 +17,8 @@ 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/request/url_encoded' # needed by multipart
28
-
29
- # External Middleware gems and their aliases
30
- require 'faraday/multipart'
31
- require 'faraday/retry'
32
- Faraday::Request::Multipart = Faraday::Multipart::Middleware
33
- Faraday::Request::Retry = Faraday::Retry::Middleware
34
-
35
- # External Adapters gems
36
- unless defined?(JRUBY_VERSION)
37
- require 'faraday/em_http'
38
- require 'faraday/em_synchrony'
39
- end
40
- require 'faraday/excon'
41
- require 'faraday/httpclient'
42
- require 'faraday/net_http'
43
- require 'faraday/net_http_persistent'
44
- require 'faraday/patron'
45
- require 'faraday/rack'
20
+ require 'faraday/file_part'
21
+ require 'faraday/param_part'
46
22
 
47
23
  # This is the main namespace for Faraday.
48
24
  #
@@ -73,7 +49,7 @@ module Faraday
73
49
 
74
50
  # @overload default_adapter
75
51
  # Gets the Symbol key identifying a default Adapter to use
76
- # for the default {Faraday::Connection}. Defaults to `:net_http`.
52
+ # for the default {Faraday::Connection}. Defaults to `:test`.
77
53
  # @return [Symbol] the default adapter
78
54
  # @overload default_adapter=(adapter)
79
55
  # Updates default adapter while resetting {.default_connection}.
@@ -120,19 +96,6 @@ module Faraday
120
96
  Faraday::Connection.new(url, options, &block)
121
97
  end
122
98
 
123
- # @private
124
- # Internal: Requires internal Faraday libraries.
125
- #
126
- # @param libs [Array] one or more relative String names to Faraday classes.
127
- # @return [void]
128
- def require_libs(*libs)
129
- libs.each do |lib|
130
- require "#{lib_path}/#{lib}"
131
- end
132
- end
133
-
134
- alias require_lib require_libs
135
-
136
99
  # Documented elsewhere, see default_adapter reader
137
100
  def default_adapter=(adapter)
138
101
  @default_connection = nil
@@ -187,7 +150,5 @@ module Faraday
187
150
  self.ignore_env_proxy = false
188
151
  self.root_path = File.expand_path __dir__
189
152
  self.lib_path = File.expand_path 'faraday', __dir__
190
- self.default_adapter = :net_http
191
-
192
- require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
153
+ self.default_adapter = :test
193
154
  end
@@ -1,5 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ class CustomEncoder
4
+ def encode(params)
5
+ params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
6
+ end
7
+
8
+ def decode(params)
9
+ params.split(',').map { |pair| pair.split('-') }.to_h
10
+ end
11
+ end
12
+
3
13
  shared_examples 'initializer with url' do
4
14
  context 'with simple url' do
5
15
  let(:address) { 'http://sushi.com' }
@@ -103,6 +113,12 @@ RSpec.describe Faraday::Connection do
103
113
  it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
104
114
  end
105
115
 
116
+ context 'with basic_auth in url' do
117
+ let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
118
+
119
+ it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
120
+ end
121
+
106
122
  context 'with custom headers' do
107
123
  let(:options) { { headers: { user_agent: 'Faraday' } } }
108
124
 
@@ -124,7 +140,7 @@ RSpec.describe Faraday::Connection do
124
140
  context 'with block' do
125
141
  let(:conn) do
126
142
  Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
127
- faraday.adapter :net_http
143
+ faraday.adapter :test
128
144
  faraday.url_prefix = 'http://sushi.com/omnom'
129
145
  end
130
146
  end
@@ -141,28 +157,6 @@ RSpec.describe Faraday::Connection do
141
157
  end
142
158
  end
143
159
 
144
- describe 'basic_auth' do
145
- subject { conn }
146
-
147
- context 'calling the #basic_auth method' do
148
- before { subject.basic_auth 'Aladdin', 'open sesame' }
149
-
150
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
151
- end
152
-
153
- context 'adding basic auth info to url' do
154
- let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
155
-
156
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
157
- end
158
- end
159
-
160
- describe '#token_auth' do
161
- before { subject.token_auth('abcdef', nonce: 'abc') }
162
-
163
- it { expect(subject.headers['Authorization']).to eq('Token nonce="abc", token="abcdef"') }
164
- end
165
-
166
160
  describe '#build_exclusive_url' do
167
161
  context 'with relative path' do
168
162
  subject { conn.build_exclusive_url('sake.html') }
@@ -307,39 +301,6 @@ RSpec.describe Faraday::Connection do
307
301
  expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400')
308
302
  end
309
303
  end
310
-
311
- context 'with protocol-relative URL (CVE-2026-25765)' do
312
- it 'does not allow host override with //evil.com/path' do
313
- conn.url_prefix = 'http://httpbingo.org/api'
314
- uri = conn.build_exclusive_url('//evil.com/path')
315
- expect(uri.host).to eq('httpbingo.org')
316
- end
317
-
318
- it 'does not allow host override with //evil.com:8080/path' do
319
- conn.url_prefix = 'http://httpbingo.org/api'
320
- uri = conn.build_exclusive_url('//evil.com:8080/path')
321
- expect(uri.host).to eq('httpbingo.org')
322
- end
323
-
324
- it 'does not allow host override with //user:pass@evil.com/path' do
325
- conn.url_prefix = 'http://httpbingo.org/api'
326
- uri = conn.build_exclusive_url('//user:pass@evil.com/path')
327
- expect(uri.host).to eq('httpbingo.org')
328
- end
329
-
330
- it 'does not allow host override with ///evil.com' do
331
- conn.url_prefix = 'http://httpbingo.org/api'
332
- uri = conn.build_exclusive_url('///evil.com')
333
- expect(uri.host).to eq('httpbingo.org')
334
- end
335
-
336
- it 'still allows single-slash absolute paths' do
337
- conn.url_prefix = 'http://httpbingo.org/api'
338
- uri = conn.build_exclusive_url('/safe/path')
339
- expect(uri.host).to eq('httpbingo.org')
340
- expect(uri.path).to eq('/safe/path')
341
- end
342
- end
343
304
  end
344
305
 
345
306
  describe '#build_url' do
@@ -355,19 +316,6 @@ RSpec.describe Faraday::Connection do
355
316
  url = conn.build_url(nil, b: 2, c: 3)
356
317
  expect(url.to_s).to eq('http://sushi.com/nigiri?a=1&b=2&c=3')
357
318
  end
358
-
359
- it 'raises a controlled error when URL query params exceed the nested depth limit' do
360
- original_param_depth_limit = Faraday::NestedParamsEncoder.param_depth_limit
361
- begin
362
- Faraday::NestedParamsEncoder.param_depth_limit = 2
363
- expect { conn.build_url('/nigiri?a[b][c]=1') }.to raise_error(
364
- Faraday::Error,
365
- 'exceeded nested parameter depth limit of 2'
366
- )
367
- ensure
368
- Faraday::NestedParamsEncoder.param_depth_limit = original_param_depth_limit
369
- end
370
- end
371
319
  end
372
320
 
373
321
  describe '#build_request' do
@@ -602,26 +550,32 @@ RSpec.describe Faraday::Connection do
602
550
  end
603
551
 
604
552
  context 'performing a request' do
605
- before { stub_request(:get, 'http://example.com') }
553
+ let(:url) { 'http://example.com' }
554
+ let(:conn) do
555
+ Faraday.new do |f|
556
+ f.adapter :test do |stubs|
557
+ stubs.get(url) do
558
+ [200, {}, 'ok']
559
+ end
560
+ end
561
+ end
562
+ end
606
563
 
607
564
  it 'dynamically checks proxy' do
608
565
  with_env 'http_proxy' => 'http://proxy.com:80' do
609
- conn = Faraday.new
610
566
  expect(conn.proxy.uri.host).to eq('proxy.com')
611
567
 
612
- conn.get('http://example.com') do |req|
568
+ conn.get(url) do |req|
613
569
  expect(req.options.proxy.uri.host).to eq('proxy.com')
614
570
  end
615
571
  end
616
572
 
617
- conn.get('http://example.com')
573
+ conn.get(url)
618
574
  expect(conn.instance_variable_get('@temp_proxy')).to be_nil
619
575
  end
620
576
 
621
577
  it 'dynamically check no proxy' do
622
578
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
623
- conn = Faraday.new
624
-
625
579
  expect(conn.proxy.uri.host).to eq('proxy.com')
626
580
 
627
581
  conn.get('http://example.com') do |req|
@@ -651,7 +605,6 @@ RSpec.describe Faraday::Connection do
651
605
 
652
606
  context 'after manual changes' do
653
607
  before do
654
- subject.basic_auth('', '')
655
608
  subject.headers['content-length'] = 12
656
609
  subject.params['b'] = '2'
657
610
  subject.options[:open_timeout] = 10
@@ -691,9 +644,16 @@ RSpec.describe Faraday::Connection do
691
644
  describe 'request params' do
692
645
  context 'with simple url' do
693
646
  let(:url) { 'http://example.com' }
694
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
647
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
648
+
649
+ before do
650
+ conn.adapter(:test, stubs)
651
+ stubs.get('http://example.com?a=a&p=3') do
652
+ [200, {}, 'ok']
653
+ end
654
+ end
695
655
 
696
- after { expect(stubbed).to have_been_made.once }
656
+ after { stubs.verify_stubbed_calls }
697
657
 
698
658
  it 'test_overrides_request_params' do
699
659
  conn.get('?p=2&a=a', p: 3)
@@ -715,15 +675,22 @@ RSpec.describe Faraday::Connection do
715
675
  context 'with url and extra params' do
716
676
  let(:url) { 'http://example.com?a=1&b=2' }
717
677
  let(:options) { { params: { c: 3 } } }
678
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
679
+
680
+ before do
681
+ conn.adapter(:test, stubs)
682
+ end
718
683
 
719
684
  it 'merges connection and request params' do
720
- stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
685
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
686
+ stubs.get(expected) { [200, {}, 'ok'] }
721
687
  conn.get('?page=1', limit: 5)
722
- expect(stubbed).to have_been_made.once
688
+ stubs.verify_stubbed_calls
723
689
  end
724
690
 
725
691
  it 'allows to override all params' do
726
- stubbed = stub_request(:get, 'http://example.com?b=b')
692
+ expected = 'http://example.com?b=b'
693
+ stubs.get(expected) { [200, {}, 'ok'] }
727
694
  conn.get('?p=1&a=a', p: 2) do |req|
728
695
  expect(req.params[:a]).to eq('a')
729
696
  expect(req.params['c']).to eq(3)
@@ -731,47 +698,61 @@ RSpec.describe Faraday::Connection do
731
698
  req.params = { b: 'b' }
732
699
  expect(req.params['b']).to eq('b')
733
700
  end
734
- expect(stubbed).to have_been_made.once
701
+ stubs.verify_stubbed_calls
735
702
  end
736
703
 
737
704
  it 'allows to set params_encoder for single request' do
738
- encoder = Object.new
739
- def encoder.encode(params)
740
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
741
- end
742
- stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
705
+ encoder = CustomEncoder.new
706
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
707
+ stubs.get(expected) { [200, {}, 'ok'] }
743
708
 
744
- conn.get('/', feeling: 'blue') do |req|
709
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
745
710
  req.options.params_encoder = encoder
746
711
  end
747
- expect(stubbed).to have_been_made.once
712
+ stubs.verify_stubbed_calls
748
713
  end
749
714
  end
750
715
 
751
716
  context 'with default params encoder' do
752
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
753
- after { expect(stubbed).to have_been_made.once }
717
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
718
+
719
+ before do
720
+ conn.adapter(:test, stubs)
721
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
722
+ [200, {}, 'ok']
723
+ end
724
+ end
725
+
726
+ after { stubs.verify_stubbed_calls }
754
727
 
755
728
  it 'supports array params in url' do
756
- conn.get('http://example.com?color[]=red&color[]=blue')
729
+ conn.get('http://example.com?color[]=blue&color[]=red')
757
730
  end
758
731
 
759
732
  it 'supports array params in params' do
760
- conn.get('http://example.com', color: %w[red blue])
733
+ conn.get('http://example.com', color: %w[blue red])
761
734
  end
762
735
  end
763
736
 
764
737
  context 'with flat params encoder' do
765
738
  let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
766
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
767
- after { expect(stubbed).to have_been_made.once }
739
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
740
+
741
+ before do
742
+ conn.adapter(:test, stubs)
743
+ stubs.get('http://example.com?color=blue&color=red') do
744
+ [200, {}, 'ok']
745
+ end
746
+ end
747
+
748
+ after { stubs.verify_stubbed_calls }
768
749
 
769
750
  it 'supports array params in params' do
770
- conn.get('http://example.com', color: %w[red blue])
751
+ conn.get('http://example.com', color: %w[blue red])
771
752
  end
772
753
 
773
754
  context 'with array param in url' do
774
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
755
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
775
756
 
776
757
  it do
777
758
  conn.get('/')
@@ -29,12 +29,12 @@ RSpec.describe Faraday::Env do
29
29
 
30
30
  it 'retains custom members' do
31
31
  env[:foo] = 'custom 1'
32
- env[:bar] = :custom_2
32
+ env[:bar] = :custom2
33
33
  env2 = Faraday::Env.from(env)
34
34
  env2[:baz] = 'custom 3'
35
35
 
36
36
  expect(env2[:foo]).to eq('custom 1')
37
- expect(env2[:bar]).to eq(:custom_2)
37
+ expect(env2[:bar]).to eq(:custom2)
38
38
  expect(env[:baz]).to be_nil
39
39
  end
40
40
 
@@ -5,15 +5,6 @@ require 'rack/utils'
5
5
  RSpec.describe Faraday::NestedParamsEncoder do
6
6
  it_behaves_like 'a params encoder'
7
7
 
8
- around do |example|
9
- original_param_depth_limit = described_class.param_depth_limit
10
- begin
11
- example.run
12
- ensure
13
- described_class.param_depth_limit = original_param_depth_limit
14
- end
15
- end
16
-
17
8
  it 'decodes arrays' do
18
9
  query = 'a[1]=one&a[2]=two&a[3]=three'
19
10
  expected = { 'a' => %w[one two three] }
@@ -68,27 +59,6 @@ RSpec.describe Faraday::NestedParamsEncoder do
68
59
  expect(subject.decode(query)).to eq(expected)
69
60
  end
70
61
 
71
- it 'allows nested params within the configured depth limit' do
72
- described_class.param_depth_limit = 3
73
-
74
- expect(subject.decode('a[b][c]=1')).to eq({ 'a' => { 'b' => { 'c' => '1' } } })
75
- end
76
-
77
- it 'raises a controlled error when nested params exceed the depth limit' do
78
- described_class.param_depth_limit = 2
79
-
80
- expect { subject.decode('a[b][c]=1') }.to raise_error(
81
- Faraday::Error,
82
- 'exceeded nested parameter depth limit of 2'
83
- )
84
- end
85
-
86
- it 'allows disabling the nested params depth limit' do
87
- described_class.param_depth_limit = nil
88
-
89
- expect(subject.decode('a[b][c][d]=1')).to eq({ 'a' => { 'b' => { 'c' => { 'd' => '1' } } } })
90
- end
91
-
92
62
  it 'encodes rack compat' do
93
63
  params = { a: [{ one: '1', two: '2' }, '3', ''] }
94
64
  result = Faraday::Utils.unescape(Faraday::NestedParamsEncoder.encode(params)).split('&')