faraday 1.8.0 → 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 (60) 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 -84
  8. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  9. data/lib/faraday/error.rb +1 -0
  10. data/lib/faraday/file_part.rb +0 -6
  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/rack_builder.rb +1 -1
  16. data/lib/faraday/request/authorization.rb +26 -40
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/multipart.rb +2 -0
  20. data/lib/faraday/request/retry.rb +3 -1
  21. data/lib/faraday/request/url_encoded.rb +2 -0
  22. data/lib/faraday/request.rb +13 -31
  23. data/lib/faraday/response/json.rb +54 -0
  24. data/lib/faraday/response/logger.rb +4 -4
  25. data/lib/faraday/response/raise_error.rb +9 -1
  26. data/lib/faraday/response.rb +8 -19
  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 -37
  31. data/spec/faraday/connection_spec.rb +78 -51
  32. data/spec/faraday/options/env_spec.rb +2 -2
  33. data/spec/faraday/rack_builder_spec.rb +5 -43
  34. data/spec/faraday/request/authorization_spec.rb +14 -36
  35. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  36. data/spec/faraday/request/json_spec.rb +111 -0
  37. data/spec/faraday/request/multipart_spec.rb +5 -5
  38. data/spec/faraday/request/retry_spec.rb +13 -1
  39. data/spec/faraday/request_spec.rb +0 -11
  40. data/spec/faraday/response/json_spec.rb +117 -0
  41. data/spec/faraday/response/raise_error_spec.rb +7 -4
  42. data/spec/faraday/utils_spec.rb +1 -1
  43. data/spec/support/fake_safe_buffer.rb +1 -1
  44. data/spec/support/shared_examples/request_method.rb +5 -5
  45. metadata +11 -134
  46. data/lib/faraday/adapter/typhoeus.rb +0 -15
  47. data/lib/faraday/autoload.rb +0 -87
  48. data/lib/faraday/dependency_loader.rb +0 -37
  49. data/lib/faraday/request/basic_authentication.rb +0 -20
  50. data/lib/faraday/request/token_authentication.rb +0 -20
  51. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  52. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  53. data/spec/faraday/adapter/excon_spec.rb +0 -49
  54. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  55. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  56. data/spec/faraday/adapter/patron_spec.rb +0 -18
  57. data/spec/faraday/adapter/rack_spec.rb +0 -8
  58. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  59. data/spec/faraday/response/middleware_spec.rb +0 -68
  60. data/spec/support/webmock_rack_app.rb +0 -68
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Faraday
6
+ class Request
7
+ # Request middleware that encodes the body as JSON.
8
+ #
9
+ # Processes only requests with matching Content-type or those without a type.
10
+ # If a request doesn't have a type but has a body, it sets the Content-type
11
+ # to JSON MIME-type.
12
+ #
13
+ # Doesn't try to encode bodies that already are in string form.
14
+ class Json < Middleware
15
+ MIME_TYPE = 'application/json'
16
+ MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
17
+
18
+ def on_request(env)
19
+ match_content_type(env) do |data|
20
+ env[:body] = encode(data)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def encode(data)
27
+ ::JSON.generate(data)
28
+ end
29
+
30
+ def match_content_type(env)
31
+ return unless process_request?(env)
32
+
33
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
34
+ yield env[:body] unless env[:body].respond_to?(:to_str)
35
+ end
36
+
37
+ def process_request?(env)
38
+ type = request_type(env)
39
+ body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
40
+ end
41
+
42
+ def body?(env)
43
+ (body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
44
+ end
45
+
46
+ def request_type(env)
47
+ type = env[:request_headers][CONTENT_TYPE].to_s
48
+ type = type.split(';', 2).first if type.index(';')
49
+ type
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ Faraday::Request.register_middleware(json: Faraday::Request::Json)
@@ -104,3 +104,5 @@ module Faraday
104
104
  end
105
105
  end
106
106
  end
107
+
108
+ Faraday::Request.register_middleware(multipart: Faraday::Request::Multipart)
@@ -184,7 +184,7 @@ module Faraday
184
184
  if ex.is_a? Module
185
185
  error.is_a? ex
186
186
  else
187
- error.class.to_s == ex.to_s
187
+ Object.const_defined?(ex.to_s) && error.is_a?(Object.const_get(ex.to_s))
188
188
  end
189
189
  end
190
190
  end
@@ -237,3 +237,5 @@ module Faraday
237
237
  end
238
238
  end
239
239
  end
240
+
241
+ Faraday::Request.register_middleware(retry: Faraday::Request::Retry)
@@ -54,3 +54,5 @@ module Faraday
54
54
  end
55
55
  end
56
56
  end
57
+
58
+ Faraday::Request.register_middleware(url_encoded: Faraday::Request::UrlEncoded)
@@ -26,28 +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
- multipart: [:Multipart, 'multipart'],
39
- retry: [:Retry, 'retry'],
40
- authorization: [:Authorization, 'authorization'],
41
- basic_auth: [
42
- :BasicAuthentication,
43
- 'basic_authentication'
44
- ],
45
- token_auth: [
46
- :TokenAuthentication,
47
- 'token_authentication'
48
- ],
49
- instrumentation: [:Instrumentation, 'instrumentation']
50
-
51
34
  # @param request_method [String]
52
35
  # @yield [request] for block customization, if block given
53
36
  # @yieldparam request [Request]
@@ -58,14 +41,6 @@ module Faraday
58
41
  end
59
42
  end
60
43
 
61
- def method
62
- warn <<~TEXT
63
- WARNING: `Faraday::Request##{__method__}` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.
64
- `Faraday::Request##{__method__}` called from #{caller_locations(1..1).first}
65
- TEXT
66
- http_method
67
- end
68
-
69
44
  # Replace params, preserving the existing hash type.
70
45
  #
71
46
  # @param hash [Hash] new params
@@ -140,11 +115,11 @@ module Faraday
140
115
  # @param serialised [Hash] the serialised object.
141
116
  def marshal_load(serialised)
142
117
  self.http_method = serialised[:http_method]
143
- self.body = serialised[:body]
144
- self.headers = serialised[:headers]
145
- self.path = serialised[:path]
146
- self.params = serialised[:params]
147
- self.options = serialised[:options]
118
+ self.body = serialised[:body]
119
+ self.headers = serialised[:headers]
120
+ self.path = serialised[:path]
121
+ self.params = serialised[:params]
122
+ self.options = serialised[:options]
148
123
  end
149
124
 
150
125
  # @return [Env] the Env for this Request
@@ -154,3 +129,10 @@ module Faraday
154
129
  end
155
130
  end
156
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'
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Faraday
6
+ class Response
7
+ # Parse response bodies as JSON.
8
+ class Json < Middleware
9
+ def initialize(app = nil, parser_options: nil, content_type: /\bjson$/, preserve_raw: false)
10
+ super(app)
11
+ @parser_options = parser_options
12
+ @content_types = Array(content_type)
13
+ @preserve_raw = preserve_raw
14
+ end
15
+
16
+ def on_complete(env)
17
+ process_response(env) if parse_response?(env)
18
+ end
19
+
20
+ private
21
+
22
+ def process_response(env)
23
+ env[:raw_body] = env[:body] if @preserve_raw
24
+ env[:body] = parse(env[:body])
25
+ rescue StandardError, SyntaxError => e
26
+ raise Faraday::ParsingError.new(e, env[:response])
27
+ end
28
+
29
+ def parse(body)
30
+ ::JSON.parse(body, @parser_options || {}) unless body.strip.empty?
31
+ end
32
+
33
+ def parse_response?(env)
34
+ process_response_type?(env) &&
35
+ env[:body].respond_to?(:to_str)
36
+ end
37
+
38
+ def process_response_type?(env)
39
+ type = response_type(env)
40
+ @content_types.empty? || @content_types.any? do |pattern|
41
+ pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
42
+ end
43
+ end
44
+
45
+ def response_type(env)
46
+ type = env[:response_headers][CONTENT_TYPE].to_s
47
+ type = type.split(';', 2).first if type.index(';')
48
+ type
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ Faraday::Response.register_middleware(json: Faraday::Response::Json)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'forwardable'
4
+ require 'logger'
4
5
  require 'faraday/logging/formatter'
5
6
 
6
7
  module Faraday
@@ -11,10 +12,7 @@ module Faraday
11
12
  class Logger < Middleware
12
13
  def initialize(app, logger = nil, options = {})
13
14
  super(app)
14
- logger ||= begin
15
- require 'logger'
16
- ::Logger.new($stdout)
17
- end
15
+ logger ||= ::Logger.new($stdout)
18
16
  formatter_class = options.delete(:formatter) || Logging::Formatter
19
17
  @formatter = formatter_class.new(logger: logger, options: options)
20
18
  yield @formatter if block_given?
@@ -31,3 +29,5 @@ module Faraday
31
29
  end
32
30
  end
33
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,25 +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
-
27
11
  def initialize(env = nil)
28
12
  @env = Env.from(env) if env
29
13
  @on_complete_callbacks = []
@@ -42,6 +26,7 @@ module Faraday
42
26
  def headers
43
27
  finished? ? env.response_headers : {}
44
28
  end
29
+
45
30
  def_delegator :headers, :[]
46
31
 
47
32
  def body
@@ -53,10 +38,10 @@ module Faraday
53
38
  end
54
39
 
55
40
  def on_complete(&block)
56
- if !finished?
57
- @on_complete_callbacks << block
58
- else
41
+ if finished?
59
42
  yield(env)
43
+ else
44
+ @on_complete_callbacks << block
60
45
  end
61
46
  self
62
47
  end
@@ -99,3 +84,7 @@ module Faraday
99
84
  end
100
85
  end
101
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.8.0'
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,21 +17,9 @@ require 'faraday/middleware'
23
17
  require 'faraday/adapter'
24
18
  require 'faraday/request'
25
19
  require 'faraday/response'
26
- require 'faraday/error'
27
20
  require 'faraday/file_part'
28
21
  require 'faraday/param_part'
29
22
 
30
- unless defined?(JRUBY_VERSION)
31
- require 'faraday/em_http'
32
- require 'faraday/em_synchrony'
33
- end
34
- require 'faraday/excon'
35
- require 'faraday/httpclient'
36
- require 'faraday/net_http'
37
- require 'faraday/net_http_persistent'
38
- require 'faraday/patron'
39
- require 'faraday/rack'
40
-
41
23
  # This is the main namespace for Faraday.
42
24
  #
43
25
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -51,6 +33,8 @@ require 'faraday/rack'
51
33
  # conn.get '/'
52
34
  #
53
35
  module Faraday
36
+ CONTENT_TYPE = 'Content-Type'
37
+
54
38
  class << self
55
39
  # The root path that Faraday is being loaded from.
56
40
  #
@@ -65,7 +49,7 @@ module Faraday
65
49
 
66
50
  # @overload default_adapter
67
51
  # Gets the Symbol key identifying a default Adapter to use
68
- # for the default {Faraday::Connection}. Defaults to `:net_http`.
52
+ # for the default {Faraday::Connection}. Defaults to `:test`.
69
53
  # @return [Symbol] the default adapter
70
54
  # @overload default_adapter=(adapter)
71
55
  # Updates default adapter while resetting {.default_connection}.
@@ -112,19 +96,6 @@ module Faraday
112
96
  Faraday::Connection.new(url, options, &block)
113
97
  end
114
98
 
115
- # @private
116
- # Internal: Requires internal Faraday libraries.
117
- #
118
- # @param libs [Array] one or more relative String names to Faraday classes.
119
- # @return [void]
120
- def require_libs(*libs)
121
- libs.each do |lib|
122
- require "#{lib_path}/#{lib}"
123
- end
124
- end
125
-
126
- alias require_lib require_libs
127
-
128
99
  # Documented elsewhere, see default_adapter reader
129
100
  def default_adapter=(adapter)
130
101
  @default_connection = nil
@@ -179,7 +150,5 @@ module Faraday
179
150
  self.ignore_env_proxy = false
180
151
  self.root_path = File.expand_path __dir__
181
152
  self.lib_path = File.expand_path 'faraday', __dir__
182
- self.default_adapter = :net_http
183
-
184
- require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
153
+ self.default_adapter = :test
185
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') }
@@ -556,26 +550,32 @@ RSpec.describe Faraday::Connection do
556
550
  end
557
551
 
558
552
  context 'performing a request' do
559
- 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
560
563
 
561
564
  it 'dynamically checks proxy' do
562
565
  with_env 'http_proxy' => 'http://proxy.com:80' do
563
- conn = Faraday.new
564
566
  expect(conn.proxy.uri.host).to eq('proxy.com')
565
567
 
566
- conn.get('http://example.com') do |req|
568
+ conn.get(url) do |req|
567
569
  expect(req.options.proxy.uri.host).to eq('proxy.com')
568
570
  end
569
571
  end
570
572
 
571
- conn.get('http://example.com')
573
+ conn.get(url)
572
574
  expect(conn.instance_variable_get('@temp_proxy')).to be_nil
573
575
  end
574
576
 
575
577
  it 'dynamically check no proxy' do
576
578
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
577
- conn = Faraday.new
578
-
579
579
  expect(conn.proxy.uri.host).to eq('proxy.com')
580
580
 
581
581
  conn.get('http://example.com') do |req|
@@ -605,7 +605,6 @@ RSpec.describe Faraday::Connection do
605
605
 
606
606
  context 'after manual changes' do
607
607
  before do
608
- subject.basic_auth('', '')
609
608
  subject.headers['content-length'] = 12
610
609
  subject.params['b'] = '2'
611
610
  subject.options[:open_timeout] = 10
@@ -645,9 +644,16 @@ RSpec.describe Faraday::Connection do
645
644
  describe 'request params' do
646
645
  context 'with simple url' do
647
646
  let(:url) { 'http://example.com' }
648
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
647
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
649
648
 
650
- after { expect(stubbed).to have_been_made.once }
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
655
+
656
+ after { stubs.verify_stubbed_calls }
651
657
 
652
658
  it 'test_overrides_request_params' do
653
659
  conn.get('?p=2&a=a', p: 3)
@@ -669,15 +675,22 @@ RSpec.describe Faraday::Connection do
669
675
  context 'with url and extra params' do
670
676
  let(:url) { 'http://example.com?a=1&b=2' }
671
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
672
683
 
673
684
  it 'merges connection and request params' do
674
- 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'] }
675
687
  conn.get('?page=1', limit: 5)
676
- expect(stubbed).to have_been_made.once
688
+ stubs.verify_stubbed_calls
677
689
  end
678
690
 
679
691
  it 'allows to override all params' do
680
- stubbed = stub_request(:get, 'http://example.com?b=b')
692
+ expected = 'http://example.com?b=b'
693
+ stubs.get(expected) { [200, {}, 'ok'] }
681
694
  conn.get('?p=1&a=a', p: 2) do |req|
682
695
  expect(req.params[:a]).to eq('a')
683
696
  expect(req.params['c']).to eq(3)
@@ -685,47 +698,61 @@ RSpec.describe Faraday::Connection do
685
698
  req.params = { b: 'b' }
686
699
  expect(req.params['b']).to eq('b')
687
700
  end
688
- expect(stubbed).to have_been_made.once
701
+ stubs.verify_stubbed_calls
689
702
  end
690
703
 
691
704
  it 'allows to set params_encoder for single request' do
692
- encoder = Object.new
693
- def encoder.encode(params)
694
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
695
- end
696
- 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'] }
697
708
 
698
- conn.get('/', feeling: 'blue') do |req|
709
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
699
710
  req.options.params_encoder = encoder
700
711
  end
701
- expect(stubbed).to have_been_made.once
712
+ stubs.verify_stubbed_calls
702
713
  end
703
714
  end
704
715
 
705
716
  context 'with default params encoder' do
706
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
707
- 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 }
708
727
 
709
728
  it 'supports array params in url' do
710
- conn.get('http://example.com?color[]=red&color[]=blue')
729
+ conn.get('http://example.com?color[]=blue&color[]=red')
711
730
  end
712
731
 
713
732
  it 'supports array params in params' do
714
- conn.get('http://example.com', color: %w[red blue])
733
+ conn.get('http://example.com', color: %w[blue red])
715
734
  end
716
735
  end
717
736
 
718
737
  context 'with flat params encoder' do
719
738
  let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
720
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
721
- 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 }
722
749
 
723
750
  it 'supports array params in params' do
724
- conn.get('http://example.com', color: %w[red blue])
751
+ conn.get('http://example.com', color: %w[blue red])
725
752
  end
726
753
 
727
754
  context 'with array param in url' do
728
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
755
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
729
756
 
730
757
  it do
731
758
  conn.get('/')