faraday 1.4.1 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +197 -3
  3. data/LICENSE.md +1 -1
  4. data/README.md +34 -20
  5. data/Rakefile +3 -1
  6. data/examples/client_spec.rb +67 -13
  7. data/examples/client_test.rb +80 -15
  8. data/lib/faraday/adapter/test.rb +117 -52
  9. data/lib/faraday/adapter.rb +5 -14
  10. data/lib/faraday/connection.rb +70 -130
  11. data/lib/faraday/encoders/nested_params_encoder.rb +14 -7
  12. data/lib/faraday/error.rb +20 -11
  13. data/lib/faraday/logging/formatter.rb +28 -15
  14. data/lib/faraday/middleware.rb +3 -1
  15. data/lib/faraday/middleware_registry.rb +17 -63
  16. data/lib/faraday/options/connection_options.rb +7 -6
  17. data/lib/faraday/options/env.rb +85 -62
  18. data/lib/faraday/options/proxy_options.rb +11 -3
  19. data/lib/faraday/options/request_options.rb +7 -6
  20. data/lib/faraday/options/ssl_options.rb +56 -45
  21. data/lib/faraday/options.rb +7 -6
  22. data/lib/faraday/rack_builder.rb +23 -21
  23. data/lib/faraday/request/authorization.rb +37 -38
  24. data/lib/faraday/request/instrumentation.rb +5 -1
  25. data/lib/faraday/request/json.rb +70 -0
  26. data/lib/faraday/request/url_encoded.rb +5 -1
  27. data/lib/faraday/request.rb +20 -37
  28. data/lib/faraday/response/json.rb +73 -0
  29. data/lib/faraday/response/logger.rb +8 -4
  30. data/lib/faraday/response/raise_error.rb +33 -6
  31. data/lib/faraday/response.rb +10 -20
  32. data/lib/faraday/utils/headers.rb +7 -2
  33. data/lib/faraday/utils.rb +11 -7
  34. data/lib/faraday/version.rb +1 -1
  35. data/lib/faraday.rb +10 -31
  36. data/spec/faraday/adapter/test_spec.rb +182 -0
  37. data/spec/faraday/connection_spec.rb +177 -90
  38. data/spec/faraday/error_spec.rb +31 -6
  39. data/spec/faraday/middleware_registry_spec.rb +31 -0
  40. data/spec/faraday/middleware_spec.rb +18 -0
  41. data/spec/faraday/options/env_spec.rb +8 -2
  42. data/spec/faraday/options/options_spec.rb +1 -1
  43. data/spec/faraday/options/proxy_options_spec.rb +15 -0
  44. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  45. data/spec/faraday/rack_builder_spec.rb +26 -54
  46. data/spec/faraday/request/authorization_spec.rb +54 -24
  47. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  48. data/spec/faraday/request/json_spec.rb +199 -0
  49. data/spec/faraday/request/url_encoded_spec.rb +12 -2
  50. data/spec/faraday/request_spec.rb +5 -15
  51. data/spec/faraday/response/json_spec.rb +189 -0
  52. data/spec/faraday/response/logger_spec.rb +38 -0
  53. data/spec/faraday/response/raise_error_spec.rb +47 -5
  54. data/spec/faraday/response_spec.rb +3 -1
  55. data/spec/faraday/utils/headers_spec.rb +22 -4
  56. data/spec/faraday/utils_spec.rb +63 -1
  57. data/spec/faraday_spec.rb +8 -4
  58. data/spec/spec_helper.rb +6 -5
  59. data/spec/support/fake_safe_buffer.rb +1 -1
  60. data/spec/support/helper_methods.rb +0 -37
  61. data/spec/support/shared_examples/adapter.rb +2 -2
  62. data/spec/support/shared_examples/request_method.rb +22 -21
  63. metadata +14 -94
  64. data/lib/faraday/adapter/em_http.rb +0 -289
  65. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  66. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  67. data/lib/faraday/adapter/em_synchrony.rb +0 -153
  68. data/lib/faraday/adapter/httpclient.rb +0 -152
  69. data/lib/faraday/adapter/patron.rb +0 -132
  70. data/lib/faraday/adapter/rack.rb +0 -75
  71. data/lib/faraday/adapter/typhoeus.rb +0 -15
  72. data/lib/faraday/autoload.rb +0 -92
  73. data/lib/faraday/dependency_loader.rb +0 -37
  74. data/lib/faraday/file_part.rb +0 -128
  75. data/lib/faraday/param_part.rb +0 -53
  76. data/lib/faraday/request/basic_authentication.rb +0 -20
  77. data/lib/faraday/request/multipart.rb +0 -106
  78. data/lib/faraday/request/retry.rb +0 -239
  79. data/lib/faraday/request/token_authentication.rb +0 -20
  80. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  81. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  82. data/spec/faraday/adapter/excon_spec.rb +0 -49
  83. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  84. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  85. data/spec/faraday/adapter/patron_spec.rb +0 -18
  86. data/spec/faraday/adapter/rack_spec.rb +0 -8
  87. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  88. data/spec/faraday/composite_read_io_spec.rb +0 -80
  89. data/spec/faraday/request/multipart_spec.rb +0 -302
  90. data/spec/faraday/request/retry_spec.rb +0 -242
  91. data/spec/faraday/response/middleware_spec.rb +0 -68
  92. data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,10 +1,20 @@
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(',').to_h { |pair| pair.split('-') }
10
+ end
11
+ end
12
+
3
13
  shared_examples 'initializer with url' do
4
14
  context 'with simple url' do
5
- let(:address) { 'http://sushi.com' }
15
+ let(:address) { 'http://httpbingo.org' }
6
16
 
7
- it { expect(subject.host).to eq('sushi.com') }
17
+ it { expect(subject.host).to eq('httpbingo.org') }
8
18
  it { expect(subject.port).to eq(80) }
9
19
  it { expect(subject.scheme).to eq('http') }
10
20
  it { expect(subject.path_prefix).to eq('/') }
@@ -12,7 +22,7 @@ shared_examples 'initializer with url' do
12
22
  end
13
23
 
14
24
  context 'with complex url' do
15
- let(:address) { 'http://sushi.com:815/fish?a=1' }
25
+ let(:address) { 'http://httpbingo.org:815/fish?a=1' }
16
26
 
17
27
  it { expect(subject.port).to eq(815) }
18
28
  it { expect(subject.path_prefix).to eq('/fish') }
@@ -31,17 +41,17 @@ shared_examples 'default connection options' do
31
41
  after { Faraday.default_connection_options = nil }
32
42
 
33
43
  it 'works with implicit url' do
34
- conn = Faraday.new 'http://sushi.com/foo'
44
+ conn = Faraday.new 'http://httpbingo.org/foo'
35
45
  expect(conn.options.timeout).to eq(10)
36
46
  end
37
47
 
38
48
  it 'works with option url' do
39
- conn = Faraday.new url: 'http://sushi.com/foo'
49
+ conn = Faraday.new url: 'http://httpbingo.org/foo'
40
50
  expect(conn.options.timeout).to eq(10)
41
51
  end
42
52
 
43
53
  it 'works with instance connection options' do
44
- conn = Faraday.new 'http://sushi.com/foo', request: { open_timeout: 1 }
54
+ conn = Faraday.new 'http://httpbingo.org/foo', request: { open_timeout: 1 }
45
55
  expect(conn.options.timeout).to eq(10)
46
56
  expect(conn.options.open_timeout).to eq(1)
47
57
  end
@@ -51,7 +61,7 @@ shared_examples 'default connection options' do
51
61
  conn.options.timeout = 1
52
62
  expect(Faraday.default_connection_options.request.timeout).to eq(10)
53
63
 
54
- other = Faraday.new url: 'https://sushi.com/foo'
64
+ other = Faraday.new url: 'https://httpbingo.org/foo'
55
65
  other.options.timeout = 1
56
66
 
57
67
  expect(Faraday.default_connection_options.request.timeout).to eq(10)
@@ -71,14 +81,14 @@ RSpec.describe Faraday::Connection do
71
81
  subject { conn }
72
82
 
73
83
  context 'with implicit url param' do
74
- # Faraday::Connection.new('http://sushi.com')
84
+ # Faraday::Connection.new('http://httpbingo.org')
75
85
  let(:url) { address }
76
86
 
77
87
  it_behaves_like 'initializer with url'
78
88
  end
79
89
 
80
90
  context 'with explicit url param' do
81
- # Faraday::Connection.new(url: 'http://sushi.com')
91
+ # Faraday::Connection.new(url: 'http://httpbingo.org')
82
92
  let(:url) { { url: address } }
83
93
 
84
94
  it_behaves_like 'initializer with url'
@@ -98,11 +108,17 @@ RSpec.describe Faraday::Connection do
98
108
  end
99
109
 
100
110
  context 'with custom params and params in url' do
101
- let(:url) { 'http://sushi.com/fish?a=1&b=2' }
111
+ let(:url) { 'http://httpbingo.org/fish?a=1&b=2' }
102
112
  let(:options) { { params: { a: 3 } } }
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@httpbingo.org/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
 
@@ -115,6 +131,12 @@ RSpec.describe Faraday::Connection do
115
131
  it { expect(subject.ssl.verify?).to be_falsey }
116
132
  end
117
133
 
134
+ context 'with verify_hostname false' do
135
+ let(:options) { { ssl: { verify_hostname: false } } }
136
+
137
+ it { expect(subject.ssl.verify_hostname?).to be_falsey }
138
+ end
139
+
118
140
  context 'with empty block' do
119
141
  let(:conn) { Faraday::Connection.new {} }
120
142
 
@@ -124,8 +146,8 @@ RSpec.describe Faraday::Connection do
124
146
  context 'with block' do
125
147
  let(:conn) do
126
148
  Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
127
- faraday.adapter :net_http
128
- faraday.url_prefix = 'http://sushi.com/omnom'
149
+ faraday.adapter :test
150
+ faraday.url_prefix = 'http://httpbingo.org/omnom'
129
151
  end
130
152
  end
131
153
 
@@ -135,41 +157,22 @@ RSpec.describe Faraday::Connection do
135
157
  end
136
158
 
137
159
  describe '#close' do
160
+ before { Faraday.default_adapter = :test }
161
+ after { Faraday.default_adapter = nil }
162
+
138
163
  it 'can close underlying app' do
139
164
  expect(conn.app).to receive(:close)
140
165
  conn.close
141
166
  end
142
167
  end
143
168
 
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
169
  describe '#build_exclusive_url' do
167
170
  context 'with relative path' do
168
171
  subject { conn.build_exclusive_url('sake.html') }
169
172
 
170
173
  it 'uses connection host as default host' do
171
- conn.host = 'sushi.com'
172
- expect(subject.host).to eq('sushi.com')
174
+ conn.host = 'httpbingo.org'
175
+ expect(subject.host).to eq('httpbingo.org')
173
176
  expect(subject.scheme).to eq('http')
174
177
  end
175
178
 
@@ -206,10 +209,10 @@ RSpec.describe Faraday::Connection do
206
209
  end
207
210
 
208
211
  context 'with complete url' do
209
- subject { conn.build_exclusive_url('http://sushi.com/sake.html?a=1') }
212
+ subject { conn.build_exclusive_url('http://httpbingo.org/sake.html?a=1') }
210
213
 
211
214
  it { expect(subject.scheme).to eq('http') }
212
- it { expect(subject.host).to eq('sushi.com') }
215
+ it { expect(subject.host).to eq('httpbingo.org') }
213
216
  it { expect(subject.port).to eq(80) }
214
217
  it { expect(subject.path).to eq('/sake.html') }
215
218
  it { expect(subject.query).to eq('a=1') }
@@ -217,35 +220,35 @@ RSpec.describe Faraday::Connection do
217
220
 
218
221
  it 'overrides connection port for absolute url' do
219
222
  conn.port = 23
220
- uri = conn.build_exclusive_url('http://sushi.com')
223
+ uri = conn.build_exclusive_url('http://httpbingo.org')
221
224
  expect(uri.port).to eq(80)
222
225
  end
223
226
 
224
227
  it 'does not add ending slash given nil url' do
225
- conn.url_prefix = 'http://sushi.com/nigiri'
228
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
226
229
  uri = conn.build_exclusive_url
227
230
  expect(uri.path).to eq('/nigiri')
228
231
  end
229
232
 
230
233
  it 'does not add ending slash given empty url' do
231
- conn.url_prefix = 'http://sushi.com/nigiri'
234
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
232
235
  uri = conn.build_exclusive_url('')
233
236
  expect(uri.path).to eq('/nigiri')
234
237
  end
235
238
 
236
239
  it 'does not use connection params' do
237
- conn.url_prefix = 'http://sushi.com/nigiri'
240
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
238
241
  conn.params = { a: 1 }
239
- expect(conn.build_exclusive_url.to_s).to eq('http://sushi.com/nigiri')
242
+ expect(conn.build_exclusive_url.to_s).to eq('http://httpbingo.org/nigiri')
240
243
  end
241
244
 
242
245
  it 'allows to provide params argument' do
243
- conn.url_prefix = 'http://sushi.com/nigiri'
246
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
244
247
  conn.params = { a: 1 }
245
248
  params = Faraday::Utils::ParamsHash.new
246
249
  params[:a] = 2
247
250
  uri = conn.build_exclusive_url(nil, params)
248
- expect(uri.to_s).to eq('http://sushi.com/nigiri?a=2')
251
+ expect(uri.to_s).to eq('http://httpbingo.org/nigiri?a=2')
249
252
  end
250
253
 
251
254
  it 'handles uri instances' do
@@ -253,28 +256,35 @@ RSpec.describe Faraday::Connection do
253
256
  expect(uri.path).to eq('/sake.html')
254
257
  end
255
258
 
259
+ it 'always returns new URI instance' do
260
+ conn.url_prefix = 'http://httpbingo.org'
261
+ uri1 = conn.build_exclusive_url(nil)
262
+ uri2 = conn.build_exclusive_url(nil)
263
+ expect(uri1).not_to equal(uri2)
264
+ end
265
+
256
266
  context 'with url_prefixed connection' do
257
- let(:url) { 'http://sushi.com/sushi/' }
267
+ let(:url) { 'http://httpbingo.org/get/' }
258
268
 
259
269
  it 'parses url and changes scheme' do
260
270
  conn.scheme = 'https'
261
271
  uri = conn.build_exclusive_url('sake.html')
262
- expect(uri.to_s).to eq('https://sushi.com/sushi/sake.html')
272
+ expect(uri.to_s).to eq('https://httpbingo.org/get/sake.html')
263
273
  end
264
274
 
265
275
  it 'joins url to base with ending slash' do
266
276
  uri = conn.build_exclusive_url('sake.html')
267
- expect(uri.to_s).to eq('http://sushi.com/sushi/sake.html')
277
+ expect(uri.to_s).to eq('http://httpbingo.org/get/sake.html')
268
278
  end
269
279
 
270
280
  it 'used default base with ending slash' do
271
281
  uri = conn.build_exclusive_url
272
- expect(uri.to_s).to eq('http://sushi.com/sushi/')
282
+ expect(uri.to_s).to eq('http://httpbingo.org/get/')
273
283
  end
274
284
 
275
285
  it 'overrides base' do
276
286
  uri = conn.build_exclusive_url('/sake/')
277
- expect(uri.to_s).to eq('http://sushi.com/sake/')
287
+ expect(uri.to_s).to eq('http://httpbingo.org/sake/')
278
288
  end
279
289
  end
280
290
 
@@ -300,25 +310,40 @@ RSpec.describe Faraday::Connection do
300
310
  expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400')
301
311
  end
302
312
  end
313
+
314
+ context 'with a custom `default_uri_parser`' do
315
+ let(:url) { 'http://httpbingo.org' }
316
+ let(:parser) { Addressable::URI }
317
+
318
+ around do |example|
319
+ with_default_uri_parser(parser) do
320
+ example.run
321
+ end
322
+ end
323
+
324
+ it 'does not raise error' do
325
+ expect { conn.build_exclusive_url('/nigiri') }.not_to raise_error
326
+ end
327
+ end
303
328
  end
304
329
 
305
330
  describe '#build_url' do
306
- let(:url) { 'http://sushi.com/nigiri' }
331
+ let(:url) { 'http://httpbingo.org/nigiri' }
307
332
 
308
333
  it 'uses params' do
309
334
  conn.params = { a: 1, b: 1 }
310
- expect(conn.build_url.to_s).to eq('http://sushi.com/nigiri?a=1&b=1')
335
+ expect(conn.build_url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=1')
311
336
  end
312
337
 
313
338
  it 'merges params' do
314
339
  conn.params = { a: 1, b: 1 }
315
340
  url = conn.build_url(nil, b: 2, c: 3)
316
- expect(url.to_s).to eq('http://sushi.com/nigiri?a=1&b=2&c=3')
341
+ expect(url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=2&c=3')
317
342
  end
318
343
  end
319
344
 
320
345
  describe '#build_request' do
321
- let(:url) { 'https://asushi.com/sake.html' }
346
+ let(:url) { 'https://ahttpbingo.org/sake.html' }
322
347
  let(:request) { conn.build_request(:get) }
323
348
 
324
349
  before do
@@ -335,7 +360,7 @@ RSpec.describe Faraday::Connection do
335
360
  describe '#to_env' do
336
361
  subject { conn.build_request(:get).to_env(conn).url }
337
362
 
338
- let(:url) { 'http://sushi.com/sake.html' }
363
+ let(:url) { 'http://httpbingo.org/sake.html' }
339
364
  let(:options) { { params: @params } }
340
365
 
341
366
  it 'parses url params into query' do
@@ -442,6 +467,14 @@ RSpec.describe Faraday::Connection do
442
467
  end
443
468
  end
444
469
 
470
+ it 'allows when url in no proxy list with url_prefix' do
471
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
472
+ conn = Faraday::Connection.new
473
+ conn.url_prefix = 'http://example.com'
474
+ expect(conn.proxy).to be_nil
475
+ end
476
+ end
477
+
445
478
  it 'allows when prefixed url is not in no proxy list' do
446
479
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
447
480
  conn = Faraday::Connection.new('http://prefixedexample.com')
@@ -493,7 +526,7 @@ RSpec.describe Faraday::Connection do
493
526
  it 'uses env http_proxy' do
494
527
  with_env 'http_proxy' => 'http://proxy.com' do
495
528
  conn = Faraday.new
496
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
529
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
497
530
  expect(conn.proxy_for_request('http://google.co.uk').host).to eq('proxy.com')
498
531
  end
499
532
  end
@@ -501,7 +534,7 @@ RSpec.describe Faraday::Connection do
501
534
  it 'uses processes no_proxy before http_proxy' do
502
535
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
503
536
  conn = Faraday.new
504
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
537
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
505
538
  expect(conn.proxy_for_request('http://google.co.uk')).to be_nil
506
539
  end
507
540
  end
@@ -509,7 +542,7 @@ RSpec.describe Faraday::Connection do
509
542
  it 'uses env https_proxy' do
510
543
  with_env 'https_proxy' => 'https://proxy.com' do
511
544
  conn = Faraday.new
512
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
545
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
513
546
  expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy.com')
514
547
  end
515
548
  end
@@ -517,7 +550,7 @@ RSpec.describe Faraday::Connection do
517
550
  it 'uses processes no_proxy before https_proxy' do
518
551
  with_env 'https_proxy' => 'https://proxy.com', 'no_proxy' => 'google.co.uk' do
519
552
  conn = Faraday.new
520
- expect(conn.instance_variable_get('@manual_proxy')).to be_falsey
553
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_falsey
521
554
  expect(conn.proxy_for_request('https://google.co.uk')).to be_nil
522
555
  end
523
556
  end
@@ -527,7 +560,7 @@ RSpec.describe Faraday::Connection do
527
560
  conn = Faraday.new
528
561
  conn.proxy = 'http://proxy2.com'
529
562
 
530
- expect(conn.instance_variable_get('@manual_proxy')).to be_truthy
563
+ expect(conn.instance_variable_get(:@manual_proxy)).to be_truthy
531
564
  expect(conn.proxy_for_request('https://google.co.uk').host).to eq('proxy2.com')
532
565
  end
533
566
  end
@@ -541,26 +574,32 @@ RSpec.describe Faraday::Connection do
541
574
  end
542
575
 
543
576
  context 'performing a request' do
544
- before { stub_request(:get, 'http://example.com') }
577
+ let(:url) { 'http://example.com' }
578
+ let(:conn) do
579
+ Faraday.new do |f|
580
+ f.adapter :test do |stubs|
581
+ stubs.get(url) do
582
+ [200, {}, 'ok']
583
+ end
584
+ end
585
+ end
586
+ end
545
587
 
546
588
  it 'dynamically checks proxy' do
547
589
  with_env 'http_proxy' => 'http://proxy.com:80' do
548
- conn = Faraday.new
549
590
  expect(conn.proxy.uri.host).to eq('proxy.com')
550
591
 
551
- conn.get('http://example.com') do |req|
592
+ conn.get(url) do |req|
552
593
  expect(req.options.proxy.uri.host).to eq('proxy.com')
553
594
  end
554
595
  end
555
596
 
556
- conn.get('http://example.com')
557
- expect(conn.instance_variable_get('@temp_proxy')).to be_nil
597
+ conn.get(url)
598
+ expect(conn.instance_variable_get(:@temp_proxy)).to be_nil
558
599
  end
559
600
 
560
601
  it 'dynamically check no proxy' do
561
602
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
562
- conn = Faraday.new
563
-
564
603
  expect(conn.proxy.uri.host).to eq('proxy.com')
565
604
 
566
605
  conn.get('http://example.com') do |req|
@@ -574,7 +613,7 @@ RSpec.describe Faraday::Connection do
574
613
  describe '#dup' do
575
614
  subject { conn.dup }
576
615
 
577
- let(:url) { 'http://sushi.com/foo' }
616
+ let(:url) { 'http://httpbingo.org/foo' }
578
617
  let(:options) do
579
618
  {
580
619
  ssl: { verify: :none },
@@ -590,7 +629,6 @@ RSpec.describe Faraday::Connection do
590
629
 
591
630
  context 'after manual changes' do
592
631
  before do
593
- subject.basic_auth('', '')
594
632
  subject.headers['content-length'] = 12
595
633
  subject.params['b'] = '2'
596
634
  subject.options[:open_timeout] = 10
@@ -625,14 +663,42 @@ RSpec.describe Faraday::Connection do
625
663
 
626
664
  it_behaves_like 'default connection options'
627
665
  end
666
+
667
+ context 'preserving a user_agent assigned via default_conncetion_options' do
668
+ around do |example|
669
+ old = Faraday.default_connection_options
670
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
671
+ example.run
672
+ Faraday.default_connection_options = old
673
+ end
674
+
675
+ context 'when url is a Hash' do
676
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
677
+
678
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
679
+ end
680
+
681
+ context 'when url is a String' do
682
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
683
+
684
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
685
+ end
686
+ end
628
687
  end
629
688
 
630
689
  describe 'request params' do
631
690
  context 'with simple url' do
632
691
  let(:url) { 'http://example.com' }
633
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
692
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
634
693
 
635
- after { expect(stubbed).to have_been_made.once }
694
+ before do
695
+ conn.adapter(:test, stubs)
696
+ stubs.get('http://example.com?a=a&p=3') do
697
+ [200, {}, 'ok']
698
+ end
699
+ end
700
+
701
+ after { stubs.verify_stubbed_calls }
636
702
 
637
703
  it 'test_overrides_request_params' do
638
704
  conn.get('?p=2&a=a', p: 3)
@@ -654,15 +720,22 @@ RSpec.describe Faraday::Connection do
654
720
  context 'with url and extra params' do
655
721
  let(:url) { 'http://example.com?a=1&b=2' }
656
722
  let(:options) { { params: { c: 3 } } }
723
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
724
+
725
+ before do
726
+ conn.adapter(:test, stubs)
727
+ end
657
728
 
658
729
  it 'merges connection and request params' do
659
- stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
730
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
731
+ stubs.get(expected) { [200, {}, 'ok'] }
660
732
  conn.get('?page=1', limit: 5)
661
- expect(stubbed).to have_been_made.once
733
+ stubs.verify_stubbed_calls
662
734
  end
663
735
 
664
736
  it 'allows to override all params' do
665
- stubbed = stub_request(:get, 'http://example.com?b=b')
737
+ expected = 'http://example.com?b=b'
738
+ stubs.get(expected) { [200, {}, 'ok'] }
666
739
  conn.get('?p=1&a=a', p: 2) do |req|
667
740
  expect(req.params[:a]).to eq('a')
668
741
  expect(req.params['c']).to eq(3)
@@ -670,47 +743,61 @@ RSpec.describe Faraday::Connection do
670
743
  req.params = { b: 'b' }
671
744
  expect(req.params['b']).to eq('b')
672
745
  end
673
- expect(stubbed).to have_been_made.once
746
+ stubs.verify_stubbed_calls
674
747
  end
675
748
 
676
749
  it 'allows to set params_encoder for single request' do
677
- encoder = Object.new
678
- def encoder.encode(params)
679
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
680
- end
681
- stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
750
+ encoder = CustomEncoder.new
751
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
752
+ stubs.get(expected) { [200, {}, 'ok'] }
682
753
 
683
- conn.get('/', feeling: 'blue') do |req|
754
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
684
755
  req.options.params_encoder = encoder
685
756
  end
686
- expect(stubbed).to have_been_made.once
757
+ stubs.verify_stubbed_calls
687
758
  end
688
759
  end
689
760
 
690
761
  context 'with default params encoder' do
691
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
692
- after { expect(stubbed).to have_been_made.once }
762
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
763
+
764
+ before do
765
+ conn.adapter(:test, stubs)
766
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
767
+ [200, {}, 'ok']
768
+ end
769
+ end
770
+
771
+ after { stubs.verify_stubbed_calls }
693
772
 
694
773
  it 'supports array params in url' do
695
- conn.get('http://example.com?color[]=red&color[]=blue')
774
+ conn.get('http://example.com?color[]=blue&color[]=red')
696
775
  end
697
776
 
698
777
  it 'supports array params in params' do
699
- conn.get('http://example.com', color: %w[red blue])
778
+ conn.get('http://example.com', color: %w[blue red])
700
779
  end
701
780
  end
702
781
 
703
782
  context 'with flat params encoder' do
704
783
  let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
705
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
706
- after { expect(stubbed).to have_been_made.once }
784
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
785
+
786
+ before do
787
+ conn.adapter(:test, stubs)
788
+ stubs.get('http://example.com?color=blue&color=red') do
789
+ [200, {}, 'ok']
790
+ end
791
+ end
792
+
793
+ after { stubs.verify_stubbed_calls }
707
794
 
708
795
  it 'supports array params in params' do
709
- conn.get('http://example.com', color: %w[red blue])
796
+ conn.get('http://example.com', color: %w[blue red])
710
797
  end
711
798
 
712
799
  context 'with array param in url' do
713
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
800
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
714
801
 
715
802
  it do
716
803
  conn.get('/')
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Faraday::ClientError do
3
+ RSpec.describe Faraday::Error do
4
4
  describe '.initialize' do
5
5
  subject { described_class.new(exception, response) }
6
6
  let(:response) { nil }
@@ -12,8 +12,10 @@ RSpec.describe Faraday::ClientError do
12
12
  it { expect(subject.response).to be_nil }
13
13
  it { expect(subject.message).to eq(exception.message) }
14
14
  it { expect(subject.backtrace).to eq(exception.backtrace) }
15
- it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
15
+ it { expect(subject.inspect).to eq('#<Faraday::Error wrapped=#<RuntimeError: test>>') }
16
16
  it { expect(subject.response_status).to be_nil }
17
+ it { expect(subject.response_headers).to be_nil }
18
+ it { expect(subject.response_body).to be_nil }
17
19
  end
18
20
 
19
21
  context 'with response hash' do
@@ -22,8 +24,10 @@ RSpec.describe Faraday::ClientError do
22
24
  it { expect(subject.wrapped_exception).to be_nil }
23
25
  it { expect(subject.response).to eq(exception) }
24
26
  it { expect(subject.message).to eq('the server responded with status 400') }
25
- it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
27
+ it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
26
28
  it { expect(subject.response_status).to eq(400) }
29
+ it { expect(subject.response_headers).to be_nil }
30
+ it { expect(subject.response_body).to be_nil }
27
31
  end
28
32
 
29
33
  context 'with string' do
@@ -32,8 +36,10 @@ RSpec.describe Faraday::ClientError do
32
36
  it { expect(subject.wrapped_exception).to be_nil }
33
37
  it { expect(subject.response).to be_nil }
34
38
  it { expect(subject.message).to eq('custom message') }
35
- it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
39
+ it { expect(subject.inspect).to eq('#<Faraday::Error #<Faraday::Error: custom message>>') }
36
40
  it { expect(subject.response_status).to be_nil }
41
+ it { expect(subject.response_headers).to be_nil }
42
+ it { expect(subject.response_body).to be_nil }
37
43
  end
38
44
 
39
45
  context 'with anything else #to_s' do
@@ -42,8 +48,10 @@ RSpec.describe Faraday::ClientError do
42
48
  it { expect(subject.wrapped_exception).to be_nil }
43
49
  it { expect(subject.response).to be_nil }
44
50
  it { expect(subject.message).to eq('["error1", "error2"]') }
45
- it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
51
+ it { expect(subject.inspect).to eq('#<Faraday::Error #<Faraday::Error: ["error1", "error2"]>>') }
46
52
  it { expect(subject.response_status).to be_nil }
53
+ it { expect(subject.response_headers).to be_nil }
54
+ it { expect(subject.response_body).to be_nil }
47
55
  end
48
56
 
49
57
  context 'with exception string and response hash' do
@@ -53,8 +61,25 @@ RSpec.describe Faraday::ClientError do
53
61
  it { expect(subject.wrapped_exception).to be_nil }
54
62
  it { expect(subject.response).to eq(response) }
55
63
  it { expect(subject.message).to eq('custom message') }
56
- it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
64
+ it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
57
65
  it { expect(subject.response_status).to eq(400) }
66
+ it { expect(subject.response_headers).to be_nil }
67
+ it { expect(subject.response_body).to be_nil }
68
+ end
69
+
70
+ context 'with exception and response object' do
71
+ let(:exception) { RuntimeError.new('test') }
72
+ let(:body) { { test: 'test' } }
73
+ let(:headers) { { 'Content-Type' => 'application/json' } }
74
+ let(:response) { Faraday::Response.new(status: 400, response_headers: headers, response_body: body) }
75
+
76
+ it { expect(subject.wrapped_exception).to eq(exception) }
77
+ it { expect(subject.response).to eq(response) }
78
+ it { expect(subject.message).to eq(exception.message) }
79
+ it { expect(subject.backtrace).to eq(exception.backtrace) }
80
+ it { expect(subject.response_status).to eq(400) }
81
+ it { expect(subject.response_headers).to eq(headers) }
82
+ it { expect(subject.response_body).to eq(body) }
58
83
  end
59
84
  end
60
85
  end