faraday 2.15.0 → 2.15.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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +11 -5
  3. data/lib/faraday/connection.rb +13 -6
  4. data/lib/faraday/encoders/nested_params_encoder.rb +1 -1
  5. data/lib/faraday/error.rb +29 -2
  6. data/lib/faraday/logging/formatter.rb +1 -1
  7. data/lib/faraday/options.rb +2 -1
  8. data/lib/faraday/rack_builder.rb +3 -0
  9. data/lib/faraday/response/raise_error.rb +7 -1
  10. data/lib/faraday/response.rb +5 -2
  11. data/lib/faraday/utils/headers.rb +41 -7
  12. data/lib/faraday/utils.rb +15 -3
  13. data/lib/faraday/version.rb +1 -1
  14. data/spec/external_adapters/faraday_specs_setup.rb +2 -0
  15. data/spec/support/integration/test_pki.rb +77 -0
  16. data/spec/support/integration/tls_server.rb +92 -0
  17. data/spec/support/integration_helper.rb +71 -0
  18. metadata +5 -29
  19. data/spec/faraday/adapter/test_spec.rb +0 -460
  20. data/spec/faraday/adapter_registry_spec.rb +0 -28
  21. data/spec/faraday/adapter_spec.rb +0 -55
  22. data/spec/faraday/connection_spec.rb +0 -897
  23. data/spec/faraday/error_spec.rb +0 -175
  24. data/spec/faraday/middleware_registry_spec.rb +0 -31
  25. data/spec/faraday/middleware_spec.rb +0 -213
  26. data/spec/faraday/options/env_spec.rb +0 -76
  27. data/spec/faraday/options/options_spec.rb +0 -331
  28. data/spec/faraday/options/proxy_options_spec.rb +0 -85
  29. data/spec/faraday/options/request_options_spec.rb +0 -19
  30. data/spec/faraday/params_encoders/flat_spec.rb +0 -42
  31. data/spec/faraday/params_encoders/nested_spec.rb +0 -179
  32. data/spec/faraday/rack_builder_spec.rb +0 -323
  33. data/spec/faraday/request/authorization_spec.rb +0 -118
  34. data/spec/faraday/request/instrumentation_spec.rb +0 -74
  35. data/spec/faraday/request/json_spec.rb +0 -199
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -93
  37. data/spec/faraday/request_spec.rb +0 -119
  38. data/spec/faraday/response/json_spec.rb +0 -206
  39. data/spec/faraday/response/logger_spec.rb +0 -299
  40. data/spec/faraday/response/raise_error_spec.rb +0 -286
  41. data/spec/faraday/response_spec.rb +0 -84
  42. data/spec/faraday/utils/headers_spec.rb +0 -109
  43. data/spec/faraday/utils_spec.rb +0 -120
  44. data/spec/faraday_spec.rb +0 -69
  45. data/spec/spec_helper.rb +0 -142
@@ -1,897 +0,0 @@
1
- # frozen_string_literal: true
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(',').to_h { |pair| pair.split('-') }
10
- end
11
- end
12
-
13
- shared_examples 'initializer with url' do
14
- context 'with simple url' do
15
- let(:address) { 'http://httpbingo.org' }
16
-
17
- it { expect(subject.host).to eq('httpbingo.org') }
18
- it { expect(subject.port).to eq(80) }
19
- it { expect(subject.scheme).to eq('http') }
20
- it { expect(subject.path_prefix).to eq('/') }
21
- it { expect(subject.params).to eq({}) }
22
- end
23
-
24
- context 'with complex url' do
25
- let(:address) { 'http://httpbingo.org:815/fish?a=1' }
26
-
27
- it { expect(subject.port).to eq(815) }
28
- it { expect(subject.path_prefix).to eq('/fish') }
29
- it { expect(subject.params).to eq('a' => '1') }
30
- end
31
-
32
- context 'with IPv6 address' do
33
- let(:address) { 'http://[::1]:85/' }
34
-
35
- it { expect(subject.host).to eq('[::1]') }
36
- it { expect(subject.port).to eq(85) }
37
- end
38
- end
39
-
40
- shared_examples 'default connection options' do
41
- after { Faraday.default_connection_options = nil }
42
-
43
- it 'works with implicit url' do
44
- conn = Faraday.new 'http://httpbingo.org/foo'
45
- expect(conn.options.timeout).to eq(10)
46
- end
47
-
48
- it 'works with option url' do
49
- conn = Faraday.new url: 'http://httpbingo.org/foo'
50
- expect(conn.options.timeout).to eq(10)
51
- end
52
-
53
- it 'works with instance connection options' do
54
- conn = Faraday.new 'http://httpbingo.org/foo', request: { open_timeout: 1 }
55
- expect(conn.options.timeout).to eq(10)
56
- expect(conn.options.open_timeout).to eq(1)
57
- end
58
-
59
- it 'default connection options persist with an instance overriding' do
60
- conn = Faraday.new 'http://nigiri.com/bar'
61
- conn.options.timeout = 1
62
- expect(Faraday.default_connection_options.request.timeout).to eq(10)
63
-
64
- other = Faraday.new url: 'https://httpbingo.org/foo'
65
- other.options.timeout = 1
66
-
67
- expect(Faraday.default_connection_options.request.timeout).to eq(10)
68
- end
69
-
70
- it 'default connection uses default connection options' do
71
- expect(Faraday.default_connection.options.timeout).to eq(10)
72
- end
73
- end
74
-
75
- RSpec.describe Faraday::Connection do
76
- let(:conn) { Faraday::Connection.new(url, options) }
77
- let(:url) { nil }
78
- let(:options) { nil }
79
-
80
- describe '.new' do
81
- subject { conn }
82
-
83
- context 'with implicit url param' do
84
- # Faraday::Connection.new('http://httpbingo.org')
85
- let(:url) { address }
86
-
87
- it_behaves_like 'initializer with url'
88
- end
89
-
90
- context 'with explicit url param' do
91
- # Faraday::Connection.new(url: 'http://httpbingo.org')
92
- let(:url) { { url: address } }
93
-
94
- it_behaves_like 'initializer with url'
95
- end
96
-
97
- context 'with custom builder' do
98
- let(:custom_builder) { Faraday::RackBuilder.new }
99
- let(:options) { { builder: custom_builder } }
100
-
101
- it { expect(subject.builder).to eq(custom_builder) }
102
- end
103
-
104
- context 'with custom params' do
105
- let(:options) { { params: { a: 1 } } }
106
-
107
- it { expect(subject.params).to eq('a' => 1) }
108
- end
109
-
110
- context 'with custom params and params in url' do
111
- let(:url) { 'http://httpbingo.org/fish?a=1&b=2' }
112
- let(:options) { { params: { a: 3 } } }
113
- it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
114
- end
115
-
116
- context 'with basic_auth in url' do
117
- let(:url) { 'http://Aladdin:open%20sesame@httpbingo.org/fish' }
118
-
119
- it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
120
- end
121
-
122
- context 'with basic_auth in url containing literal plus signs' do
123
- let(:url) { 'http://Ala+ddin:open+sesame%2B@httpbingo.org/fish' }
124
-
125
- it 'does not decode plus signs as spaces' do
126
- expected = Faraday::Utils.basic_header_from('Ala+ddin', 'open+sesame+')
127
- expect(subject.headers['Authorization']).to eq(expected)
128
- end
129
- end
130
-
131
- context 'with custom headers' do
132
- let(:options) { { headers: { user_agent: 'Faraday' } } }
133
-
134
- it { expect(subject.headers['User-agent']).to eq('Faraday') }
135
- end
136
-
137
- context 'with ssl false' do
138
- let(:options) { { ssl: { verify: false } } }
139
-
140
- it { expect(subject.ssl.verify?).to be_falsey }
141
- end
142
-
143
- context 'with verify_hostname false' do
144
- let(:options) { { ssl: { verify_hostname: false } } }
145
-
146
- it { expect(subject.ssl.verify_hostname?).to be_falsey }
147
- end
148
-
149
- context 'with empty block' do
150
- let(:conn) { Faraday::Connection.new {} }
151
-
152
- it { expect(conn.builder.handlers.size).to eq(0) }
153
- end
154
-
155
- context 'with block' do
156
- let(:conn) do
157
- Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
158
- faraday.adapter :test
159
- faraday.url_prefix = 'http://httpbingo.org/omnom'
160
- end
161
- end
162
-
163
- it { expect(conn.builder.handlers.size).to eq(0) }
164
- it { expect(conn.path_prefix).to eq('/omnom') }
165
- end
166
- end
167
-
168
- describe '#close' do
169
- before { Faraday.default_adapter = :test }
170
- after { Faraday.default_adapter = nil }
171
-
172
- it 'can close underlying app' do
173
- expect(conn.app).to receive(:close)
174
- conn.close
175
- end
176
- end
177
-
178
- describe '#build_exclusive_url' do
179
- context 'with relative path' do
180
- subject { conn.build_exclusive_url('sake.html') }
181
-
182
- it 'uses connection host as default host' do
183
- conn.host = 'httpbingo.org'
184
- expect(subject.host).to eq('httpbingo.org')
185
- expect(subject.scheme).to eq('http')
186
- end
187
-
188
- it do
189
- conn.path_prefix = '/fish'
190
- expect(subject.path).to eq('/fish/sake.html')
191
- end
192
-
193
- it do
194
- conn.path_prefix = '/'
195
- expect(subject.path).to eq('/sake.html')
196
- end
197
-
198
- it do
199
- conn.path_prefix = 'fish'
200
- expect(subject.path).to eq('/fish/sake.html')
201
- end
202
-
203
- it do
204
- conn.path_prefix = '/fish/'
205
- expect(subject.path).to eq('/fish/sake.html')
206
- end
207
- end
208
-
209
- context 'with absolute path' do
210
- subject { conn.build_exclusive_url('/sake.html') }
211
-
212
- after { expect(subject.path).to eq('/sake.html') }
213
-
214
- it { conn.path_prefix = '/fish' }
215
- it { conn.path_prefix = '/' }
216
- it { conn.path_prefix = 'fish' }
217
- it { conn.path_prefix = '/fish/' }
218
- end
219
-
220
- context 'with complete url' do
221
- subject { conn.build_exclusive_url('http://httpbingo.org/sake.html?a=1') }
222
-
223
- it { expect(subject.scheme).to eq('http') }
224
- it { expect(subject.host).to eq('httpbingo.org') }
225
- it { expect(subject.port).to eq(80) }
226
- it { expect(subject.path).to eq('/sake.html') }
227
- it { expect(subject.query).to eq('a=1') }
228
- end
229
-
230
- it 'overrides connection port for absolute url' do
231
- conn.port = 23
232
- uri = conn.build_exclusive_url('http://httpbingo.org')
233
- expect(uri.port).to eq(80)
234
- end
235
-
236
- it 'does not add ending slash given nil url' do
237
- conn.url_prefix = 'http://httpbingo.org/nigiri'
238
- uri = conn.build_exclusive_url
239
- expect(uri.path).to eq('/nigiri')
240
- end
241
-
242
- it 'does not add ending slash given empty url' do
243
- conn.url_prefix = 'http://httpbingo.org/nigiri'
244
- uri = conn.build_exclusive_url('')
245
- expect(uri.path).to eq('/nigiri')
246
- end
247
-
248
- it 'does not use connection params' do
249
- conn.url_prefix = 'http://httpbingo.org/nigiri'
250
- conn.params = { a: 1 }
251
- expect(conn.build_exclusive_url.to_s).to eq('http://httpbingo.org/nigiri')
252
- end
253
-
254
- it 'allows to provide params argument' do
255
- conn.url_prefix = 'http://httpbingo.org/nigiri'
256
- conn.params = { a: 1 }
257
- params = Faraday::Utils::ParamsHash.new
258
- params[:a] = 2
259
- uri = conn.build_exclusive_url(nil, params)
260
- expect(uri.to_s).to eq('http://httpbingo.org/nigiri?a=2')
261
- end
262
-
263
- it 'handles uri instances' do
264
- uri = conn.build_exclusive_url(URI('/sake.html'))
265
- expect(uri.path).to eq('/sake.html')
266
- end
267
-
268
- it 'always returns new URI instance' do
269
- conn.url_prefix = 'http://httpbingo.org'
270
- uri1 = conn.build_exclusive_url(nil)
271
- uri2 = conn.build_exclusive_url(nil)
272
- expect(uri1).not_to equal(uri2)
273
- end
274
-
275
- context 'with url_prefixed connection' do
276
- let(:url) { 'http://httpbingo.org/get/' }
277
-
278
- it 'parses url and changes scheme' do
279
- conn.scheme = 'https'
280
- uri = conn.build_exclusive_url('sake.html')
281
- expect(uri.to_s).to eq('https://httpbingo.org/get/sake.html')
282
- end
283
-
284
- it 'joins url to base with ending slash' do
285
- uri = conn.build_exclusive_url('sake.html')
286
- expect(uri.to_s).to eq('http://httpbingo.org/get/sake.html')
287
- end
288
-
289
- it 'used default base with ending slash' do
290
- uri = conn.build_exclusive_url
291
- expect(uri.to_s).to eq('http://httpbingo.org/get/')
292
- end
293
-
294
- it 'overrides base' do
295
- uri = conn.build_exclusive_url('/sake/')
296
- expect(uri.to_s).to eq('http://httpbingo.org/sake/')
297
- end
298
- end
299
-
300
- context 'with colon in path' do
301
- let(:url) { 'http://service.com' }
302
-
303
- it 'joins url to base when used absolute path' do
304
- conn = Faraday.new(url: url)
305
- uri = conn.build_exclusive_url('/service:search?limit=400')
306
- expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
307
- end
308
-
309
- it 'joins url to base when used relative path' do
310
- conn = Faraday.new(url: url)
311
- uri = conn.build_exclusive_url('service:search?limit=400')
312
- expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
313
- end
314
-
315
- it 'joins url to base when used with path prefix' do
316
- conn = Faraday.new(url: url)
317
- conn.path_prefix = '/api'
318
- uri = conn.build_exclusive_url('service:search?limit=400')
319
- expect(uri.to_s).to eq('http://service.com/api/service:search?limit=400')
320
- end
321
- end
322
-
323
- context 'with protocol-relative URL (GHSA-33mh-2634-fwr2)' do
324
- it 'does not allow host override with //evil.com/path' do
325
- conn.url_prefix = 'http://httpbingo.org/api'
326
- uri = conn.build_exclusive_url('//evil.com/path')
327
- expect(uri.host).to eq('httpbingo.org')
328
- end
329
-
330
- it 'does not allow host override with URI("//evil.com/path")' do
331
- conn.url_prefix = 'http://httpbingo.org/api'
332
- uri = conn.build_exclusive_url(URI('//evil.com/path?token=1'))
333
- expect(uri.host).to eq('httpbingo.org')
334
- expect(uri.query).to eq('token=1')
335
- end
336
-
337
- it 'does not allow host override with //evil.com:8080/path' do
338
- conn.url_prefix = 'http://httpbingo.org/api'
339
- uri = conn.build_exclusive_url('//evil.com:8080/path')
340
- expect(uri.host).to eq('httpbingo.org')
341
- end
342
-
343
- it 'does not allow host override with //user:pass@evil.com/path' do
344
- conn.url_prefix = 'http://httpbingo.org/api'
345
- uri = conn.build_exclusive_url('//user:pass@evil.com/path')
346
- expect(uri.host).to eq('httpbingo.org')
347
- end
348
-
349
- it 'does not allow host override with ///evil.com' do
350
- conn.url_prefix = 'http://httpbingo.org/api'
351
- uri = conn.build_exclusive_url('///evil.com')
352
- expect(uri.host).to eq('httpbingo.org')
353
- end
354
-
355
- it 'still allows single-slash absolute paths' do
356
- conn.url_prefix = 'http://httpbingo.org/api'
357
- uri = conn.build_exclusive_url('/safe/path')
358
- expect(uri.host).to eq('httpbingo.org')
359
- expect(uri.path).to eq('/safe/path')
360
- end
361
- end
362
-
363
- context 'with a custom `default_uri_parser`' do
364
- let(:url) { 'http://httpbingo.org' }
365
- let(:parser) { Addressable::URI }
366
-
367
- around do |example|
368
- with_default_uri_parser(parser) do
369
- example.run
370
- end
371
- end
372
-
373
- it 'does not raise error' do
374
- expect { conn.build_exclusive_url('/nigiri') }.not_to raise_error
375
- end
376
- end
377
- end
378
-
379
- describe '#build_url' do
380
- let(:url) { 'http://httpbingo.org/nigiri' }
381
-
382
- it 'uses params' do
383
- conn.params = { a: 1, b: 1 }
384
- expect(conn.build_url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=1')
385
- end
386
-
387
- it 'merges params' do
388
- conn.params = { a: 1, b: 1 }
389
- url = conn.build_url(nil, b: 2, c: 3)
390
- expect(url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=2&c=3')
391
- end
392
-
393
- it 'raises a controlled error when URL query params exceed the nested depth limit' do
394
- original_param_depth_limit = Faraday::NestedParamsEncoder.param_depth_limit
395
- Faraday::NestedParamsEncoder.param_depth_limit = 2
396
-
397
- expect { conn.build_url('/nigiri?a[b][c]=1') }.to raise_error(
398
- Faraday::Error,
399
- 'exceeded nested parameter depth limit of 2'
400
- )
401
- ensure
402
- Faraday::NestedParamsEncoder.param_depth_limit = original_param_depth_limit
403
- end
404
- end
405
-
406
- describe '#build_request' do
407
- let(:url) { 'https://ahttpbingo.org/sake.html' }
408
- let(:request) { conn.build_request(:get) }
409
-
410
- before do
411
- conn.headers = { 'Authorization' => 'token abc123' }
412
- request.headers.delete('Authorization')
413
- end
414
-
415
- it { expect(conn.headers.keys).to eq(['Authorization']) }
416
- it { expect(conn.headers.include?('Authorization')).to be_truthy }
417
- it { expect(request.headers.keys).to be_empty }
418
- it { expect(request.headers.include?('Authorization')).to be_falsey }
419
- end
420
-
421
- describe '#to_env' do
422
- subject { conn.build_request(:get).to_env(conn).url }
423
-
424
- let(:url) { 'http://httpbingo.org/sake.html' }
425
- let(:options) { { params: @params } }
426
-
427
- it 'parses url params into query' do
428
- @params = { 'a[b]' => '1 + 2' }
429
- expect(subject.query).to eq('a%5Bb%5D=1+%2B+2')
430
- end
431
-
432
- it 'escapes per spec' do
433
- @params = { 'a' => '1+2 foo~bar.-baz' }
434
- expect(subject.query).to eq('a=1%2B2+foo~bar.-baz')
435
- end
436
-
437
- it 'bracketizes nested params in query' do
438
- @params = { 'a' => { 'b' => 'c' } }
439
- expect(subject.query).to eq('a%5Bb%5D=c')
440
- end
441
-
442
- it 'bracketizes repeated params in query' do
443
- @params = { 'a' => [1, 2] }
444
- expect(subject.query).to eq('a%5B%5D=1&a%5B%5D=2')
445
- end
446
-
447
- it 'without braketizing repeated params in query' do
448
- @params = { 'a' => [1, 2] }
449
- conn.options.params_encoder = Faraday::FlatParamsEncoder
450
- expect(subject.query).to eq('a=1&a=2')
451
- end
452
- end
453
-
454
- describe 'proxy support' do
455
- it 'accepts string' do
456
- with_env 'http_proxy' => 'http://env-proxy.com:80' do
457
- conn.proxy = 'http://proxy.com'
458
- expect(conn.proxy.host).to eq('proxy.com')
459
- end
460
- end
461
-
462
- it 'accepts uri' do
463
- with_env 'http_proxy' => 'http://env-proxy.com:80' do
464
- conn.proxy = URI.parse('http://proxy.com')
465
- expect(conn.proxy.host).to eq('proxy.com')
466
- end
467
- end
468
-
469
- it 'accepts hash with string uri' do
470
- with_env 'http_proxy' => 'http://env-proxy.com:80' do
471
- conn.proxy = { uri: 'http://proxy.com', user: 'rick' }
472
- expect(conn.proxy.host).to eq('proxy.com')
473
- expect(conn.proxy.user).to eq('rick')
474
- end
475
- end
476
-
477
- it 'accepts hash' do
478
- with_env 'http_proxy' => 'http://env-proxy.com:80' do
479
- conn.proxy = { uri: URI.parse('http://proxy.com'), user: 'rick' }
480
- expect(conn.proxy.host).to eq('proxy.com')
481
- expect(conn.proxy.user).to eq('rick')
482
- end
483
- end
484
-
485
- it 'accepts http env' do
486
- with_env 'http_proxy' => 'http://env-proxy.com:80' do
487
- expect(conn.proxy.host).to eq('env-proxy.com')
488
- end
489
- end
490
-
491
- it 'accepts http env with auth' do
492
- with_env 'http_proxy' => 'http://a%40b:my%20pass@proxy.com:80' do
493
- expect(conn.proxy.user).to eq('a@b')
494
- expect(conn.proxy.password).to eq('my pass')
495
- end
496
- end
497
-
498
- it 'accepts env without scheme' do
499
- with_env 'http_proxy' => 'localhost:8888' do
500
- uri = conn.proxy[:uri]
501
- expect(uri.host).to eq('localhost')
502
- expect(uri.port).to eq(8888)
503
- end
504
- end
505
-
506
- it 'fetches no proxy from nil env' do
507
- with_env 'http_proxy' => nil do
508
- expect(conn.proxy).to be_nil
509
- end
510
- end
511
-
512
- it 'fetches no proxy from blank env' do
513
- with_env 'http_proxy' => '' do
514
- expect(conn.proxy).to be_nil
515
- end
516
- end
517
-
518
- it 'does not accept uppercase env' do
519
- with_env 'HTTP_PROXY' => 'http://localhost:8888/' do
520
- expect(conn.proxy).to be_nil
521
- end
522
- end
523
-
524
- it 'allows when url in no proxy list' do
525
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
526
- conn = Faraday::Connection.new('http://example.com')
527
- expect(conn.proxy).to be_nil
528
- end
529
- end
530
-
531
- it 'allows when url in no proxy list with url_prefix' do
532
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
533
- conn = Faraday::Connection.new
534
- conn.url_prefix = 'http://example.com'
535
- expect(conn.proxy).to be_nil
536
- end
537
- end
538
-
539
- it 'allows when prefixed url is not in no proxy list' do
540
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
541
- conn = Faraday::Connection.new('http://prefixedexample.com')
542
- expect(conn.proxy.host).to eq('proxy.com')
543
- end
544
- end
545
-
546
- it 'allows when subdomain url is in no proxy list' do
547
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
548
- conn = Faraday::Connection.new('http://subdomain.example.com')
549
- expect(conn.proxy).to be_nil
550
- end
551
- end
552
-
553
- it 'allows when url not in no proxy list' do
554
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example2.com' do
555
- conn = Faraday::Connection.new('http://example.com')
556
- expect(conn.proxy.host).to eq('proxy.com')
557
- end
558
- end
559
-
560
- it 'allows when ip address is not in no proxy list but url is' do
561
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'localhost' do
562
- conn = Faraday::Connection.new('http://127.0.0.1')
563
- expect(conn.proxy).to be_nil
564
- end
565
- end
566
-
567
- it 'allows when url is not in no proxy list but ip address is' do
568
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => '127.0.0.1' do
569
- conn = Faraday::Connection.new('http://localhost')
570
- expect(conn.proxy).to be_nil
571
- end
572
- end
573
-
574
- it 'allows in multi element no proxy list' do
575
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example0.com,example.com,example1.com' do
576
- expect(Faraday::Connection.new('http://example0.com').proxy).to be_nil
577
- expect(Faraday::Connection.new('http://example.com').proxy).to be_nil
578
- expect(Faraday::Connection.new('http://example1.com').proxy).to be_nil
579
- expect(Faraday::Connection.new('http://example2.com').proxy.host).to eq('proxy.com')
580
- end
581
- end
582
-
583
- it 'test proxy requires uri' do
584
- expect { conn.proxy = { uri: :bad_uri, user: 'rick' } }.to raise_error(ArgumentError)
585
- end
586
-
587
- it 'uses env http_proxy' do
588
- with_env 'http_proxy' => 'http://proxy.com' do
589
- conn = Faraday.new
590
- expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
591
- expect(conn.proxy_for_request('http://google.co.uk').host).to eq('proxy.com')
592
- end
593
- end
594
-
595
- it 'uses processes no_proxy before http_proxy' do
596
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
597
- conn = Faraday.new
598
- expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
599
- expect(conn.proxy_for_request('http://google.co.uk')).to be_nil
600
- end
601
- end
602
-
603
- it 'uses env https_proxy' do
604
- with_env 'https_proxy' => 'https://proxy.com' do
605
- conn = Faraday.new
606
- expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
607
- expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy.com')
608
- end
609
- end
610
-
611
- it 'uses processes no_proxy before https_proxy' do
612
- with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
613
- conn = Faraday.new
614
- expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
615
- expect(conn.proxy_for_request('https://google.co.uk')).to be_nil
616
- end
617
- end
618
-
619
- it 'gives priority to manually set proxy' do
620
- with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
621
- conn = Faraday.new
622
- conn.proxy = 'http://proxy2.com'
623
-
624
- expect(conn.instance_variable_get(:@manual_proxy)).to be_truthy
625
- expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy2.com')
626
- end
627
- end
628
-
629
- it 'ignores env proxy if set that way' do
630
- with_env_proxy_disabled do
631
- with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
632
- expect(conn.proxy).to be_nil
633
- end
634
- end
635
- end
636
-
637
- context 'performing a request' do
638
- let(:url) { 'http://example.com' }
639
- let(:conn) do
640
- Faraday.new do |f|
641
- f.adapter :test do |stubs|
642
- stubs.get(url) do
643
- [200, {}, 'ok']
644
- end
645
- end
646
- end
647
- end
648
-
649
- it 'dynamically checks proxy' do
650
- with_env 'http_proxy' => 'http://proxy.com:80' do
651
- expect(conn.proxy.uri.host).to eq('proxy.com')
652
-
653
- conn.get(url) do |req|
654
- expect(req.options.proxy.uri.host).to eq('proxy.com')
655
- end
656
- end
657
-
658
- conn.get(url)
659
- expect(conn.instance_variable_get(:@temp_proxy)).to be_nil
660
- end
661
-
662
- it 'dynamically check no proxy' do
663
- with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
664
- expect(conn.proxy.uri.host).to eq('proxy.com')
665
-
666
- conn.get('http://example.com') do |req|
667
- expect(req.options.proxy).to be_nil
668
- end
669
- end
670
- end
671
- end
672
- end
673
-
674
- describe '#dup' do
675
- subject { conn.dup }
676
-
677
- let(:url) { 'http://httpbingo.org/foo' }
678
- let(:options) do
679
- {
680
- ssl: { verify: :none },
681
- headers: { 'content-type' => 'text/plain' },
682
- params: { 'a' => '1' },
683
- request: { timeout: 5 }
684
- }
685
- end
686
-
687
- it { expect(subject.build_exclusive_url).to eq(conn.build_exclusive_url) }
688
- it { expect(subject.headers['content-type']).to eq('text/plain') }
689
- it { expect(subject.params['a']).to eq('1') }
690
-
691
- context 'after manual changes' do
692
- before do
693
- subject.headers['content-length'] = 12
694
- subject.params['b'] = '2'
695
- subject.options[:open_timeout] = 10
696
- end
697
-
698
- it { expect(subject.builder.handlers.size).to eq(1) }
699
- it { expect(conn.builder.handlers.size).to eq(1) }
700
- it { expect(conn.headers.key?('content-length')).to be_falsey }
701
- it { expect(conn.params.key?('b')).to be_falsey }
702
- it { expect(subject.options[:timeout]).to eq(5) }
703
- it { expect(conn.options[:open_timeout]).to be_nil }
704
- end
705
-
706
- context 'with a manually set proxy' do
707
- it 'preserves the proxy over env http_proxy' do
708
- with_env 'http_proxy' => 'http://env-proxy.com' do
709
- conn.proxy = 'http://manual-proxy.com'
710
-
711
- expect(subject.proxy.host).to eq('manual-proxy.com')
712
- expect(subject.proxy_for_request(url).host).to eq('manual-proxy.com')
713
- end
714
- end
715
-
716
- it 'preserves an explicitly disabled proxy' do
717
- with_env 'http_proxy' => 'http://env-proxy.com' do
718
- conn.proxy = nil
719
-
720
- expect(subject.proxy).to be_nil
721
- expect(subject.proxy_for_request(url)).to be_nil
722
- end
723
- end
724
- end
725
-
726
- context 'without a manually set proxy' do
727
- it 'uses env http_proxy' do
728
- with_env 'http_proxy' => 'http://env-proxy.com' do
729
- expect(subject.proxy_for_request(url).host).to eq('env-proxy.com')
730
- end
731
- end
732
- end
733
- end
734
-
735
- describe '#respond_to?' do
736
- it { expect(Faraday.respond_to?(:get)).to be_truthy }
737
- it { expect(Faraday.respond_to?(:post)).to be_truthy }
738
- end
739
-
740
- describe 'default_connection_options' do
741
- context 'assigning a default value' do
742
- before do
743
- Faraday.default_connection_options = nil
744
- Faraday.default_connection_options.request.timeout = 10
745
- end
746
-
747
- it_behaves_like 'default connection options'
748
- end
749
-
750
- context 'assigning a hash' do
751
- before { Faraday.default_connection_options = { request: { timeout: 10 } } }
752
-
753
- it_behaves_like 'default connection options'
754
- end
755
-
756
- context 'preserving a user_agent assigned via default_conncetion_options' do
757
- around do |example|
758
- old = Faraday.default_connection_options
759
- Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
760
- example.run
761
- Faraday.default_connection_options = old
762
- end
763
-
764
- context 'when url is a Hash' do
765
- let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
766
-
767
- it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
768
- end
769
-
770
- context 'when url is a String' do
771
- let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
772
-
773
- it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
774
- end
775
- end
776
- end
777
-
778
- describe 'request params' do
779
- context 'with simple url' do
780
- let(:url) { 'http://example.com' }
781
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
782
-
783
- before do
784
- conn.adapter(:test, stubs)
785
- stubs.get('http://example.com?a=a&p=3') do
786
- [200, {}, 'ok']
787
- end
788
- end
789
-
790
- after { stubs.verify_stubbed_calls }
791
-
792
- it 'test_overrides_request_params' do
793
- conn.get('?p=2&a=a', p: 3)
794
- end
795
-
796
- it 'test_overrides_request_params_block' do
797
- conn.get('?p=1&a=a', p: 2) do |req|
798
- req.params[:p] = 3
799
- end
800
- end
801
-
802
- it 'test_overrides_request_params_block_url' do
803
- conn.get(nil, p: 2) do |req|
804
- req.url('?p=1&a=a', 'p' => 3)
805
- end
806
- end
807
- end
808
-
809
- context 'with url and extra params' do
810
- let(:url) { 'http://example.com?a=1&b=2' }
811
- let(:options) { { params: { c: 3 } } }
812
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
813
-
814
- before do
815
- conn.adapter(:test, stubs)
816
- end
817
-
818
- it 'merges connection and request params' do
819
- expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
820
- stubs.get(expected) { [200, {}, 'ok'] }
821
- conn.get('?page=1', limit: 5)
822
- stubs.verify_stubbed_calls
823
- end
824
-
825
- it 'allows to override all params' do
826
- expected = 'http://example.com?b=b'
827
- stubs.get(expected) { [200, {}, 'ok'] }
828
- conn.get('?p=1&a=a', p: 2) do |req|
829
- expect(req.params[:a]).to eq('a')
830
- expect(req.params['c']).to eq(3)
831
- expect(req.params['p']).to eq(2)
832
- req.params = { b: 'b' }
833
- expect(req.params['b']).to eq('b')
834
- end
835
- stubs.verify_stubbed_calls
836
- end
837
-
838
- it 'allows to set params_encoder for single request' do
839
- encoder = CustomEncoder.new
840
- expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
841
- stubs.get(expected) { [200, {}, 'ok'] }
842
-
843
- conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
844
- req.options.params_encoder = encoder
845
- end
846
- stubs.verify_stubbed_calls
847
- end
848
- end
849
-
850
- context 'with default params encoder' do
851
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
852
-
853
- before do
854
- conn.adapter(:test, stubs)
855
- stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
856
- [200, {}, 'ok']
857
- end
858
- end
859
-
860
- after { stubs.verify_stubbed_calls }
861
-
862
- it 'supports array params in url' do
863
- conn.get('http://example.com?color[]=blue&color[]=red')
864
- end
865
-
866
- it 'supports array params in params' do
867
- conn.get('http://example.com', color: %w[blue red])
868
- end
869
- end
870
-
871
- context 'with flat params encoder' do
872
- let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
873
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
874
-
875
- before do
876
- conn.adapter(:test, stubs)
877
- stubs.get('http://example.com?color=blue&color=red') do
878
- [200, {}, 'ok']
879
- end
880
- end
881
-
882
- after { stubs.verify_stubbed_calls }
883
-
884
- it 'supports array params in params' do
885
- conn.get('http://example.com', color: %w[blue red])
886
- end
887
-
888
- context 'with array param in url' do
889
- let(:url) { 'http://example.com?color[]=blue&color[]=red' }
890
-
891
- it do
892
- conn.get('/')
893
- end
894
- end
895
- end
896
- end
897
- end