faraday 1.0.1 → 1.6.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/CHANGELOG.md +104 -0
- data/LICENSE.md +1 -1
- data/README.md +3 -5
- data/examples/client_spec.rb +1 -1
- data/lib/faraday.rb +60 -41
- data/lib/faraday/adapter.rb +2 -12
- data/lib/faraday/autoload.rb +2 -10
- data/lib/faraday/connection.rb +14 -7
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
- data/lib/faraday/error.rb +20 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options.rb +4 -8
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/rack_builder.rb +13 -12
- data/lib/faraday/request.rb +20 -10
- data/lib/faraday/request/multipart.rb +9 -2
- data/lib/faraday/request/retry.rb +2 -2
- data/lib/faraday/response.rb +0 -6
- data/lib/faraday/response/raise_error.rb +12 -1
- data/lib/faraday/utils.rb +2 -2
- data/lib/faraday/utils/headers.rb +2 -2
- data/lib/faraday/version.rb +5 -0
- 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 +260 -0
- data/spec/faraday/connection_spec.rb +45 -0
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +149 -0
- data/spec/faraday/request/authorization_spec.rb +2 -2
- data/spec/faraday/request/multipart_spec.rb +41 -13
- data/spec/faraday/request/retry_spec.rb +1 -1
- data/spec/faraday/request_spec.rb +16 -5
- data/spec/faraday/response/raise_error_spec.rb +63 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +39 -11
- metadata +134 -16
- data/lib/faraday/adapter/em_http.rb +0 -286
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/net_http.rb +0 -219
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
@@ -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
|
@@ -0,0 +1,260 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Adapter::Test do
|
4
|
+
let(:stubs) do
|
5
|
+
described_class::Stubs.new do |stub|
|
6
|
+
stub.get('http://domain.test/hello') do
|
7
|
+
[200, { 'Content-Type' => 'text/html' }, 'domain: hello']
|
8
|
+
end
|
9
|
+
|
10
|
+
stub.get('http://wrong.test/hello') do
|
11
|
+
[200, { 'Content-Type' => 'text/html' }, 'wrong: hello']
|
12
|
+
end
|
13
|
+
|
14
|
+
stub.get('http://wrong.test/bait') do
|
15
|
+
[404, { 'Content-Type' => 'text/html' }]
|
16
|
+
end
|
17
|
+
|
18
|
+
stub.get('/hello') do
|
19
|
+
[200, { 'Content-Type' => 'text/html' }, 'hello']
|
20
|
+
end
|
21
|
+
|
22
|
+
stub.get('/method-echo') do |env|
|
23
|
+
[200, { 'Content-Type' => 'text/html' }, env[:method].to_s]
|
24
|
+
end
|
25
|
+
|
26
|
+
stub.get(%r{\A/resources/\d+(?:\?|\z)}) do
|
27
|
+
[200, { 'Content-Type' => 'text/html' }, 'show']
|
28
|
+
end
|
29
|
+
|
30
|
+
stub.get(%r{\A/resources/(specified)\z}) do |_env, meta|
|
31
|
+
[200, { 'Content-Type' => 'text/html' }, "show #{meta[:match_data][1]}"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:connection) do
|
37
|
+
Faraday.new do |builder|
|
38
|
+
builder.adapter :test, stubs
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:response) { connection.get('/hello') }
|
43
|
+
|
44
|
+
context 'with simple path sets status' do
|
45
|
+
subject { response.status }
|
46
|
+
|
47
|
+
it { is_expected.to eq 200 }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'with simple path sets headers' do
|
51
|
+
subject { response.headers['Content-Type'] }
|
52
|
+
|
53
|
+
it { is_expected.to eq 'text/html' }
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'with simple path sets body' do
|
57
|
+
subject { response.body }
|
58
|
+
|
59
|
+
it { is_expected.to eq 'hello' }
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with host points to the right stub' do
|
63
|
+
subject { connection.get('http://domain.test/hello').body }
|
64
|
+
|
65
|
+
it { is_expected.to eq 'domain: hello' }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'can be called several times' do
|
69
|
+
subject { connection.get('/hello').body }
|
70
|
+
|
71
|
+
it { is_expected.to eq 'hello' }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'can handle regular expression path' do
|
75
|
+
subject { connection.get('/resources/1').body }
|
76
|
+
|
77
|
+
it { is_expected.to eq 'show' }
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'can handle single parameter block' do
|
81
|
+
subject { connection.get('/method-echo').body }
|
82
|
+
|
83
|
+
it { is_expected.to eq 'get' }
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'can handle regular expression path with captured result' do
|
87
|
+
subject { connection.get('/resources/specified').body }
|
88
|
+
|
89
|
+
it { is_expected.to eq 'show specified' }
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'with get params' do
|
93
|
+
subject { connection.get('/param?a=1').body }
|
94
|
+
|
95
|
+
before do
|
96
|
+
stubs.get('/param?a=1') { [200, {}, 'a'] }
|
97
|
+
end
|
98
|
+
|
99
|
+
it { is_expected.to eq 'a' }
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'ignoring unspecified get params' do
|
103
|
+
before do
|
104
|
+
stubs.get('/optional?a=1') { [200, {}, 'a'] }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'with multiple params' do
|
108
|
+
subject { connection.get('/optional?a=1&b=1').body }
|
109
|
+
|
110
|
+
it { is_expected.to eq 'a' }
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'with single param' do
|
114
|
+
subject { connection.get('/optional?a=1').body }
|
115
|
+
|
116
|
+
it { is_expected.to eq 'a' }
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'without params' do
|
120
|
+
subject(:request) { connection.get('/optional') }
|
121
|
+
|
122
|
+
it do
|
123
|
+
expect { request }.to raise_error(
|
124
|
+
Faraday::Adapter::Test::Stubs::NotFound
|
125
|
+
)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'with http headers' do
|
131
|
+
before do
|
132
|
+
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
|
133
|
+
stubs.get('/yo') { [200, {}, 'b'] }
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'with header' do
|
137
|
+
subject do
|
138
|
+
connection.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
|
139
|
+
end
|
140
|
+
|
141
|
+
it { is_expected.to eq 'a' }
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'without header' do
|
145
|
+
subject do
|
146
|
+
connection.get('/yo').body
|
147
|
+
end
|
148
|
+
|
149
|
+
it { is_expected.to eq 'b' }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe 'different outcomes for the same request' do
|
154
|
+
def make_request
|
155
|
+
connection.get('/foo')
|
156
|
+
end
|
157
|
+
|
158
|
+
subject(:request) { make_request.body }
|
159
|
+
|
160
|
+
before do
|
161
|
+
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
|
162
|
+
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'world'] }
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'the first request' do
|
166
|
+
it { is_expected.to eq 'hello' }
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'the second request' do
|
170
|
+
before do
|
171
|
+
make_request
|
172
|
+
end
|
173
|
+
|
174
|
+
it { is_expected.to eq 'world' }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe 'yielding env to stubs' do
|
179
|
+
subject { connection.get('http://foo.com/foo?a=1').body }
|
180
|
+
|
181
|
+
before do
|
182
|
+
stubs.get '/foo' do |env|
|
183
|
+
expect(env[:url].path).to eq '/foo'
|
184
|
+
expect(env[:url].host).to eq 'foo.com'
|
185
|
+
expect(env[:params]['a']).to eq '1'
|
186
|
+
expect(env[:request_headers]['Accept']).to eq 'text/plain'
|
187
|
+
[200, {}, 'a']
|
188
|
+
end
|
189
|
+
|
190
|
+
connection.headers['Accept'] = 'text/plain'
|
191
|
+
end
|
192
|
+
|
193
|
+
it { is_expected.to eq 'a' }
|
194
|
+
end
|
195
|
+
|
196
|
+
describe 'params parsing' do
|
197
|
+
subject { connection.get('http://foo.com/foo?a[b]=1').body }
|
198
|
+
|
199
|
+
context 'with default encoder' do
|
200
|
+
before do
|
201
|
+
stubs.get '/foo' do |env|
|
202
|
+
expect(env[:params]['a']['b']).to eq '1'
|
203
|
+
[200, {}, 'a']
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
it { is_expected.to eq 'a' }
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'with nested encoder' do
|
211
|
+
before do
|
212
|
+
stubs.get '/foo' do |env|
|
213
|
+
expect(env[:params]['a']['b']).to eq '1'
|
214
|
+
[200, {}, 'a']
|
215
|
+
end
|
216
|
+
|
217
|
+
connection.options.params_encoder = Faraday::NestedParamsEncoder
|
218
|
+
end
|
219
|
+
|
220
|
+
it { is_expected.to eq 'a' }
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'with flat encoder' do
|
224
|
+
before do
|
225
|
+
stubs.get '/foo' do |env|
|
226
|
+
expect(env[:params]['a[b]']).to eq '1'
|
227
|
+
[200, {}, 'a']
|
228
|
+
end
|
229
|
+
|
230
|
+
connection.options.params_encoder = Faraday::FlatParamsEncoder
|
231
|
+
end
|
232
|
+
|
233
|
+
it { is_expected.to eq 'a' }
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe 'raising an error if no stub was found' do
|
238
|
+
describe 'for request' do
|
239
|
+
subject(:request) { connection.get('/invalid') { [200, {}, []] } }
|
240
|
+
|
241
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'for specified host' do
|
245
|
+
subject(:request) { connection.get('http://domain.test/bait') }
|
246
|
+
|
247
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
248
|
+
end
|
249
|
+
|
250
|
+
describe 'for request without specified header' do
|
251
|
+
subject(:request) { connection.get('/yo') }
|
252
|
+
|
253
|
+
before do
|
254
|
+
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
|
255
|
+
end
|
256
|
+
|
257
|
+
it { expect { request }.to raise_error described_class::Stubs::NotFound }
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
@@ -18,6 +18,13 @@ shared_examples 'initializer with url' do
|
|
18
18
|
it { expect(subject.path_prefix).to eq('/fish') }
|
19
19
|
it { expect(subject.params).to eq('a' => '1') }
|
20
20
|
end
|
21
|
+
|
22
|
+
context 'with IPv6 address' do
|
23
|
+
let(:address) { 'http://[::1]:85/' }
|
24
|
+
|
25
|
+
it { expect(subject.host).to eq('[::1]') }
|
26
|
+
it { expect(subject.port).to eq(85) }
|
27
|
+
end
|
21
28
|
end
|
22
29
|
|
23
30
|
shared_examples 'default connection options' do
|
@@ -246,6 +253,13 @@ RSpec.describe Faraday::Connection do
|
|
246
253
|
expect(uri.path).to eq('/sake.html')
|
247
254
|
end
|
248
255
|
|
256
|
+
it 'always returns new URI instance' do
|
257
|
+
conn.url_prefix = 'http://sushi.com'
|
258
|
+
uri1 = conn.build_exclusive_url(nil)
|
259
|
+
uri2 = conn.build_exclusive_url(nil)
|
260
|
+
expect(uri1).not_to equal(uri2)
|
261
|
+
end
|
262
|
+
|
249
263
|
context 'with url_prefixed connection' do
|
250
264
|
let(:url) { 'http://sushi.com/sushi/' }
|
251
265
|
|
@@ -270,6 +284,29 @@ RSpec.describe Faraday::Connection do
|
|
270
284
|
expect(uri.to_s).to eq('http://sushi.com/sake/')
|
271
285
|
end
|
272
286
|
end
|
287
|
+
|
288
|
+
context 'with colon in path' do
|
289
|
+
let(:url) { 'http://service.com' }
|
290
|
+
|
291
|
+
it 'joins url to base when used absolute path' do
|
292
|
+
conn = Faraday.new(url: url)
|
293
|
+
uri = conn.build_exclusive_url('/service:search?limit=400')
|
294
|
+
expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'joins url to base when used relative path' do
|
298
|
+
conn = Faraday.new(url: url)
|
299
|
+
uri = conn.build_exclusive_url('service:search?limit=400')
|
300
|
+
expect(uri.to_s).to eq('http://service.com/service%3Asearch?limit=400')
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'joins url to base when used with path prefix' do
|
304
|
+
conn = Faraday.new(url: url)
|
305
|
+
conn.path_prefix = '/api'
|
306
|
+
uri = conn.build_exclusive_url('service:search?limit=400')
|
307
|
+
expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400')
|
308
|
+
end
|
309
|
+
end
|
273
310
|
end
|
274
311
|
|
275
312
|
describe '#build_url' do
|
@@ -412,6 +449,14 @@ RSpec.describe Faraday::Connection do
|
|
412
449
|
end
|
413
450
|
end
|
414
451
|
|
452
|
+
it 'allows when url in no proxy list with url_prefix' do
|
453
|
+
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
454
|
+
conn = Faraday::Connection.new
|
455
|
+
conn.url_prefix = 'http://example.com'
|
456
|
+
expect(conn.proxy).to be_nil
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
415
460
|
it 'allows when prefixed url is not in no proxy list' do
|
416
461
|
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
417
462
|
conn = Faraday::Connection.new('http://prefixedexample.com')
|
data/spec/faraday/error_spec.rb
CHANGED
@@ -13,6 +13,7 @@ RSpec.describe Faraday::ClientError do
|
|
13
13
|
it { expect(subject.message).to eq(exception.message) }
|
14
14
|
it { expect(subject.backtrace).to eq(exception.backtrace) }
|
15
15
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
|
16
|
+
it { expect(subject.response_status).to be_nil }
|
16
17
|
end
|
17
18
|
|
18
19
|
context 'with response hash' do
|
@@ -22,6 +23,7 @@ RSpec.describe Faraday::ClientError do
|
|
22
23
|
it { expect(subject.response).to eq(exception) }
|
23
24
|
it { expect(subject.message).to eq('the server responded with status 400') }
|
24
25
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
|
26
|
+
it { expect(subject.response_status).to eq(400) }
|
25
27
|
end
|
26
28
|
|
27
29
|
context 'with string' do
|
@@ -31,6 +33,7 @@ RSpec.describe Faraday::ClientError do
|
|
31
33
|
it { expect(subject.response).to be_nil }
|
32
34
|
it { expect(subject.message).to eq('custom message') }
|
33
35
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
|
36
|
+
it { expect(subject.response_status).to be_nil }
|
34
37
|
end
|
35
38
|
|
36
39
|
context 'with anything else #to_s' do
|
@@ -40,6 +43,18 @@ RSpec.describe Faraday::ClientError do
|
|
40
43
|
it { expect(subject.response).to be_nil }
|
41
44
|
it { expect(subject.message).to eq('["error1", "error2"]') }
|
42
45
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
|
46
|
+
it { expect(subject.response_status).to be_nil }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with exception string and response hash' do
|
50
|
+
let(:exception) { 'custom message' }
|
51
|
+
let(:response) { { status: 400 } }
|
52
|
+
|
53
|
+
it { expect(subject.wrapped_exception).to be_nil }
|
54
|
+
it { expect(subject.response).to eq(response) }
|
55
|
+
it { expect(subject.message).to eq('custom message') }
|
56
|
+
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
|
57
|
+
it { expect(subject.response_status).to eq(400) }
|
43
58
|
end
|
44
59
|
end
|
45
60
|
end
|
@@ -2,23 +2,49 @@
|
|
2
2
|
|
3
3
|
RSpec.describe Faraday::Middleware do
|
4
4
|
subject { described_class.new(app) }
|
5
|
+
let(:app) { double }
|
6
|
+
|
7
|
+
describe 'options' do
|
8
|
+
context 'when options are passed to the middleware' do
|
9
|
+
subject { described_class.new(app, options) }
|
10
|
+
let(:options) { { field: 'value' } }
|
11
|
+
|
12
|
+
it 'accepts options when initialized' do
|
13
|
+
expect(subject.options[:field]).to eq('value')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#on_request' do
|
19
|
+
subject do
|
20
|
+
Class.new(described_class) do
|
21
|
+
def on_request(env)
|
22
|
+
# do nothing
|
23
|
+
end
|
24
|
+
end.new(app)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'is called by #call' do
|
28
|
+
expect(app).to receive(:call).and_return(app)
|
29
|
+
expect(app).to receive(:on_complete)
|
30
|
+
is_expected.to receive(:call).and_call_original
|
31
|
+
is_expected.to receive(:on_request)
|
32
|
+
subject.call(double)
|
33
|
+
end
|
34
|
+
end
|
5
35
|
|
6
36
|
describe '#close' do
|
7
37
|
context "with app that doesn't support \#close" do
|
8
|
-
let(:app) { double }
|
9
|
-
|
10
38
|
it 'should issue warning' do
|
11
|
-
|
39
|
+
is_expected.to receive(:warn)
|
12
40
|
subject.close
|
13
41
|
end
|
14
42
|
end
|
15
43
|
|
16
44
|
context "with app that supports \#close" do
|
17
|
-
let(:app) { double }
|
18
|
-
|
19
45
|
it 'should issue warning' do
|
20
46
|
expect(app).to receive(:close)
|
21
|
-
|
47
|
+
is_expected.to_not receive(:warn)
|
22
48
|
subject.close
|
23
49
|
end
|
24
50
|
end
|