faraday 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +221 -1
- data/LICENSE.md +1 -1
- data/README.md +19 -14
- data/examples/client_spec.rb +36 -4
- data/examples/client_test.rb +43 -4
- data/lib/faraday/adapter/test.rb +61 -43
- data/lib/faraday/adapter.rb +3 -16
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/connection.rb +25 -78
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +9 -4
- data/lib/faraday/error.rb +23 -8
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -5
- data/lib/faraday/middleware_registry.rb +15 -79
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options.rb +7 -11
- data/lib/faraday/rack_builder.rb +34 -30
- data/lib/faraday/request/authorization.rb +32 -36
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +5 -1
- data/lib/faraday/request.rb +13 -23
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +20 -1
- data/lib/faraday/response.rb +8 -22
- data/lib/faraday/utils/headers.rb +3 -3
- data/lib/faraday/utils.rb +21 -8
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +44 -59
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/connection_spec.rb +147 -51
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/env_spec.rb +2 -2
- 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 +144 -38
- data/spec/faraday/request/authorization_spec.rb +19 -24
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +13 -1
- data/spec/faraday/request_spec.rb +6 -6
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/raise_error_spec.rb +66 -0
- data/spec/faraday/utils_spec.rb +62 -1
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/helper_methods.rb +0 -37
- data/spec/support/shared_examples/adapter.rb +2 -2
- data/spec/support/shared_examples/request_method.rb +43 -28
- metadata +16 -48
- data/UPGRADING.md +0 -55
- data/lib/faraday/adapter/em_http.rb +0 -285
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -151
- data/lib/faraday/adapter/net_http.rb +0 -209
- 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/lib/faraday/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -95
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/multipart.rb +0 -99
- data/lib/faraday/request/retry.rb +0 -239
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -47
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/adapter/net_http_spec.rb +0 -64
- data/spec/faraday/adapter/patron_spec.rb +0 -18
- data/spec/faraday/adapter/rack_spec.rb +0 -8
- data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
- data/spec/faraday/composite_read_io_spec.rb +0 -80
- data/spec/faraday/request/multipart_spec.rb +0 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -52
- data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,5 +1,15 @@
|
|
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
15
|
let(:address) { 'http://sushi.com' }
|
@@ -18,6 +28,13 @@ shared_examples 'initializer with url' do
|
|
18
28
|
it { expect(subject.path_prefix).to eq('/fish') }
|
19
29
|
it { expect(subject.params).to eq('a' => '1') }
|
20
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
|
21
38
|
end
|
22
39
|
|
23
40
|
shared_examples 'default connection options' do
|
@@ -96,6 +113,12 @@ RSpec.describe Faraday::Connection do
|
|
96
113
|
it { expect(subject.params).to eq('a' => 3, 'b' => '2') }
|
97
114
|
end
|
98
115
|
|
116
|
+
context 'with basic_auth in url' do
|
117
|
+
let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
|
118
|
+
|
119
|
+
it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
|
120
|
+
end
|
121
|
+
|
99
122
|
context 'with custom headers' do
|
100
123
|
let(:options) { { headers: { user_agent: 'Faraday' } } }
|
101
124
|
|
@@ -117,7 +140,7 @@ RSpec.describe Faraday::Connection do
|
|
117
140
|
context 'with block' do
|
118
141
|
let(:conn) do
|
119
142
|
Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
|
120
|
-
faraday.adapter :
|
143
|
+
faraday.adapter :test
|
121
144
|
faraday.url_prefix = 'http://sushi.com/omnom'
|
122
145
|
end
|
123
146
|
end
|
@@ -128,34 +151,15 @@ RSpec.describe Faraday::Connection do
|
|
128
151
|
end
|
129
152
|
|
130
153
|
describe '#close' do
|
154
|
+
before { Faraday.default_adapter = :test }
|
155
|
+
after { Faraday.default_adapter = nil }
|
156
|
+
|
131
157
|
it 'can close underlying app' do
|
132
158
|
expect(conn.app).to receive(:close)
|
133
159
|
conn.close
|
134
160
|
end
|
135
161
|
end
|
136
162
|
|
137
|
-
describe 'basic_auth' do
|
138
|
-
subject { conn }
|
139
|
-
|
140
|
-
context 'calling the #basic_auth method' do
|
141
|
-
before { subject.basic_auth 'Aladdin', 'open sesame' }
|
142
|
-
|
143
|
-
it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
|
144
|
-
end
|
145
|
-
|
146
|
-
context 'adding basic auth info to url' do
|
147
|
-
let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' }
|
148
|
-
|
149
|
-
it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') }
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe '#token_auth' do
|
154
|
-
before { subject.token_auth('abcdef', nonce: 'abc') }
|
155
|
-
|
156
|
-
it { expect(subject.headers['Authorization']).to eq('Token nonce="abc", token="abcdef"') }
|
157
|
-
end
|
158
|
-
|
159
163
|
describe '#build_exclusive_url' do
|
160
164
|
context 'with relative path' do
|
161
165
|
subject { conn.build_exclusive_url('sake.html') }
|
@@ -246,6 +250,13 @@ RSpec.describe Faraday::Connection do
|
|
246
250
|
expect(uri.path).to eq('/sake.html')
|
247
251
|
end
|
248
252
|
|
253
|
+
it 'always returns new URI instance' do
|
254
|
+
conn.url_prefix = 'http://sushi.com'
|
255
|
+
uri1 = conn.build_exclusive_url(nil)
|
256
|
+
uri2 = conn.build_exclusive_url(nil)
|
257
|
+
expect(uri1).not_to equal(uri2)
|
258
|
+
end
|
259
|
+
|
249
260
|
context 'with url_prefixed connection' do
|
250
261
|
let(:url) { 'http://sushi.com/sushi/' }
|
251
262
|
|
@@ -270,6 +281,29 @@ RSpec.describe Faraday::Connection do
|
|
270
281
|
expect(uri.to_s).to eq('http://sushi.com/sake/')
|
271
282
|
end
|
272
283
|
end
|
284
|
+
|
285
|
+
context 'with colon in path' do
|
286
|
+
let(:url) { 'http://service.com' }
|
287
|
+
|
288
|
+
it 'joins url to base when used absolute path' do
|
289
|
+
conn = Faraday.new(url: url)
|
290
|
+
uri = conn.build_exclusive_url('/service:search?limit=400')
|
291
|
+
expect(uri.to_s).to eq('http://service.com/service:search?limit=400')
|
292
|
+
end
|
293
|
+
|
294
|
+
it 'joins url to base when used relative path' do
|
295
|
+
conn = Faraday.new(url: url)
|
296
|
+
uri = conn.build_exclusive_url('service:search?limit=400')
|
297
|
+
expect(uri.to_s).to eq('http://service.com/service%3Asearch?limit=400')
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'joins url to base when used with path prefix' do
|
301
|
+
conn = Faraday.new(url: url)
|
302
|
+
conn.path_prefix = '/api'
|
303
|
+
uri = conn.build_exclusive_url('service:search?limit=400')
|
304
|
+
expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400')
|
305
|
+
end
|
306
|
+
end
|
273
307
|
end
|
274
308
|
|
275
309
|
describe '#build_url' do
|
@@ -412,6 +446,14 @@ RSpec.describe Faraday::Connection do
|
|
412
446
|
end
|
413
447
|
end
|
414
448
|
|
449
|
+
it 'allows when url in no proxy list with url_prefix' do
|
450
|
+
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
451
|
+
conn = Faraday::Connection.new
|
452
|
+
conn.url_prefix = 'http://example.com'
|
453
|
+
expect(conn.proxy).to be_nil
|
454
|
+
end
|
455
|
+
end
|
456
|
+
|
415
457
|
it 'allows when prefixed url is not in no proxy list' do
|
416
458
|
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
417
459
|
conn = Faraday::Connection.new('http://prefixedexample.com')
|
@@ -511,26 +553,32 @@ RSpec.describe Faraday::Connection do
|
|
511
553
|
end
|
512
554
|
|
513
555
|
context 'performing a request' do
|
514
|
-
|
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
|
515
566
|
|
516
567
|
it 'dynamically checks proxy' do
|
517
568
|
with_env 'http_proxy' => 'http://proxy.com:80' do
|
518
|
-
conn = Faraday.new
|
519
569
|
expect(conn.proxy.uri.host).to eq('proxy.com')
|
520
570
|
|
521
|
-
conn.get(
|
571
|
+
conn.get(url) do |req|
|
522
572
|
expect(req.options.proxy.uri.host).to eq('proxy.com')
|
523
573
|
end
|
524
574
|
end
|
525
575
|
|
526
|
-
conn.get(
|
576
|
+
conn.get(url)
|
527
577
|
expect(conn.instance_variable_get('@temp_proxy')).to be_nil
|
528
578
|
end
|
529
579
|
|
530
580
|
it 'dynamically check no proxy' do
|
531
581
|
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
|
532
|
-
conn = Faraday.new
|
533
|
-
|
534
582
|
expect(conn.proxy.uri.host).to eq('proxy.com')
|
535
583
|
|
536
584
|
conn.get('http://example.com') do |req|
|
@@ -560,7 +608,6 @@ RSpec.describe Faraday::Connection do
|
|
560
608
|
|
561
609
|
context 'after manual changes' do
|
562
610
|
before do
|
563
|
-
subject.basic_auth('', '')
|
564
611
|
subject.headers['content-length'] = 12
|
565
612
|
subject.params['b'] = '2'
|
566
613
|
subject.options[:open_timeout] = 10
|
@@ -595,14 +642,42 @@ RSpec.describe Faraday::Connection do
|
|
595
642
|
|
596
643
|
it_behaves_like 'default connection options'
|
597
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
|
598
666
|
end
|
599
667
|
|
600
668
|
describe 'request params' do
|
601
669
|
context 'with simple url' do
|
602
670
|
let(:url) { 'http://example.com' }
|
603
|
-
let
|
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
|
604
679
|
|
605
|
-
after {
|
680
|
+
after { stubs.verify_stubbed_calls }
|
606
681
|
|
607
682
|
it 'test_overrides_request_params' do
|
608
683
|
conn.get('?p=2&a=a', p: 3)
|
@@ -624,15 +699,22 @@ RSpec.describe Faraday::Connection do
|
|
624
699
|
context 'with url and extra params' do
|
625
700
|
let(:url) { 'http://example.com?a=1&b=2' }
|
626
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
|
627
707
|
|
628
708
|
it 'merges connection and request params' do
|
629
|
-
|
709
|
+
expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
|
710
|
+
stubs.get(expected) { [200, {}, 'ok'] }
|
630
711
|
conn.get('?page=1', limit: 5)
|
631
|
-
|
712
|
+
stubs.verify_stubbed_calls
|
632
713
|
end
|
633
714
|
|
634
715
|
it 'allows to override all params' do
|
635
|
-
|
716
|
+
expected = 'http://example.com?b=b'
|
717
|
+
stubs.get(expected) { [200, {}, 'ok'] }
|
636
718
|
conn.get('?p=1&a=a', p: 2) do |req|
|
637
719
|
expect(req.params[:a]).to eq('a')
|
638
720
|
expect(req.params['c']).to eq(3)
|
@@ -640,47 +722,61 @@ RSpec.describe Faraday::Connection do
|
|
640
722
|
req.params = { b: 'b' }
|
641
723
|
expect(req.params['b']).to eq('b')
|
642
724
|
end
|
643
|
-
|
725
|
+
stubs.verify_stubbed_calls
|
644
726
|
end
|
645
727
|
|
646
728
|
it 'allows to set params_encoder for single request' do
|
647
|
-
encoder =
|
648
|
-
|
649
|
-
|
650
|
-
end
|
651
|
-
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'] }
|
652
732
|
|
653
|
-
conn.get('/', feeling: 'blue') do |req|
|
733
|
+
conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
|
654
734
|
req.options.params_encoder = encoder
|
655
735
|
end
|
656
|
-
|
736
|
+
stubs.verify_stubbed_calls
|
657
737
|
end
|
658
738
|
end
|
659
739
|
|
660
740
|
context 'with default params encoder' do
|
661
|
-
let
|
662
|
-
|
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 }
|
663
751
|
|
664
752
|
it 'supports array params in url' do
|
665
|
-
conn.get('http://example.com?color[]=
|
753
|
+
conn.get('http://example.com?color[]=blue&color[]=red')
|
666
754
|
end
|
667
755
|
|
668
756
|
it 'supports array params in params' do
|
669
|
-
conn.get('http://example.com', color: %w[red
|
757
|
+
conn.get('http://example.com', color: %w[blue red])
|
670
758
|
end
|
671
759
|
end
|
672
760
|
|
673
761
|
context 'with flat params encoder' do
|
674
762
|
let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
|
675
|
-
let
|
676
|
-
|
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 }
|
677
773
|
|
678
774
|
it 'supports array params in params' do
|
679
|
-
conn.get('http://example.com', color: %w[red
|
775
|
+
conn.get('http://example.com', color: %w[blue red])
|
680
776
|
end
|
681
777
|
|
682
778
|
context 'with array param in url' do
|
683
|
-
let(:url) { 'http://example.com?color[]=
|
779
|
+
let(:url) { 'http://example.com?color[]=blue&color[]=red' }
|
684
780
|
|
685
781
|
it do
|
686
782
|
conn.get('/')
|
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
|
@@ -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] = :
|
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(:
|
37
|
+
expect(env2[:bar]).to eq(:custom2)
|
38
38
|
expect(env[:baz]).to be_nil
|
39
39
|
end
|
40
40
|
|
@@ -14,6 +14,13 @@ RSpec.describe Faraday::ProxyOptions do
|
|
14
14
|
expect(options.inspect).to match('#<Faraday::ProxyOptions uri=')
|
15
15
|
end
|
16
16
|
|
17
|
+
it 'defaults to http' do
|
18
|
+
options = Faraday::ProxyOptions.from 'example.org'
|
19
|
+
expect(options.port).to eq(80)
|
20
|
+
expect(options.host).to eq('example.org')
|
21
|
+
expect(options.scheme).to eq('http')
|
22
|
+
end
|
23
|
+
|
17
24
|
it 'works with nil' do
|
18
25
|
options = Faraday::ProxyOptions.from nil
|
19
26
|
expect(options).to be_a_kind_of(Faraday::ProxyOptions)
|
@@ -31,4 +31,12 @@ RSpec.describe Faraday::FlatParamsEncoder do
|
|
31
31
|
params = { a: [] }
|
32
32
|
expect(subject.encode(params)).to eq('a=')
|
33
33
|
end
|
34
|
+
|
35
|
+
it 'encodes unsorted when asked' do
|
36
|
+
params = { b: false, a: true }
|
37
|
+
expect(subject.encode(params)).to eq('a=true&b=false')
|
38
|
+
Faraday::FlatParamsEncoder.sort_params = false
|
39
|
+
expect(subject.encode(params)).to eq('b=false&a=true')
|
40
|
+
Faraday::FlatParamsEncoder.sort_params = true
|
41
|
+
end
|
34
42
|
end
|
@@ -94,6 +94,14 @@ RSpec.describe Faraday::NestedParamsEncoder do
|
|
94
94
|
expect(subject.encode(params)).to eq('a%5B%5D=true&a%5B%5D=false')
|
95
95
|
end
|
96
96
|
|
97
|
+
it 'encodes unsorted when asked' do
|
98
|
+
params = { b: false, a: true }
|
99
|
+
expect(subject.encode(params)).to eq('a=true&b=false')
|
100
|
+
Faraday::NestedParamsEncoder.sort_params = false
|
101
|
+
expect(subject.encode(params)).to eq('b=false&a=true')
|
102
|
+
Faraday::NestedParamsEncoder.sort_params = true
|
103
|
+
end
|
104
|
+
|
97
105
|
shared_examples 'a wrong decoding' do
|
98
106
|
it do
|
99
107
|
expect { subject.decode(query) }.to raise_error(TypeError) do |e|
|