faraday 1.9.3 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +117 -1
- data/README.md +16 -9
- data/examples/client_spec.rb +1 -1
- data/examples/client_test.rb +2 -2
- data/lib/faraday/adapter/test.rb +2 -0
- data/lib/faraday/adapter.rb +2 -5
- data/lib/faraday/connection.rb +5 -86
- data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
- data/lib/faraday/error.rb +3 -2
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/middleware.rb +0 -1
- data/lib/faraday/middleware_registry.rb +15 -79
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +21 -18
- data/lib/faraday/request/authorization.rb +28 -41
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +2 -0
- data/lib/faraday/request.rb +11 -29
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +9 -1
- data/lib/faraday/response.rb +8 -19
- data/lib/faraday/utils/headers.rb +1 -1
- data/lib/faraday/utils.rb +10 -5
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +6 -45
- data/spec/faraday/connection_spec.rb +102 -51
- data/spec/faraday/options/env_spec.rb +2 -2
- data/spec/faraday/rack_builder_spec.rb +7 -50
- data/spec/faraday/request/authorization_spec.rb +19 -32
- 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 +0 -1
- data/spec/faraday/request_spec.rb +0 -11
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/raise_error_spec.rb +7 -4
- 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 +0 -1
- data/spec/support/shared_examples/request_method.rb +5 -18
- metadata +11 -149
- data/lib/faraday/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -87
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -49
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- 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/response/middleware_spec.rb +0 -68
- 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' }
|
@@ -103,6 +113,12 @@ RSpec.describe Faraday::Connection do
|
|
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@sushi.com/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,7 +140,7 @@ 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 :
|
143
|
+
faraday.adapter :test
|
128
144
|
faraday.url_prefix = 'http://sushi.com/omnom'
|
129
145
|
end
|
130
146
|
end
|
@@ -135,34 +151,15 @@ 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') }
|
@@ -556,26 +553,32 @@ RSpec.describe Faraday::Connection do
|
|
556
553
|
end
|
557
554
|
|
558
555
|
context 'performing a request' do
|
559
|
-
|
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(
|
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(
|
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|
|
@@ -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
|
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 {
|
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
|
-
|
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
|
-
|
712
|
+
stubs.verify_stubbed_calls
|
677
713
|
end
|
678
714
|
|
679
715
|
it 'allows to override all params' do
|
680
|
-
|
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
|
-
|
725
|
+
stubs.verify_stubbed_calls
|
689
726
|
end
|
690
727
|
|
691
728
|
it 'allows to set params_encoder for single request' do
|
692
|
-
encoder =
|
693
|
-
|
694
|
-
|
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
|
-
|
736
|
+
stubs.verify_stubbed_calls
|
702
737
|
end
|
703
738
|
end
|
704
739
|
|
705
740
|
context 'with default params encoder' do
|
706
|
-
let
|
707
|
-
|
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[]=
|
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
|
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
|
721
|
-
|
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
|
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[]=
|
779
|
+
let(:url) { 'http://example.com?color[]=blue&color[]=red' }
|
729
780
|
|
730
781
|
it do
|
731
782
|
conn.get('/')
|
@@ -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
|
|
@@ -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
|
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])
|
@@ -127,24 +120,6 @@ RSpec.describe Faraday::RackBuilder do
|
|
127
120
|
subject.use(:apple)
|
128
121
|
expect(subject.handlers).to eq([Apple])
|
129
122
|
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
123
|
end
|
149
124
|
|
150
125
|
context 'when having two handlers' do
|
@@ -176,24 +151,6 @@ RSpec.describe Faraday::RackBuilder do
|
|
176
151
|
end
|
177
152
|
end
|
178
153
|
|
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, {}, ''] }
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
before { subject.use(Broken) }
|
189
|
-
|
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
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
154
|
context 'when middleware is added with named arguments' do
|
198
155
|
let(:conn) { Faraday::Connection.new {} }
|
199
156
|
|
@@ -220,7 +177,7 @@ RSpec.describe Faraday::RackBuilder do
|
|
220
177
|
end
|
221
178
|
end
|
222
179
|
|
223
|
-
context 'when a
|
180
|
+
context 'when a middleware is added with named arguments' do
|
224
181
|
let(:conn) { Faraday::Connection.new {} }
|
225
182
|
|
226
183
|
let(:cat_request) do
|
@@ -247,11 +204,11 @@ RSpec.describe Faraday::RackBuilder do
|
|
247
204
|
end
|
248
205
|
end
|
249
206
|
|
250
|
-
context 'when a
|
207
|
+
context 'when a middleware is added with named arguments' do
|
251
208
|
let(:conn) { Faraday::Connection.new {} }
|
252
209
|
|
253
210
|
let(:fish_response) do
|
254
|
-
Class.new(Faraday::
|
211
|
+
Class.new(Faraday::Middleware) do
|
255
212
|
attr_accessor :name
|
256
213
|
|
257
214
|
def initialize(app, name:)
|
@@ -3,7 +3,7 @@
|
|
3
3
|
RSpec.describe Faraday::Request::Authorization do
|
4
4
|
let(:conn) do
|
5
5
|
Faraday.new do |b|
|
6
|
-
b.request auth_type, *auth_config
|
6
|
+
b.request :authorization, auth_type, *auth_config
|
7
7
|
b.adapter :test do |stub|
|
8
8
|
stub.get('/auth-echo') do |env|
|
9
9
|
[200, {}, env[:request_headers]['Authorization']]
|
@@ -14,10 +14,10 @@ RSpec.describe Faraday::Request::Authorization do
|
|
14
14
|
|
15
15
|
shared_examples 'does not interfere with existing authentication' do
|
16
16
|
context 'and request already has an authentication header' do
|
17
|
-
let(:response) { conn.get('/auth-echo', nil, authorization: '
|
17
|
+
let(:response) { conn.get('/auth-echo', nil, authorization: 'OAuth oauth_token') }
|
18
18
|
|
19
19
|
it 'does not interfere with existing authorization' do
|
20
|
-
expect(response.body).to eq('
|
20
|
+
expect(response.body).to eq('OAuth oauth_token')
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -25,7 +25,7 @@ RSpec.describe Faraday::Request::Authorization do
|
|
25
25
|
let(:response) { conn.get('/auth-echo') }
|
26
26
|
|
27
27
|
describe 'basic_auth' do
|
28
|
-
let(:auth_type) { :
|
28
|
+
let(:auth_type) { :basic }
|
29
29
|
|
30
30
|
context 'when passed correct params' do
|
31
31
|
let(:auth_config) { %w[aladdin opensesame] }
|
@@ -44,51 +44,38 @@ RSpec.describe Faraday::Request::Authorization do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe '
|
48
|
-
let(:auth_type) { :
|
49
|
-
|
50
|
-
context 'when passed correct params' do
|
51
|
-
let(:auth_config) { 'quux' }
|
52
|
-
|
53
|
-
it { expect(response.body).to eq('Token token="quux"') }
|
54
|
-
|
55
|
-
include_examples 'does not interfere with existing authentication'
|
56
|
-
end
|
47
|
+
describe 'authorization' do
|
48
|
+
let(:auth_type) { :Bearer }
|
57
49
|
|
58
|
-
context 'when
|
59
|
-
let(:auth_config) { ['
|
50
|
+
context 'when passed a string' do
|
51
|
+
let(:auth_config) { ['custom'] }
|
60
52
|
|
61
|
-
it { expect(response.body).to
|
62
|
-
it { expect(response.body).to match(/token="baz"/) }
|
63
|
-
it { expect(response.body).to match(/foo="42"/) }
|
53
|
+
it { expect(response.body).to eq('Bearer custom') }
|
64
54
|
|
65
55
|
include_examples 'does not interfere with existing authentication'
|
66
56
|
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe 'authorization' do
|
70
|
-
let(:auth_type) { :authorization }
|
71
57
|
|
72
|
-
context 'when passed
|
73
|
-
let(:auth_config) { ['
|
58
|
+
context 'when passed a proc' do
|
59
|
+
let(:auth_config) { [-> { 'custom_from_proc' }] }
|
74
60
|
|
75
|
-
it { expect(response.body).to eq('
|
61
|
+
it { expect(response.body).to eq('Bearer custom_from_proc') }
|
76
62
|
|
77
63
|
include_examples 'does not interfere with existing authentication'
|
78
64
|
end
|
79
65
|
|
80
|
-
context 'when passed a
|
81
|
-
let(:
|
66
|
+
context 'when passed a callable' do
|
67
|
+
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
|
68
|
+
let(:auth_config) { [callable] }
|
82
69
|
|
83
|
-
it { expect(response.body).to eq('
|
70
|
+
it { expect(response.body).to eq('Bearer custom_from_callable') }
|
84
71
|
|
85
72
|
include_examples 'does not interfere with existing authentication'
|
86
73
|
end
|
87
74
|
|
88
|
-
context 'when passed
|
89
|
-
let(:auth_config) { [
|
75
|
+
context 'when passed too many arguments' do
|
76
|
+
let(:auth_config) { %w[baz foo] }
|
90
77
|
|
91
|
-
it { expect
|
78
|
+
it { expect { response }.to raise_error(ArgumentError) }
|
92
79
|
|
93
80
|
include_examples 'does not interfere with existing authentication'
|
94
81
|
end
|
@@ -30,13 +30,11 @@ RSpec.describe Faraday::Request::Instrumentation do
|
|
30
30
|
|
31
31
|
it { expect(options.name).to eq('request.faraday') }
|
32
32
|
it 'defaults to ActiveSupport::Notifications' do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
expect(res).to eq(ActiveSupport::Notifications)
|
39
|
-
end
|
33
|
+
res = options.instrumenter
|
34
|
+
rescue NameError => e
|
35
|
+
expect(e.to_s).to match('ActiveSupport')
|
36
|
+
else
|
37
|
+
expect(res).to eq(ActiveSupport::Notifications)
|
40
38
|
end
|
41
39
|
|
42
40
|
it 'instruments with default name' do
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Request::Json do
|
4
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
5
|
+
|
6
|
+
def process(body, content_type = nil)
|
7
|
+
env = { body: body, request_headers: Faraday::Utils::Headers.new }
|
8
|
+
env[:request_headers]['content-type'] = content_type if content_type
|
9
|
+
middleware.call(Faraday::Env.from(env)).env
|
10
|
+
end
|
11
|
+
|
12
|
+
def result_body
|
13
|
+
result[:body]
|
14
|
+
end
|
15
|
+
|
16
|
+
def result_type
|
17
|
+
result[:request_headers]['content-type']
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'no body' do
|
21
|
+
let(:result) { process(nil) }
|
22
|
+
|
23
|
+
it "doesn't change body" do
|
24
|
+
expect(result_body).to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "doesn't add content type" do
|
28
|
+
expect(result_type).to be_nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'empty body' do
|
33
|
+
let(:result) { process('') }
|
34
|
+
|
35
|
+
it "doesn't change body" do
|
36
|
+
expect(result_body).to be_empty
|
37
|
+
end
|
38
|
+
|
39
|
+
it "doesn't add content type" do
|
40
|
+
expect(result_type).to be_nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'string body' do
|
45
|
+
let(:result) { process('{"a":1}') }
|
46
|
+
|
47
|
+
it "doesn't change body" do
|
48
|
+
expect(result_body).to eq('{"a":1}')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'adds content type' do
|
52
|
+
expect(result_type).to eq('application/json')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'object body' do
|
57
|
+
let(:result) { process(a: 1) }
|
58
|
+
|
59
|
+
it 'encodes body' do
|
60
|
+
expect(result_body).to eq('{"a":1}')
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'adds content type' do
|
64
|
+
expect(result_type).to eq('application/json')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'empty object body' do
|
69
|
+
let(:result) { process({}) }
|
70
|
+
|
71
|
+
it 'encodes body' do
|
72
|
+
expect(result_body).to eq('{}')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'object body with json type' do
|
77
|
+
let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
|
78
|
+
|
79
|
+
it 'encodes body' do
|
80
|
+
expect(result_body).to eq('{"a":1}')
|
81
|
+
end
|
82
|
+
|
83
|
+
it "doesn't change content type" do
|
84
|
+
expect(result_type).to eq('application/json; charset=utf-8')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'object body with vendor json type' do
|
89
|
+
let(:result) { process({ a: 1 }, 'application/vnd.myapp.v1+json; charset=utf-8') }
|
90
|
+
|
91
|
+
it 'encodes body' do
|
92
|
+
expect(result_body).to eq('{"a":1}')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "doesn't change content type" do
|
96
|
+
expect(result_type).to eq('application/vnd.myapp.v1+json; charset=utf-8')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'object body with incompatible type' do
|
101
|
+
let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
|
102
|
+
|
103
|
+
it "doesn't change body" do
|
104
|
+
expect(result_body).to eq(a: 1)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "doesn't change content type" do
|
108
|
+
expect(result_type).to eq('application/xml; charset=utf-8')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -22,17 +22,6 @@ RSpec.describe Faraday::Request do
|
|
22
22
|
it { expect(subject.http_method).to eq(:post) }
|
23
23
|
end
|
24
24
|
|
25
|
-
describe 'deprecate method for HTTP method' do
|
26
|
-
let(:http_method) { :post }
|
27
|
-
let(:expected_warning) do
|
28
|
-
%r{WARNING: `Faraday::Request#method` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.\n`Faraday::Request#method` called from .+/spec/faraday/request_spec.rb:\d+.}
|
29
|
-
end
|
30
|
-
|
31
|
-
it { expect(subject.method).to eq(:post) }
|
32
|
-
|
33
|
-
it { expect { subject.method }.to output(expected_warning).to_stderr }
|
34
|
-
end
|
35
|
-
|
36
25
|
context 'when setting the url on setup with a URI' do
|
37
26
|
let(:block) { proc { |req| req.url URI.parse('foo.json?a=1') } }
|
38
27
|
|