faraday 1.7.0 → 1.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0b66c2e6b13140f2093c73a7b8dab39dd25690b15b1e5eadaba90a787d2fdf5
4
- data.tar.gz: af34783d38f124632f2e4edcfd7f6bf2d763bd5d6f36ad8b6de6cac675c9858d
3
+ metadata.gz: fa04fbf791435b105f33ae2772bfbe8527a24f4b5ec429fe74bd205627fdc39b
4
+ data.tar.gz: 5b0be01323591c9628daf113adf8723b2a0396c8c6dbf10440536530def8faf2
5
5
  SHA512:
6
- metadata.gz: dd547129c965998e13208a5a0accab40ddee955f7a001365f2a951c6a0e7d7fefc9e61c5d708188deff9e8e239c991627485e3f07e9686a87a3cd30938a8f05b
7
- data.tar.gz: 81fdc31d08d17db983ea4a4c68951942c3673d5447096e1f51cd0f4eebac1289348b860a56e064131b65ef59e3ed6f63ce1fc004db435f7fb051460b98190b31
6
+ metadata.gz: d7edb34be0a6efc85503a80923b2d109504263cf9042f047dedfdba750f55756ba8d153b23b7774c53f4d6e136a77ab1c2ad82e29099564646bd257d5e311281
7
+ data.tar.gz: 05115f5198bbc8de3d4eee3d15c42eefbe7d6ce576f930486c1f4fb1daf79b4223e345adf16e0bc6ef2913d90df73fd069314dfcbeb33fea2ad4108b4ad7ba92
@@ -81,4 +81,17 @@ RSpec.describe Client do
81
81
  stubs.verify_stubbed_calls
82
82
  end
83
83
  end
84
+
85
+ context 'When the Faraday connection is configured with FlatParamsEncoder' do
86
+ let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
87
+
88
+ it 'handles the same multiple URL parameters' do
89
+ stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
90
+
91
+ # uncomment to raise Stubs::NotFound
92
+ # expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
93
+ expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
94
+ stubs.verify_stubbed_calls
95
+ end
96
+ end
84
97
  end
@@ -13,8 +13,8 @@ class Client
13
13
  @conn = conn
14
14
  end
15
15
 
16
- def sushi(jname)
17
- res = @conn.get("/#{jname}")
16
+ def sushi(jname, params: {})
17
+ res = @conn.get("/#{jname}", params)
18
18
  data = JSON.parse(res.body)
19
19
  data['name']
20
20
  end
@@ -70,6 +70,45 @@ class ClientTest < Test::Unit::TestCase
70
70
  stubs.verify_stubbed_calls
71
71
  end
72
72
 
73
+ def test_strict_mode
74
+ stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
75
+ stubs.get('/ebi?abc=123') do
76
+ [
77
+ 200,
78
+ { 'Content-Type': 'application/javascript' },
79
+ '{"name": "shrimp"}'
80
+ ]
81
+ end
82
+
83
+ cli = client(stubs)
84
+ assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
85
+
86
+ # uncomment to raise Stubs::NotFound
87
+ # assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
88
+ stubs.verify_stubbed_calls
89
+ end
90
+
91
+ def test_non_default_params_encoder
92
+ stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
93
+ stubs.get('/ebi?a=x&a=y&a=z') do
94
+ [
95
+ 200,
96
+ { 'Content-Type': 'application/javascript' },
97
+ '{"name": "shrimp"}'
98
+ ]
99
+ end
100
+ conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
101
+ builder.adapter :test, stubs
102
+ end
103
+
104
+ cli = Client.new(conn)
105
+ assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
106
+
107
+ # uncomment to raise Stubs::NotFound
108
+ # assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
109
+ stubs.verify_stubbed_calls
110
+ end
111
+
73
112
  def client(stubs)
74
113
  conn = Faraday.new do |builder|
75
114
  builder.adapter :test, stubs
@@ -62,18 +62,20 @@ module Faraday
62
62
  @stack.empty?
63
63
  end
64
64
 
65
- def match(request_method, host, path, headers, body)
65
+ # @param env [Faraday::Env]
66
+ def match(env)
67
+ request_method = env[:method]
66
68
  return false unless @stack.key?(request_method)
67
69
 
68
70
  stack = @stack[request_method]
69
71
  consumed = (@consumed[request_method] ||= [])
70
72
 
71
- stub, meta = matches?(stack, host, path, headers, body)
73
+ stub, meta = matches?(stack, env)
72
74
  if stub
73
75
  consumed << stack.delete(stub)
74
76
  return stub, meta
75
77
  end
76
- matches?(consumed, host, path, headers, body)
78
+ matches?(consumed, env)
77
79
  end
78
80
 
79
81
  def get(path, headers = {}, &block)
@@ -142,15 +144,18 @@ module Faraday
142
144
  Faraday::Utils.URI(path).host
143
145
  ]
144
146
  end
145
-
147
+ path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
146
148
  headers = Utils::Headers.new(headers)
147
- stub = Stub.new(host, normalized_path, headers, body, @strict_mode, block)
149
+
150
+ stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
148
151
  (@stack[request_method] ||= []) << stub
149
152
  end
150
153
 
151
- def matches?(stack, host, path, headers, body)
154
+ # @param stack [Hash]
155
+ # @param env [Faraday::Env]
156
+ def matches?(stack, env)
152
157
  stack.each do |stub|
153
- match_result, meta = stub.matches?(host, path, headers, body)
158
+ match_result, meta = stub.matches?(env)
154
159
  return stub, meta if match_result
155
160
  end
156
161
  nil
@@ -158,35 +163,20 @@ module Faraday
158
163
  end
159
164
 
160
165
  # Stub request
161
- # rubocop:disable Style/StructInheritance
162
- class Stub < Struct.new(:host, :path, :params, :headers, :body, :strict_mode, :block)
163
- # rubocop:enable Style/StructInheritance
164
- def initialize(host, full, headers, body, strict_mode, block) # rubocop:disable Metrics/ParameterLists
165
- path, query = full.respond_to?(:split) ? full.split('?') : full
166
- params =
167
- if query
168
- Faraday::Utils.parse_nested_query(query)
169
- else
170
- {}
171
- end
166
+ class Stub < Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) # rubocop:disable Style/StructInheritance
167
+ # @param env [Faraday::Env]
168
+ def matches?(env)
169
+ request_host = env[:url].host
170
+ request_path = Faraday::Utils.normalize_path(env[:url].path)
171
+ request_headers = env.request_headers
172
+ request_body = env[:body]
172
173
 
173
- super(host, path, params, headers, body, strict_mode, block)
174
- end
175
-
176
- def matches?(request_host, request_uri, request_headers, request_body)
177
- request_path, request_query = request_uri.split('?')
178
- request_params =
179
- if request_query
180
- Faraday::Utils.parse_nested_query(request_query)
181
- else
182
- {}
183
- end
184
174
  # meta is a hash used as carrier
185
175
  # that will be yielded to consumer block
186
176
  meta = {}
187
177
  [(host.nil? || host == request_host) &&
188
178
  path_match?(request_path, meta) &&
189
- params_match?(request_params) &&
179
+ params_match?(env) &&
190
180
  (body.to_s.size.zero? || request_body == body) &&
191
181
  headers_match?(request_headers), meta]
192
182
  end
@@ -199,7 +189,11 @@ module Faraday
199
189
  end
200
190
  end
201
191
 
202
- def params_match?(request_params)
192
+ # @param env [Faraday::Env]
193
+ def params_match?(env)
194
+ request_params = env[:params]
195
+ params = env.params_encoder.decode(query) || {}
196
+
203
197
  if strict_mode
204
198
  return Set.new(params) == Set.new(request_params)
205
199
  end
@@ -239,26 +233,19 @@ module Faraday
239
233
  yield(stubs)
240
234
  end
241
235
 
236
+ # @param env [Faraday::Env]
242
237
  def call(env)
243
238
  super
244
- host = env[:url].host
245
- normalized_path = Faraday::Utils.normalize_path(env[:url])
246
- params_encoder = env.request.params_encoder ||
247
- Faraday::Utils.default_params_encoder
248
239
 
249
- stub, meta = stubs.match(env[:method], host, normalized_path,
250
- env.request_headers, env[:body])
240
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
241
+ env[:params] = env.params_encoder.decode(env[:url].query) || {}
242
+ stub, meta = stubs.match(env)
251
243
 
252
244
  unless stub
253
245
  raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
254
- "#{normalized_path} #{env[:body]}"
246
+ "#{env[:url]} #{env[:body]}"
255
247
  end
256
248
 
257
- env[:params] = if (query = env[:url].query)
258
- params_encoder.decode(query)
259
- else
260
- {}
261
- end
262
249
  block_arity = stub.block.arity
263
250
  status, headers, body =
264
251
  if block_arity >= 0
@@ -297,6 +297,11 @@ module Faraday
297
297
  #
298
298
  # @return [void]
299
299
  def basic_auth(login, pass)
300
+ warn <<~TEXT
301
+ WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
302
+ While initializing your connection, use `#request(:basic_auth, ...)` instead.
303
+ See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
304
+ TEXT
300
305
  set_authorization_header(:basic_auth, login, pass)
301
306
  end
302
307
 
@@ -314,6 +319,11 @@ module Faraday
314
319
  #
315
320
  # @return [void]
316
321
  def token_auth(token, options = nil)
322
+ warn <<~TEXT
323
+ WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0.
324
+ While initializing your connection, use `#request(:token_auth, ...)` instead.
325
+ See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
326
+ TEXT
317
327
  set_authorization_header(:token_auth, token, options)
318
328
  end
319
329
 
@@ -336,6 +346,11 @@ module Faraday
336
346
  #
337
347
  # @return [void]
338
348
  def authorization(type, token)
349
+ warn <<~TEXT
350
+ WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
351
+ While initializing your connection, use `#request(:authorization, ...)` instead.
352
+ See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
353
+ TEXT
339
354
  set_authorization_header(:authorization, type, token)
340
355
  end
341
356
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '1.7.0'
4
+ VERSION = '1.7.1'
5
5
  end
@@ -258,6 +258,38 @@ RSpec.describe Faraday::Adapter::Test do
258
258
  end
259
259
  end
260
260
 
261
+ describe 'for request with non default params encoder' do
262
+ let(:connection) do
263
+ Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
264
+ builder.adapter :test, stubs
265
+ end
266
+ end
267
+ let(:stubs) do
268
+ described_class::Stubs.new do |stubs|
269
+ stubs.get('/path?a=x&a=y&a=z') { [200, {}, 'a'] }
270
+ end
271
+ end
272
+
273
+ context 'when all flat param values are correctly set' do
274
+ subject(:request) { connection.get('/path?a=x&a=y&a=z') }
275
+
276
+ it { expect(request.status).to eq 200 }
277
+ end
278
+
279
+ shared_examples 'raise NotFound when params do not satisfy the flat param values' do |params|
280
+ subject(:request) { connection.get('/path', params) }
281
+
282
+ context "with #{params.inspect}" do
283
+ it { expect { request }.to raise_error described_class::Stubs::NotFound }
284
+ end
285
+ end
286
+
287
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x] }
288
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x y] }
289
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x z y] } # NOTE: The order of the value is also compared.
290
+ it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { b: %w[x y z] }
291
+ end
292
+
261
293
  describe 'strict_mode' do
262
294
  let(:stubs) do
263
295
  described_class::Stubs.new(strict_mode: true) do |stubs|
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: 1.7.0
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-08-09 00:00:00.000000000 Z
13
+ date: 2021-08-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday-em_http
@@ -260,7 +260,7 @@ licenses:
260
260
  - MIT
261
261
  metadata:
262
262
  homepage_uri: https://lostisland.github.io/faraday
263
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.7.0
263
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.7.1
264
264
  source_code_uri: https://github.com/lostisland/faraday
265
265
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
266
266
  post_install_message: