faraday 1.7.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -3
  3. data/README.md +11 -9
  4. data/examples/client_spec.rb +19 -19
  5. data/examples/client_test.rb +22 -22
  6. data/lib/faraday/adapter/test.rb +10 -4
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +17 -93
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -8
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +17 -63
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +23 -20
  16. data/lib/faraday/request/authorization.rb +32 -38
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/url_encoded.rb +2 -0
  20. data/lib/faraday/request.rb +11 -31
  21. data/lib/faraday/response/json.rb +54 -0
  22. data/lib/faraday/response/logger.rb +4 -4
  23. data/lib/faraday/response/raise_error.rb +9 -1
  24. data/lib/faraday/response.rb +8 -19
  25. data/lib/faraday/utils/headers.rb +1 -1
  26. data/lib/faraday/utils.rb +10 -5
  27. data/lib/faraday/version.rb +1 -1
  28. data/lib/faraday.rb +11 -39
  29. data/spec/faraday/connection_spec.rb +136 -85
  30. data/spec/faraday/middleware_registry_spec.rb +31 -0
  31. data/spec/faraday/options/env_spec.rb +2 -2
  32. data/spec/faraday/rack_builder_spec.rb +26 -54
  33. data/spec/faraday/request/authorization_spec.rb +19 -24
  34. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  35. data/spec/faraday/request/json_spec.rb +111 -0
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  37. data/spec/faraday/request_spec.rb +4 -15
  38. data/spec/faraday/response/json_spec.rb +117 -0
  39. data/spec/faraday/response/raise_error_spec.rb +7 -4
  40. data/spec/faraday/utils/headers_spec.rb +2 -2
  41. data/spec/faraday/utils_spec.rb +62 -1
  42. data/spec/support/fake_safe_buffer.rb +1 -1
  43. data/spec/support/helper_methods.rb +0 -37
  44. data/spec/support/shared_examples/adapter.rb +0 -1
  45. data/spec/support/shared_examples/request_method.rb +5 -18
  46. metadata +12 -147
  47. data/lib/faraday/adapter/typhoeus.rb +0 -15
  48. data/lib/faraday/autoload.rb +0 -87
  49. data/lib/faraday/dependency_loader.rb +0 -37
  50. data/lib/faraday/file_part.rb +0 -128
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/request/basic_authentication.rb +0 -20
  53. data/lib/faraday/request/multipart.rb +0 -106
  54. data/lib/faraday/request/retry.rb +0 -239
  55. data/lib/faraday/request/token_authentication.rb +0 -20
  56. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  57. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  58. data/spec/faraday/adapter/excon_spec.rb +0 -49
  59. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  60. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  61. data/spec/faraday/adapter/patron_spec.rb +0 -18
  62. data/spec/faraday/adapter/rack_spec.rb +0 -8
  63. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  64. data/spec/faraday/composite_read_io_spec.rb +0 -80
  65. data/spec/faraday/request/multipart_spec.rb +0 -302
  66. data/spec/faraday/request/retry_spec.rb +0 -242
  67. data/spec/faraday/response/middleware_spec.rb +0 -68
  68. 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(',').map { |pair| pair.split('-') }.to_h
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
 
@@ -124,8 +140,8 @@ RSpec.describe Faraday::Connection do
124
140
  context 'with block' do
125
141
  let(:conn) do
126
142
  Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
127
- faraday.adapter :net_http
128
- faraday.url_prefix = 'http://sushi.com/omnom'
143
+ faraday.adapter :test
144
+ faraday.url_prefix = 'http://httpbingo.org/omnom'
129
145
  end
130
146
  end
131
147
 
@@ -135,41 +151,22 @@ RSpec.describe Faraday::Connection do
135
151
  end
136
152
 
137
153
  describe '#close' do
154
+ before { Faraday.default_adapter = :test }
155
+ after { Faraday.default_adapter = nil }
156
+
138
157
  it 'can close underlying app' do
139
158
  expect(conn.app).to receive(:close)
140
159
  conn.close
141
160
  end
142
161
  end
143
162
 
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
163
  describe '#build_exclusive_url' do
167
164
  context 'with relative path' do
168
165
  subject { conn.build_exclusive_url('sake.html') }
169
166
 
170
167
  it 'uses connection host as default host' do
171
- conn.host = 'sushi.com'
172
- expect(subject.host).to eq('sushi.com')
168
+ conn.host = 'httpbingo.org'
169
+ expect(subject.host).to eq('httpbingo.org')
173
170
  expect(subject.scheme).to eq('http')
174
171
  end
175
172
 
@@ -206,10 +203,10 @@ RSpec.describe Faraday::Connection do
206
203
  end
207
204
 
208
205
  context 'with complete url' do
209
- subject { conn.build_exclusive_url('http://sushi.com/sake.html?a=1') }
206
+ subject { conn.build_exclusive_url('http://httpbingo.org/sake.html?a=1') }
210
207
 
211
208
  it { expect(subject.scheme).to eq('http') }
212
- it { expect(subject.host).to eq('sushi.com') }
209
+ it { expect(subject.host).to eq('httpbingo.org') }
213
210
  it { expect(subject.port).to eq(80) }
214
211
  it { expect(subject.path).to eq('/sake.html') }
215
212
  it { expect(subject.query).to eq('a=1') }
@@ -217,35 +214,35 @@ RSpec.describe Faraday::Connection do
217
214
 
218
215
  it 'overrides connection port for absolute url' do
219
216
  conn.port = 23
220
- uri = conn.build_exclusive_url('http://sushi.com')
217
+ uri = conn.build_exclusive_url('http://httpbingo.org')
221
218
  expect(uri.port).to eq(80)
222
219
  end
223
220
 
224
221
  it 'does not add ending slash given nil url' do
225
- conn.url_prefix = 'http://sushi.com/nigiri'
222
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
226
223
  uri = conn.build_exclusive_url
227
224
  expect(uri.path).to eq('/nigiri')
228
225
  end
229
226
 
230
227
  it 'does not add ending slash given empty url' do
231
- conn.url_prefix = 'http://sushi.com/nigiri'
228
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
232
229
  uri = conn.build_exclusive_url('')
233
230
  expect(uri.path).to eq('/nigiri')
234
231
  end
235
232
 
236
233
  it 'does not use connection params' do
237
- conn.url_prefix = 'http://sushi.com/nigiri'
234
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
238
235
  conn.params = { a: 1 }
239
- expect(conn.build_exclusive_url.to_s).to eq('http://sushi.com/nigiri')
236
+ expect(conn.build_exclusive_url.to_s).to eq('http://httpbingo.org/nigiri')
240
237
  end
241
238
 
242
239
  it 'allows to provide params argument' do
243
- conn.url_prefix = 'http://sushi.com/nigiri'
240
+ conn.url_prefix = 'http://httpbingo.org/nigiri'
244
241
  conn.params = { a: 1 }
245
242
  params = Faraday::Utils::ParamsHash.new
246
243
  params[:a] = 2
247
244
  uri = conn.build_exclusive_url(nil, params)
248
- expect(uri.to_s).to eq('http://sushi.com/nigiri?a=2')
245
+ expect(uri.to_s).to eq('http://httpbingo.org/nigiri?a=2')
249
246
  end
250
247
 
251
248
  it 'handles uri instances' do
@@ -254,34 +251,34 @@ RSpec.describe Faraday::Connection do
254
251
  end
255
252
 
256
253
  it 'always returns new URI instance' do
257
- conn.url_prefix = 'http://sushi.com'
254
+ conn.url_prefix = 'http://httpbingo.org'
258
255
  uri1 = conn.build_exclusive_url(nil)
259
256
  uri2 = conn.build_exclusive_url(nil)
260
257
  expect(uri1).not_to equal(uri2)
261
258
  end
262
259
 
263
260
  context 'with url_prefixed connection' do
264
- let(:url) { 'http://sushi.com/sushi/' }
261
+ let(:url) { 'http://httpbingo.org/get/' }
265
262
 
266
263
  it 'parses url and changes scheme' do
267
264
  conn.scheme = 'https'
268
265
  uri = conn.build_exclusive_url('sake.html')
269
- expect(uri.to_s).to eq('https://sushi.com/sushi/sake.html')
266
+ expect(uri.to_s).to eq('https://httpbingo.org/get/sake.html')
270
267
  end
271
268
 
272
269
  it 'joins url to base with ending slash' do
273
270
  uri = conn.build_exclusive_url('sake.html')
274
- expect(uri.to_s).to eq('http://sushi.com/sushi/sake.html')
271
+ expect(uri.to_s).to eq('http://httpbingo.org/get/sake.html')
275
272
  end
276
273
 
277
274
  it 'used default base with ending slash' do
278
275
  uri = conn.build_exclusive_url
279
- expect(uri.to_s).to eq('http://sushi.com/sushi/')
276
+ expect(uri.to_s).to eq('http://httpbingo.org/get/')
280
277
  end
281
278
 
282
279
  it 'overrides base' do
283
280
  uri = conn.build_exclusive_url('/sake/')
284
- expect(uri.to_s).to eq('http://sushi.com/sake/')
281
+ expect(uri.to_s).to eq('http://httpbingo.org/sake/')
285
282
  end
286
283
  end
287
284
 
@@ -310,22 +307,22 @@ RSpec.describe Faraday::Connection do
310
307
  end
311
308
 
312
309
  describe '#build_url' do
313
- let(:url) { 'http://sushi.com/nigiri' }
310
+ let(:url) { 'http://httpbingo.org/nigiri' }
314
311
 
315
312
  it 'uses params' do
316
313
  conn.params = { a: 1, b: 1 }
317
- expect(conn.build_url.to_s).to eq('http://sushi.com/nigiri?a=1&b=1')
314
+ expect(conn.build_url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=1')
318
315
  end
319
316
 
320
317
  it 'merges params' do
321
318
  conn.params = { a: 1, b: 1 }
322
319
  url = conn.build_url(nil, b: 2, c: 3)
323
- expect(url.to_s).to eq('http://sushi.com/nigiri?a=1&b=2&c=3')
320
+ expect(url.to_s).to eq('http://httpbingo.org/nigiri?a=1&b=2&c=3')
324
321
  end
325
322
  end
326
323
 
327
324
  describe '#build_request' do
328
- let(:url) { 'https://asushi.com/sake.html' }
325
+ let(:url) { 'https://ahttpbingo.org/sake.html' }
329
326
  let(:request) { conn.build_request(:get) }
330
327
 
331
328
  before do
@@ -342,7 +339,7 @@ RSpec.describe Faraday::Connection do
342
339
  describe '#to_env' do
343
340
  subject { conn.build_request(:get).to_env(conn).url }
344
341
 
345
- let(:url) { 'http://sushi.com/sake.html' }
342
+ let(:url) { 'http://httpbingo.org/sake.html' }
346
343
  let(:options) { { params: @params } }
347
344
 
348
345
  it 'parses url params into query' do
@@ -556,26 +553,32 @@ RSpec.describe Faraday::Connection do
556
553
  end
557
554
 
558
555
  context 'performing a request' do
559
- before { stub_request(:get, 'http://example.com') }
556
+ let(:url) { 'http://example.com' }
557
+ let(:conn) do
558
+ Faraday.new do |f|
559
+ f.adapter :test do |stubs|
560
+ stubs.get(url) do
561
+ [200, {}, 'ok']
562
+ end
563
+ end
564
+ end
565
+ end
560
566
 
561
567
  it 'dynamically checks proxy' do
562
568
  with_env 'http_proxy' => 'http://proxy.com:80' do
563
- conn = Faraday.new
564
569
  expect(conn.proxy.uri.host).to eq('proxy.com')
565
570
 
566
- conn.get('http://example.com') do |req|
571
+ conn.get(url) do |req|
567
572
  expect(req.options.proxy.uri.host).to eq('proxy.com')
568
573
  end
569
574
  end
570
575
 
571
- conn.get('http://example.com')
576
+ conn.get(url)
572
577
  expect(conn.instance_variable_get('@temp_proxy')).to be_nil
573
578
  end
574
579
 
575
580
  it 'dynamically check no proxy' do
576
581
  with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
577
- conn = Faraday.new
578
-
579
582
  expect(conn.proxy.uri.host).to eq('proxy.com')
580
583
 
581
584
  conn.get('http://example.com') do |req|
@@ -589,7 +592,7 @@ RSpec.describe Faraday::Connection do
589
592
  describe '#dup' do
590
593
  subject { conn.dup }
591
594
 
592
- let(:url) { 'http://sushi.com/foo' }
595
+ let(:url) { 'http://httpbingo.org/foo' }
593
596
  let(:options) do
594
597
  {
595
598
  ssl: { verify: :none },
@@ -605,7 +608,6 @@ RSpec.describe Faraday::Connection do
605
608
 
606
609
  context 'after manual changes' do
607
610
  before do
608
- subject.basic_auth('', '')
609
611
  subject.headers['content-length'] = 12
610
612
  subject.params['b'] = '2'
611
613
  subject.options[:open_timeout] = 10
@@ -640,14 +642,42 @@ RSpec.describe Faraday::Connection do
640
642
 
641
643
  it_behaves_like 'default connection options'
642
644
  end
645
+
646
+ context 'preserving a user_agent assigned via default_conncetion_options' do
647
+ around do |example|
648
+ old = Faraday.default_connection_options
649
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
650
+ example.run
651
+ Faraday.default_connection_options = old
652
+ end
653
+
654
+ context 'when url is a Hash' do
655
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
656
+
657
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
658
+ end
659
+
660
+ context 'when url is a String' do
661
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
662
+
663
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
664
+ end
665
+ end
643
666
  end
644
667
 
645
668
  describe 'request params' do
646
669
  context 'with simple url' do
647
670
  let(:url) { 'http://example.com' }
648
- let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
671
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
672
+
673
+ before do
674
+ conn.adapter(:test, stubs)
675
+ stubs.get('http://example.com?a=a&p=3') do
676
+ [200, {}, 'ok']
677
+ end
678
+ end
649
679
 
650
- after { expect(stubbed).to have_been_made.once }
680
+ after { stubs.verify_stubbed_calls }
651
681
 
652
682
  it 'test_overrides_request_params' do
653
683
  conn.get('?p=2&a=a', p: 3)
@@ -669,15 +699,22 @@ RSpec.describe Faraday::Connection do
669
699
  context 'with url and extra params' do
670
700
  let(:url) { 'http://example.com?a=1&b=2' }
671
701
  let(:options) { { params: { c: 3 } } }
702
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
703
+
704
+ before do
705
+ conn.adapter(:test, stubs)
706
+ end
672
707
 
673
708
  it 'merges connection and request params' do
674
- stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
709
+ expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
710
+ stubs.get(expected) { [200, {}, 'ok'] }
675
711
  conn.get('?page=1', limit: 5)
676
- expect(stubbed).to have_been_made.once
712
+ stubs.verify_stubbed_calls
677
713
  end
678
714
 
679
715
  it 'allows to override all params' do
680
- stubbed = stub_request(:get, 'http://example.com?b=b')
716
+ expected = 'http://example.com?b=b'
717
+ stubs.get(expected) { [200, {}, 'ok'] }
681
718
  conn.get('?p=1&a=a', p: 2) do |req|
682
719
  expect(req.params[:a]).to eq('a')
683
720
  expect(req.params['c']).to eq(3)
@@ -685,47 +722,61 @@ RSpec.describe Faraday::Connection do
685
722
  req.params = { b: 'b' }
686
723
  expect(req.params['b']).to eq('b')
687
724
  end
688
- expect(stubbed).to have_been_made.once
725
+ stubs.verify_stubbed_calls
689
726
  end
690
727
 
691
728
  it 'allows to set params_encoder for single request' do
692
- encoder = Object.new
693
- def encoder.encode(params)
694
- params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
695
- end
696
- stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
729
+ encoder = CustomEncoder.new
730
+ expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
731
+ stubs.get(expected) { [200, {}, 'ok'] }
697
732
 
698
- conn.get('/', feeling: 'blue') do |req|
733
+ conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
699
734
  req.options.params_encoder = encoder
700
735
  end
701
- expect(stubbed).to have_been_made.once
736
+ stubs.verify_stubbed_calls
702
737
  end
703
738
  end
704
739
 
705
740
  context 'with default params encoder' do
706
- let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
707
- after { expect(stubbed).to have_been_made.once }
741
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
742
+
743
+ before do
744
+ conn.adapter(:test, stubs)
745
+ stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
746
+ [200, {}, 'ok']
747
+ end
748
+ end
749
+
750
+ after { stubs.verify_stubbed_calls }
708
751
 
709
752
  it 'supports array params in url' do
710
- conn.get('http://example.com?color[]=red&color[]=blue')
753
+ conn.get('http://example.com?color[]=blue&color[]=red')
711
754
  end
712
755
 
713
756
  it 'supports array params in params' do
714
- conn.get('http://example.com', color: %w[red blue])
757
+ conn.get('http://example.com', color: %w[blue red])
715
758
  end
716
759
  end
717
760
 
718
761
  context 'with flat params encoder' do
719
762
  let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
720
- let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
721
- after { expect(stubbed).to have_been_made.once }
763
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
764
+
765
+ before do
766
+ conn.adapter(:test, stubs)
767
+ stubs.get('http://example.com?color=blue&color=red') do
768
+ [200, {}, 'ok']
769
+ end
770
+ end
771
+
772
+ after { stubs.verify_stubbed_calls }
722
773
 
723
774
  it 'supports array params in params' do
724
- conn.get('http://example.com', color: %w[red blue])
775
+ conn.get('http://example.com', color: %w[blue red])
725
776
  end
726
777
 
727
778
  context 'with array param in url' do
728
- let(:url) { 'http://example.com?color[]=red&color[]=blue' }
779
+ let(:url) { 'http://example.com?color[]=blue&color[]=red' }
729
780
 
730
781
  it do
731
782
  conn.get('/')
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::MiddlewareRegistry do
4
+ before do
5
+ stub_const('CustomMiddleware', custom_middleware_klass)
6
+ end
7
+ let(:custom_middleware_klass) { Class.new(Faraday::Middleware) }
8
+ let(:dummy) { Class.new { extend Faraday::MiddlewareRegistry } }
9
+
10
+ after { dummy.unregister_middleware(:custom) }
11
+
12
+ it 'allows to register with constant' do
13
+ dummy.register_middleware(custom: custom_middleware_klass)
14
+ expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
15
+ end
16
+
17
+ it 'allows to register with symbol' do
18
+ dummy.register_middleware(custom: :CustomMiddleware)
19
+ expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
20
+ end
21
+
22
+ it 'allows to register with string' do
23
+ dummy.register_middleware(custom: 'CustomMiddleware')
24
+ expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
25
+ end
26
+
27
+ it 'allows to register with Proc' do
28
+ dummy.register_middleware(custom: -> { custom_middleware_klass })
29
+ expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
30
+ end
31
+ end
@@ -29,12 +29,12 @@ RSpec.describe Faraday::Env do
29
29
 
30
30
  it 'retains custom members' do
31
31
  env[:foo] = 'custom 1'
32
- env[:bar] = :custom_2
32
+ env[:bar] = :custom2
33
33
  env2 = Faraday::Env.from(env)
34
34
  env2[:baz] = 'custom 3'
35
35
 
36
36
  expect(env2[:foo]).to eq('custom 1')
37
- expect(env2[:bar]).to eq(:custom_2)
37
+ expect(env2[:bar]).to eq(:custom2)
38
38
  expect(env[:baz]).to be_nil
39
39
  end
40
40
 
@@ -12,16 +12,16 @@ RSpec.describe Faraday::RackBuilder do
12
12
 
13
13
  class Apple < Handler
14
14
  end
15
+
15
16
  class Orange < Handler
16
17
  end
17
- class Banana < Handler
18
- end
19
18
 
20
- class Broken < Faraday::Middleware
21
- dependency 'zomg/i_dont/exist'
19
+ class Banana < Handler
22
20
  end
23
21
 
24
22
  subject { conn.builder }
23
+ before { Faraday.default_adapter = :test }
24
+ after { Faraday.default_adapter = nil }
25
25
 
26
26
  context 'with default stack' do
27
27
  let(:conn) { Faraday::Connection.new }
@@ -88,13 +88,6 @@ RSpec.describe Faraday::RackBuilder do
88
88
 
89
89
  it { expect(subject.handlers).to eq([Apple]) }
90
90
 
91
- it 'allows rebuilding' do
92
- subject.build do |builder|
93
- builder.use(Orange)
94
- end
95
- expect(subject.handlers).to eq([Orange])
96
- end
97
-
98
91
  it 'allows use' do
99
92
  subject.use(Orange)
100
93
  expect(subject.handlers).to eq([Apple, Orange])
@@ -117,36 +110,6 @@ RSpec.describe Faraday::RackBuilder do
117
110
  end
118
111
  end
119
112
 
120
- context 'with custom registered middleware' do
121
- let(:conn) { Faraday::Connection.new {} }
122
-
123
- after { Faraday::Middleware.unregister_middleware(:apple) }
124
-
125
- it 'allows to register with constant' do
126
- Faraday::Middleware.register_middleware(apple: Apple)
127
- subject.use(:apple)
128
- expect(subject.handlers).to eq([Apple])
129
- end
130
-
131
- it 'allows to register with symbol' do
132
- Faraday::Middleware.register_middleware(apple: :Apple)
133
- subject.use(:apple)
134
- expect(subject.handlers).to eq([Apple])
135
- end
136
-
137
- it 'allows to register with string' do
138
- Faraday::Middleware.register_middleware(apple: 'Apple')
139
- subject.use(:apple)
140
- expect(subject.handlers).to eq([Apple])
141
- end
142
-
143
- it 'allows to register with Proc' do
144
- Faraday::Middleware.register_middleware(apple: -> { Apple })
145
- subject.use(:apple)
146
- expect(subject.handlers).to eq([Apple])
147
- end
148
- end
149
-
150
113
  context 'when having two handlers' do
151
114
  let(:conn) { Faraday::Connection.new {} }
152
115
 
@@ -176,21 +139,30 @@ RSpec.describe Faraday::RackBuilder do
176
139
  end
177
140
  end
178
141
 
179
- context 'when having a handler with broken dependency' do
180
- let(:conn) do
181
- Faraday::Connection.new do |builder|
182
- builder.adapter :test do |stub|
183
- stub.get('/') { |_| [200, {}, ''] }
142
+ context 'when adapter is added with named options' do
143
+ after { Faraday.default_adapter_options = {} }
144
+ let(:conn) { Faraday::Connection.new {} }
145
+
146
+ let(:cat_adapter) do
147
+ Class.new(Faraday::Adapter) do
148
+ attr_accessor :name
149
+
150
+ def initialize(app, name:)
151
+ super(app)
152
+ @name = name
184
153
  end
185
154
  end
186
155
  end
187
156
 
188
- before { subject.use(Broken) }
157
+ let(:cat) { subject.adapter.build }
189
158
 
190
- it 'raises an error while making a request' do
191
- expect { conn.get('/') }.to raise_error(RuntimeError) do |err|
192
- expect(err.message).to match(%r{missing dependency for Broken: .+ -- zomg/i_dont/exist})
193
- end
159
+ it 'adds a handler to construct adapter with named options' do
160
+ Faraday.default_adapter = cat_adapter
161
+ Faraday.default_adapter_options = { name: 'Chloe' }
162
+ expect { cat }.to_not output(
163
+ /warning: Using the last argument as keyword parameters is deprecated/
164
+ ).to_stderr
165
+ expect(cat.name).to eq 'Chloe'
194
166
  end
195
167
  end
196
168
 
@@ -220,7 +192,7 @@ RSpec.describe Faraday::RackBuilder do
220
192
  end
221
193
  end
222
194
 
223
- context 'when a request adapter is added with named arguments' do
195
+ context 'when a middleware is added with named arguments' do
224
196
  let(:conn) { Faraday::Connection.new {} }
225
197
 
226
198
  let(:cat_request) do
@@ -247,11 +219,11 @@ RSpec.describe Faraday::RackBuilder do
247
219
  end
248
220
  end
249
221
 
250
- context 'when a response adapter is added with named arguments' do
222
+ context 'when a middleware is added with named arguments' do
251
223
  let(:conn) { Faraday::Connection.new {} }
252
224
 
253
225
  let(:fish_response) do
254
- Class.new(Faraday::Response::Middleware) do
226
+ Class.new(Faraday::Middleware) do
255
227
  attr_accessor :name
256
228
 
257
229
  def initialize(app, name:)