faraday 1.4.2 → 1.9.0
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 +4 -4
- data/examples/client_spec.rb +34 -2
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/adapter.rb +1 -4
- data/lib/faraday/autoload.rb +1 -4
- data/lib/faraday/connection.rb +26 -5
- data/lib/faraday/error.rb +0 -6
- data/lib/faraday/request/authorization.rb +14 -7
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +13 -4
- data/spec/faraday/adapter/em_http_spec.rb +39 -37
- data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
- data/spec/faraday/adapter/test_spec.rb +117 -0
- data/spec/faraday/connection_spec.rb +7 -0
- data/spec/faraday/request/authorization_spec.rb +8 -0
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/support/webmock_rack_app.rb +1 -2
- metadata +66 -25
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -106
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/request/multipart_spec.rb +0 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d04b0b5407943b47e2c55e93f2102cb1b0b268f2de5505eb5346410aec53ee8
|
4
|
+
data.tar.gz: 7793d3c324d35bb183192e8ed0f43d1036f8590907491b979a7e5da2d0a09266
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 308fd91172d26082a1de8401221507492a74a2e9a2ac8bad59e4e6f25f3d2b88f8c42258c12be65f0587ffb9d361e1c8f4c889d877dc3a401322283dc3106707
|
7
|
+
data.tar.gz: c5b84b518095290e8ddbbf660b37f798a54849e604cc6550bf6ba9e6b4792b6a1de3994c41d2df182ec0cdafcdab62c15bafc9dc147b658aabc9e5059f7a2a52
|
data/examples/client_spec.rb
CHANGED
@@ -12,8 +12,8 @@ class Client
|
|
12
12
|
@conn = conn
|
13
13
|
end
|
14
14
|
|
15
|
-
def sushi(jname)
|
16
|
-
res = @conn.get("/#{jname}")
|
15
|
+
def sushi(jname, params: {})
|
16
|
+
res = @conn.get("/#{jname}", params)
|
17
17
|
data = JSON.parse(res.body)
|
18
18
|
data['name']
|
19
19
|
end
|
@@ -62,4 +62,36 @@ RSpec.describe Client do
|
|
62
62
|
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
|
63
63
|
stubs.verify_stubbed_calls
|
64
64
|
end
|
65
|
+
|
66
|
+
context 'When the test stub is run in strict_mode' do
|
67
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
|
68
|
+
|
69
|
+
it 'verifies the all parameter values are identical' do
|
70
|
+
stubs.get('/ebi?abc=123') do
|
71
|
+
[
|
72
|
+
200,
|
73
|
+
{ 'Content-Type': 'application/javascript' },
|
74
|
+
'{"name": "shrimp"}'
|
75
|
+
]
|
76
|
+
end
|
77
|
+
|
78
|
+
# uncomment to raise Stubs::NotFound
|
79
|
+
# expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
|
80
|
+
expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
|
81
|
+
stubs.verify_stubbed_calls
|
82
|
+
end
|
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
|
65
97
|
end
|
data/examples/client_test.rb
CHANGED
@@ -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
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -25,6 +25,9 @@ module Faraday
|
|
25
25
|
# "showing item: #{meta[:match_data][1]}"
|
26
26
|
# ]
|
27
27
|
# end
|
28
|
+
#
|
29
|
+
# # You can set strict_mode to exactly match the stubbed requests.
|
30
|
+
# stub.strict_mode = true
|
28
31
|
# end
|
29
32
|
# end
|
30
33
|
#
|
@@ -47,10 +50,11 @@ module Faraday
|
|
47
50
|
class NotFound < StandardError
|
48
51
|
end
|
49
52
|
|
50
|
-
def initialize
|
53
|
+
def initialize(strict_mode: false)
|
51
54
|
# { get: [Stub, Stub] }
|
52
55
|
@stack = {}
|
53
56
|
@consumed = {}
|
57
|
+
@strict_mode = strict_mode
|
54
58
|
yield(self) if block_given?
|
55
59
|
end
|
56
60
|
|
@@ -58,18 +62,20 @@ module Faraday
|
|
58
62
|
@stack.empty?
|
59
63
|
end
|
60
64
|
|
61
|
-
|
65
|
+
# @param env [Faraday::Env]
|
66
|
+
def match(env)
|
67
|
+
request_method = env[:method]
|
62
68
|
return false unless @stack.key?(request_method)
|
63
69
|
|
64
70
|
stack = @stack[request_method]
|
65
71
|
consumed = (@consumed[request_method] ||= [])
|
66
72
|
|
67
|
-
stub, meta = matches?(stack,
|
73
|
+
stub, meta = matches?(stack, env)
|
68
74
|
if stub
|
69
75
|
consumed << stack.delete(stub)
|
70
76
|
return stub, meta
|
71
77
|
end
|
72
|
-
matches?(consumed,
|
78
|
+
matches?(consumed, env)
|
73
79
|
end
|
74
80
|
|
75
81
|
def get(path, headers = {}, &block)
|
@@ -115,6 +121,17 @@ module Faraday
|
|
115
121
|
raise failed_stubs.join(' ') unless failed_stubs.empty?
|
116
122
|
end
|
117
123
|
|
124
|
+
# Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
|
125
|
+
# which means that all of a path, parameters, and headers must be the same as an actual request.
|
126
|
+
def strict_mode=(value)
|
127
|
+
@strict_mode = value
|
128
|
+
@stack.each do |_method, stubs|
|
129
|
+
stubs.each do |stub|
|
130
|
+
stub.strict_mode = value
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
118
135
|
protected
|
119
136
|
|
120
137
|
def new_stub(request_method, path, headers = {}, body = nil, &block)
|
@@ -127,14 +144,18 @@ module Faraday
|
|
127
144
|
Faraday::Utils.URI(path).host
|
128
145
|
]
|
129
146
|
end
|
147
|
+
path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
|
148
|
+
headers = Utils::Headers.new(headers)
|
130
149
|
|
131
|
-
stub = Stub.new(host,
|
150
|
+
stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
|
132
151
|
(@stack[request_method] ||= []) << stub
|
133
152
|
end
|
134
153
|
|
135
|
-
|
154
|
+
# @param stack [Hash]
|
155
|
+
# @param env [Faraday::Env]
|
156
|
+
def matches?(stack, env)
|
136
157
|
stack.each do |stub|
|
137
|
-
match_result, meta = stub.matches?(
|
158
|
+
match_result, meta = stub.matches?(env)
|
138
159
|
return stub, meta if match_result
|
139
160
|
end
|
140
161
|
nil
|
@@ -142,35 +163,20 @@ module Faraday
|
|
142
163
|
end
|
143
164
|
|
144
165
|
# Stub request
|
145
|
-
# rubocop:disable Style/StructInheritance
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
Faraday::Utils.parse_nested_query(query)
|
153
|
-
else
|
154
|
-
{}
|
155
|
-
end
|
156
|
-
|
157
|
-
super(host, path, params, headers, body, block)
|
158
|
-
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]
|
159
173
|
|
160
|
-
def matches?(request_host, request_uri, request_headers, request_body)
|
161
|
-
request_path, request_query = request_uri.split('?')
|
162
|
-
request_params =
|
163
|
-
if request_query
|
164
|
-
Faraday::Utils.parse_nested_query(request_query)
|
165
|
-
else
|
166
|
-
{}
|
167
|
-
end
|
168
174
|
# meta is a hash used as carrier
|
169
175
|
# that will be yielded to consumer block
|
170
176
|
meta = {}
|
171
177
|
[(host.nil? || host == request_host) &&
|
172
178
|
path_match?(request_path, meta) &&
|
173
|
-
params_match?(
|
179
|
+
params_match?(env) &&
|
174
180
|
(body.to_s.size.zero? || request_body == body) &&
|
175
181
|
headers_match?(request_headers), meta]
|
176
182
|
end
|
@@ -183,13 +189,30 @@ module Faraday
|
|
183
189
|
end
|
184
190
|
end
|
185
191
|
|
186
|
-
|
192
|
+
# @param env [Faraday::Env]
|
193
|
+
def params_match?(env)
|
194
|
+
request_params = env[:params]
|
195
|
+
params = env.params_encoder.decode(query) || {}
|
196
|
+
|
197
|
+
if strict_mode
|
198
|
+
return Set.new(params) == Set.new(request_params)
|
199
|
+
end
|
200
|
+
|
187
201
|
params.keys.all? do |key|
|
188
202
|
request_params[key] == params[key]
|
189
203
|
end
|
190
204
|
end
|
191
205
|
|
192
206
|
def headers_match?(request_headers)
|
207
|
+
if strict_mode
|
208
|
+
headers_with_user_agent = headers.dup.tap do |hs|
|
209
|
+
# NOTE: Set User-Agent in case it's not set when creating Stubs.
|
210
|
+
# Users would not want to set Faraday's User-Agent explicitly.
|
211
|
+
hs[:user_agent] ||= Connection::USER_AGENT
|
212
|
+
end
|
213
|
+
return Set.new(headers_with_user_agent) == Set.new(request_headers)
|
214
|
+
end
|
215
|
+
|
193
216
|
headers.keys.all? do |key|
|
194
217
|
request_headers[key] == headers[key]
|
195
218
|
end
|
@@ -210,26 +233,19 @@ module Faraday
|
|
210
233
|
yield(stubs)
|
211
234
|
end
|
212
235
|
|
236
|
+
# @param env [Faraday::Env]
|
213
237
|
def call(env)
|
214
238
|
super
|
215
|
-
host = env[:url].host
|
216
|
-
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
217
|
-
params_encoder = env.request.params_encoder ||
|
218
|
-
Faraday::Utils.default_params_encoder
|
219
239
|
|
220
|
-
|
221
|
-
|
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)
|
222
243
|
|
223
244
|
unless stub
|
224
245
|
raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
|
225
|
-
"#{
|
246
|
+
"#{env[:url]} #{env[:body]}"
|
226
247
|
end
|
227
248
|
|
228
|
-
env[:params] = if (query = env[:url].query)
|
229
|
-
params_encoder.decode(query)
|
230
|
-
else
|
231
|
-
{}
|
232
|
-
end
|
233
249
|
block_arity = stub.block.arity
|
234
250
|
status, headers, body =
|
235
251
|
if block_arity >= 0
|
data/lib/faraday/adapter.rb
CHANGED
@@ -11,10 +11,7 @@ module Faraday
|
|
11
11
|
|
12
12
|
register_middleware File.expand_path('adapter', __dir__),
|
13
13
|
test: [:Test, 'test'],
|
14
|
-
typhoeus: [:Typhoeus, 'typhoeus']
|
15
|
-
patron: [:Patron, 'patron'],
|
16
|
-
rack: [:Rack, 'rack'],
|
17
|
-
httpclient: [:HTTPClient, 'httpclient']
|
14
|
+
typhoeus: [:Typhoeus, 'typhoeus']
|
18
15
|
|
19
16
|
# This module marks an Adapter as supporting parallel requests.
|
20
17
|
module Parallelism
|
data/lib/faraday/autoload.rb
CHANGED
@@ -59,10 +59,7 @@ module Faraday
|
|
59
59
|
extend AutoloadHelper
|
60
60
|
autoload_all 'faraday/adapter',
|
61
61
|
Typhoeus: 'typhoeus',
|
62
|
-
|
63
|
-
Test: 'test',
|
64
|
-
Rack: 'rack',
|
65
|
-
HTTPClient: 'httpclient'
|
62
|
+
Test: 'test'
|
66
63
|
end
|
67
64
|
|
68
65
|
# Request represents a single HTTP request for a Faraday adapter to make.
|
data/lib/faraday/connection.rb
CHANGED
@@ -15,6 +15,7 @@ module Faraday
|
|
15
15
|
class Connection
|
16
16
|
# A Set of allowed HTTP verbs.
|
17
17
|
METHODS = Set.new %i[get post put delete head patch options trace]
|
18
|
+
USER_AGENT = "Faraday v#{VERSION}"
|
18
19
|
|
19
20
|
# @return [Hash] URI query unencoded key/value pairs.
|
20
21
|
attr_reader :params
|
@@ -26,7 +27,7 @@ module Faraday
|
|
26
27
|
# Connection. This includes a default host name, scheme, port, and path.
|
27
28
|
attr_reader :url_prefix
|
28
29
|
|
29
|
-
# @return [Faraday::
|
30
|
+
# @return [Faraday::RackBuilder] Builder for this Connection.
|
30
31
|
attr_reader :builder
|
31
32
|
|
32
33
|
# @return [Hash] SSL options.
|
@@ -73,6 +74,7 @@ module Faraday
|
|
73
74
|
@options = options.request
|
74
75
|
@ssl = options.ssl
|
75
76
|
@default_parallel_manager = options.parallel_manager
|
77
|
+
@manual_proxy = nil
|
76
78
|
|
77
79
|
@builder = options.builder || begin
|
78
80
|
# pass an empty block to Builder so it doesn't assume default middleware
|
@@ -88,7 +90,7 @@ module Faraday
|
|
88
90
|
|
89
91
|
yield(self) if block_given?
|
90
92
|
|
91
|
-
@headers[:user_agent] ||=
|
93
|
+
@headers[:user_agent] ||= USER_AGENT
|
92
94
|
end
|
93
95
|
|
94
96
|
def initialize_proxy(url, options)
|
@@ -295,6 +297,11 @@ module Faraday
|
|
295
297
|
#
|
296
298
|
# @return [void]
|
297
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
|
298
305
|
set_authorization_header(:basic_auth, login, pass)
|
299
306
|
end
|
300
307
|
|
@@ -312,6 +319,11 @@ module Faraday
|
|
312
319
|
#
|
313
320
|
# @return [void]
|
314
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
|
315
327
|
set_authorization_header(:token_auth, token, options)
|
316
328
|
end
|
317
329
|
|
@@ -334,6 +346,11 @@ module Faraday
|
|
334
346
|
#
|
335
347
|
# @return [void]
|
336
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
|
337
354
|
set_authorization_header(:authorization, type, token)
|
338
355
|
end
|
339
356
|
|
@@ -416,13 +433,18 @@ module Faraday
|
|
416
433
|
uri.query = nil
|
417
434
|
|
418
435
|
with_uri_credentials(uri) do |user, password|
|
419
|
-
|
436
|
+
set_basic_auth(user, password)
|
420
437
|
uri.user = uri.password = nil
|
421
438
|
end
|
422
439
|
|
423
440
|
@proxy = proxy_from_env(url) unless @manual_proxy
|
424
441
|
end
|
425
442
|
|
443
|
+
def set_basic_auth(user, password)
|
444
|
+
header = Faraday::Request::BasicAuthentication.header(user, password)
|
445
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
446
|
+
end
|
447
|
+
|
426
448
|
# Sets the path prefix and ensures that it always has a leading
|
427
449
|
# slash.
|
428
450
|
#
|
@@ -519,9 +541,8 @@ module Faraday
|
|
519
541
|
# @return [URI]
|
520
542
|
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
|
521
543
|
url = nil if url.respond_to?(:empty?) && url.empty?
|
522
|
-
base = url_prefix
|
544
|
+
base = url_prefix.dup
|
523
545
|
if url && base.path && base.path !~ %r{/$}
|
524
|
-
base = base.dup
|
525
546
|
base.path = "#{base.path}/" # ensure trailing slash
|
526
547
|
end
|
527
548
|
url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
|
data/lib/faraday/error.rb
CHANGED
@@ -143,10 +143,4 @@ module Faraday
|
|
143
143
|
# Raised by FaradayMiddleware::ResponseMiddleware
|
144
144
|
class ParsingError < Error
|
145
145
|
end
|
146
|
-
|
147
|
-
# Exception used to control the Retry middleware.
|
148
|
-
#
|
149
|
-
# @see Faraday::Request::Retry
|
150
|
-
class RetriableResponse < Error
|
151
|
-
end
|
152
146
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
class Request
|
5
7
|
# Request middleware for the Authorization HTTP header
|
@@ -13,7 +15,8 @@ module Faraday
|
|
13
15
|
# @return [String] a header value
|
14
16
|
def self.header(type, token)
|
15
17
|
case token
|
16
|
-
when String, Symbol
|
18
|
+
when String, Symbol, Proc
|
19
|
+
token = token.call if token.is_a?(Proc)
|
17
20
|
"#{type} #{token}"
|
18
21
|
when Hash
|
19
22
|
build_hash(type.to_s, token)
|
@@ -32,6 +35,7 @@ module Faraday
|
|
32
35
|
comma = ', '
|
33
36
|
values = []
|
34
37
|
hash.each do |key, value|
|
38
|
+
value = value.call if value.is_a?(Proc)
|
35
39
|
values << "#{key}=#{value.to_s.inspect}"
|
36
40
|
end
|
37
41
|
"#{type} #{values * comma}"
|
@@ -39,16 +43,19 @@ module Faraday
|
|
39
43
|
|
40
44
|
# @param app [#call]
|
41
45
|
# @param type [String, Symbol] Type of Authorization
|
42
|
-
# @param
|
43
|
-
|
44
|
-
|
46
|
+
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
|
47
|
+
# This value can be a proc, in which case it will be invoked on each request.
|
48
|
+
def initialize(app, type, param)
|
49
|
+
@type = type
|
50
|
+
@param = param
|
45
51
|
super(app)
|
46
52
|
end
|
47
53
|
|
48
54
|
# @param env [Faraday::Env]
|
49
|
-
def
|
50
|
-
|
51
|
-
|
55
|
+
def on_request(env)
|
56
|
+
return if env.request_headers[KEY]
|
57
|
+
|
58
|
+
env.request_headers[KEY] = self.class.header(@type, @param)
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
data/lib/faraday/version.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -24,14 +24,23 @@ require 'faraday/adapter'
|
|
24
24
|
require 'faraday/request'
|
25
25
|
require 'faraday/response'
|
26
26
|
require 'faraday/error'
|
27
|
-
require 'faraday/
|
28
|
-
require 'faraday/param_part'
|
27
|
+
require 'faraday/request/url_encoded' # needed by multipart
|
29
28
|
|
30
|
-
|
31
|
-
require 'faraday/
|
29
|
+
# External Middleware gems
|
30
|
+
require 'faraday/multipart'
|
31
|
+
require 'faraday/retry'
|
32
|
+
|
33
|
+
# External Adapters gems
|
34
|
+
unless defined?(JRUBY_VERSION)
|
35
|
+
require 'faraday/em_http'
|
36
|
+
require 'faraday/em_synchrony'
|
37
|
+
end
|
32
38
|
require 'faraday/excon'
|
39
|
+
require 'faraday/httpclient'
|
33
40
|
require 'faraday/net_http'
|
34
41
|
require 'faraday/net_http_persistent'
|
42
|
+
require 'faraday/patron'
|
43
|
+
require 'faraday/rack'
|
35
44
|
|
36
45
|
# This is the main namespace for Faraday.
|
37
46
|
#
|
@@ -1,47 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMHttp do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
6
7
|
|
7
|
-
|
8
|
+
it_behaves_like 'an adapter'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
13
14
|
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'Options' do
|
18
|
-
let(:request) { Faraday::RequestOptions.new }
|
19
|
-
let(:env) { { request: request } }
|
20
|
-
let(:options) { {} }
|
21
|
-
let(:adapter) { Faraday::Adapter::EMHttp.new }
|
22
|
-
|
23
|
-
it 'configures timeout' do
|
24
|
-
request.timeout = 5
|
25
|
-
adapter.configure_timeout(options, env)
|
26
|
-
expect(options[:inactivity_timeout]).to eq(5)
|
27
|
-
expect(options[:connect_timeout]).to eq(5)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'configures timeout and open_timeout' do
|
31
|
-
request.timeout = 5
|
32
|
-
request.open_timeout = 1
|
33
|
-
adapter.configure_timeout(options, env)
|
34
|
-
expect(options[:inactivity_timeout]).to eq(5)
|
35
|
-
expect(options[:connect_timeout]).to eq(1)
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
36
16
|
end
|
37
17
|
|
38
|
-
|
39
|
-
request.
|
40
|
-
request
|
41
|
-
|
42
|
-
adapter.
|
43
|
-
|
44
|
-
|
18
|
+
context 'Options' do
|
19
|
+
let(:request) { Faraday::RequestOptions.new }
|
20
|
+
let(:env) { { request: request } }
|
21
|
+
let(:options) { {} }
|
22
|
+
let(:adapter) { Faraday::Adapter::EMHttp.new }
|
23
|
+
|
24
|
+
it 'configures timeout' do
|
25
|
+
request.timeout = 5
|
26
|
+
adapter.configure_timeout(options, env)
|
27
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
28
|
+
expect(options[:connect_timeout]).to eq(5)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'configures timeout and open_timeout' do
|
32
|
+
request.timeout = 5
|
33
|
+
request.open_timeout = 1
|
34
|
+
adapter.configure_timeout(options, env)
|
35
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
36
|
+
expect(options[:connect_timeout]).to eq(1)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'configures all timeout settings' do
|
40
|
+
request.timeout = 5
|
41
|
+
request.read_timeout = 3
|
42
|
+
request.open_timeout = 1
|
43
|
+
adapter.configure_timeout(options, env)
|
44
|
+
expect(options[:inactivity_timeout]).to eq(3)
|
45
|
+
expect(options[:connect_timeout]).to eq(1)
|
46
|
+
end
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMSynchrony do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
6
7
|
|
7
|
-
|
8
|
+
it_behaves_like 'an adapter'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
13
14
|
|
14
|
-
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|