faraday 1.10.1 → 2.12.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 +198 -4
- data/LICENSE.md +1 -1
- data/README.md +34 -20
- data/Rakefile +6 -1
- data/examples/client_spec.rb +41 -19
- data/examples/client_test.rb +48 -22
- data/lib/faraday/adapter/test.rb +62 -13
- data/lib/faraday/adapter.rb +6 -10
- data/lib/faraday/connection.rb +72 -150
- data/lib/faraday/encoders/nested_params_encoder.rb +14 -7
- data/lib/faraday/error.rb +24 -5
- data/lib/faraday/logging/formatter.rb +28 -15
- data/lib/faraday/middleware.rb +43 -2
- data/lib/faraday/middleware_registry.rb +17 -63
- data/lib/faraday/options/connection_options.rb +7 -6
- data/lib/faraday/options/env.rb +85 -62
- data/lib/faraday/options/proxy_options.rb +7 -3
- data/lib/faraday/options/request_options.rb +7 -6
- data/lib/faraday/options/ssl_options.rb +59 -45
- data/lib/faraday/options.rb +7 -6
- data/lib/faraday/rack_builder.rb +23 -21
- data/lib/faraday/request/authorization.rb +33 -41
- data/lib/faraday/request/instrumentation.rb +5 -1
- data/lib/faraday/request/json.rb +18 -3
- data/lib/faraday/request/url_encoded.rb +5 -1
- data/lib/faraday/request.rb +15 -31
- data/lib/faraday/response/json.rb +25 -5
- data/lib/faraday/response/logger.rb +6 -0
- data/lib/faraday/response/raise_error.rb +45 -18
- data/lib/faraday/response.rb +9 -21
- data/lib/faraday/utils/headers.rb +15 -4
- data/lib/faraday/utils.rb +11 -7
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +8 -44
- data/spec/faraday/adapter/test_spec.rb +65 -0
- data/spec/faraday/connection_spec.rb +165 -93
- data/spec/faraday/error_spec.rb +31 -6
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +161 -0
- data/spec/faraday/options/env_spec.rb +8 -2
- data/spec/faraday/options/options_spec.rb +1 -1
- data/spec/faraday/options/proxy_options_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +10 -1
- data/spec/faraday/rack_builder_spec.rb +26 -54
- data/spec/faraday/request/authorization_spec.rb +50 -28
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/json_spec.rb +88 -0
- data/spec/faraday/request/url_encoded_spec.rb +12 -2
- data/spec/faraday/request_spec.rb +5 -15
- data/spec/faraday/response/json_spec.rb +93 -6
- data/spec/faraday/response/logger_spec.rb +38 -0
- data/spec/faraday/response/raise_error_spec.rb +111 -5
- data/spec/faraday/response_spec.rb +3 -1
- data/spec/faraday/utils/headers_spec.rb +31 -4
- data/spec/faraday/utils_spec.rb +64 -1
- data/spec/faraday_spec.rb +10 -4
- data/spec/spec_helper.rb +5 -6
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/faraday_middleware_subclasses.rb +18 -0
- 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 +22 -21
- metadata +24 -145
- 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/deprecate.rb +0 -109
- 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/deprecate_spec.rb +0 -147
- data/spec/faraday/response/middleware_spec.rb +0 -68
- data/spec/support/webmock_rack_app.rb +0 -68
@@ -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
|
@@ -33,6 +33,24 @@ RSpec.describe Faraday::Middleware do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
describe '#on_error' do
|
37
|
+
subject do
|
38
|
+
Class.new(described_class) do
|
39
|
+
def on_error(error)
|
40
|
+
# do nothing
|
41
|
+
end
|
42
|
+
end.new(app)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'is called by #call' do
|
46
|
+
expect(app).to receive(:call).and_raise(Faraday::ConnectionFailed)
|
47
|
+
is_expected.to receive(:call).and_call_original
|
48
|
+
is_expected.to receive(:on_error)
|
49
|
+
|
50
|
+
expect { subject.call(double) }.to raise_error(Faraday::ConnectionFailed)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
36
54
|
describe '#close' do
|
37
55
|
context "with app that doesn't support \#close" do
|
38
56
|
it 'should issue warning' do
|
@@ -49,4 +67,147 @@ RSpec.describe Faraday::Middleware do
|
|
49
67
|
end
|
50
68
|
end
|
51
69
|
end
|
70
|
+
|
71
|
+
describe '::default_options' do
|
72
|
+
let(:subclass_no_options) { FaradayMiddlewareSubclasses::SubclassNoOptions }
|
73
|
+
let(:subclass_one_option) { FaradayMiddlewareSubclasses::SubclassOneOption }
|
74
|
+
let(:subclass_two_options) { FaradayMiddlewareSubclasses::SubclassTwoOptions }
|
75
|
+
|
76
|
+
def build_conn(resp_middleware)
|
77
|
+
Faraday.new do |c|
|
78
|
+
c.adapter :test do |stub|
|
79
|
+
stub.get('/success') { [200, {}, 'ok'] }
|
80
|
+
end
|
81
|
+
c.response resp_middleware
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
RSpec.shared_context 'reset @default_options' do
|
86
|
+
before(:each) do
|
87
|
+
FaradayMiddlewareSubclasses::SubclassNoOptions.instance_variable_set(:@default_options, nil)
|
88
|
+
FaradayMiddlewareSubclasses::SubclassOneOption.instance_variable_set(:@default_options, nil)
|
89
|
+
FaradayMiddlewareSubclasses::SubclassTwoOptions.instance_variable_set(:@default_options, nil)
|
90
|
+
Faraday::Middleware.instance_variable_set(:@default_options, nil)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
after(:all) do
|
95
|
+
FaradayMiddlewareSubclasses::SubclassNoOptions.instance_variable_set(:@default_options, nil)
|
96
|
+
FaradayMiddlewareSubclasses::SubclassOneOption.instance_variable_set(:@default_options, nil)
|
97
|
+
FaradayMiddlewareSubclasses::SubclassTwoOptions.instance_variable_set(:@default_options, nil)
|
98
|
+
Faraday::Middleware.instance_variable_set(:@default_options, nil)
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'with subclass DEFAULT_OPTIONS defined' do
|
102
|
+
include_context 'reset @default_options'
|
103
|
+
|
104
|
+
context 'and without application options configured' do
|
105
|
+
let(:resp1) { build_conn(:one_option).get('/success') }
|
106
|
+
|
107
|
+
it 'has only subclass defaults' do
|
108
|
+
expect(Faraday::Middleware.default_options).to eq(Faraday::Middleware::DEFAULT_OPTIONS)
|
109
|
+
expect(subclass_no_options.default_options).to eq(subclass_no_options::DEFAULT_OPTIONS)
|
110
|
+
expect(subclass_one_option.default_options).to eq(subclass_one_option::DEFAULT_OPTIONS)
|
111
|
+
expect(subclass_two_options.default_options).to eq(subclass_two_options::DEFAULT_OPTIONS)
|
112
|
+
end
|
113
|
+
|
114
|
+
it { expect(resp1.body).to eq('ok') }
|
115
|
+
end
|
116
|
+
|
117
|
+
context "and with one application's options changed" do
|
118
|
+
let(:resp2) { build_conn(:two_options).get('/success') }
|
119
|
+
|
120
|
+
before(:each) do
|
121
|
+
FaradayMiddlewareSubclasses::SubclassTwoOptions.default_options = { some_option: false }
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'only updates default options of target subclass' do
|
125
|
+
expect(Faraday::Middleware.default_options).to eq(Faraday::Middleware::DEFAULT_OPTIONS)
|
126
|
+
expect(subclass_no_options.default_options).to eq(subclass_no_options::DEFAULT_OPTIONS)
|
127
|
+
expect(subclass_one_option.default_options).to eq(subclass_one_option::DEFAULT_OPTIONS)
|
128
|
+
expect(subclass_two_options.default_options).to eq({ some_option: false, some_other_option: false })
|
129
|
+
end
|
130
|
+
|
131
|
+
it { expect(resp2.body).to eq('ok') }
|
132
|
+
end
|
133
|
+
|
134
|
+
context "and with two applications' options changed" do
|
135
|
+
let(:resp1) { build_conn(:one_option).get('/success') }
|
136
|
+
let(:resp2) { build_conn(:two_options).get('/success') }
|
137
|
+
|
138
|
+
before(:each) do
|
139
|
+
FaradayMiddlewareSubclasses::SubclassOneOption.default_options = { some_other_option: true }
|
140
|
+
FaradayMiddlewareSubclasses::SubclassTwoOptions.default_options = { some_option: false }
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'updates subclasses and parent independent of each other' do
|
144
|
+
expect(Faraday::Middleware.default_options).to eq(Faraday::Middleware::DEFAULT_OPTIONS)
|
145
|
+
expect(subclass_no_options.default_options).to eq(subclass_no_options::DEFAULT_OPTIONS)
|
146
|
+
expect(subclass_one_option.default_options).to eq({ some_other_option: true })
|
147
|
+
expect(subclass_two_options.default_options).to eq({ some_option: false, some_other_option: false })
|
148
|
+
end
|
149
|
+
|
150
|
+
it { expect(resp1.body).to eq('ok') }
|
151
|
+
it { expect(resp2.body).to eq('ok') }
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'with FARADAY::MIDDLEWARE DEFAULT_OPTIONS and with Subclass DEFAULT_OPTIONS' do
|
156
|
+
before(:each) do
|
157
|
+
stub_const('Faraday::Middleware::DEFAULT_OPTIONS', { its_magic: false })
|
158
|
+
end
|
159
|
+
|
160
|
+
# Must stub Faraday::Middleware::DEFAULT_OPTIONS before resetting default options
|
161
|
+
include_context 'reset @default_options'
|
162
|
+
|
163
|
+
context 'and without application options configured' do
|
164
|
+
let(:resp1) { build_conn(:one_option).get('/success') }
|
165
|
+
|
166
|
+
it 'has only subclass defaults' do
|
167
|
+
expect(Faraday::Middleware.default_options).to eq(Faraday::Middleware::DEFAULT_OPTIONS)
|
168
|
+
expect(FaradayMiddlewareSubclasses::SubclassNoOptions.default_options).to eq({ its_magic: false })
|
169
|
+
expect(FaradayMiddlewareSubclasses::SubclassOneOption.default_options).to eq({ its_magic: false, some_other_option: false })
|
170
|
+
expect(FaradayMiddlewareSubclasses::SubclassTwoOptions.default_options).to eq({ its_magic: false, some_option: true, some_other_option: false })
|
171
|
+
end
|
172
|
+
|
173
|
+
it { expect(resp1.body).to eq('ok') }
|
174
|
+
end
|
175
|
+
|
176
|
+
context "and with two applications' options changed" do
|
177
|
+
let(:resp1) { build_conn(:one_option).get('/success') }
|
178
|
+
let(:resp2) { build_conn(:two_options).get('/success') }
|
179
|
+
|
180
|
+
before(:each) do
|
181
|
+
FaradayMiddlewareSubclasses::SubclassOneOption.default_options = { some_other_option: true }
|
182
|
+
FaradayMiddlewareSubclasses::SubclassTwoOptions.default_options = { some_option: false }
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'updates subclasses and parent independent of each other' do
|
186
|
+
expect(Faraday::Middleware.default_options).to eq(Faraday::Middleware::DEFAULT_OPTIONS)
|
187
|
+
expect(FaradayMiddlewareSubclasses::SubclassNoOptions.default_options).to eq({ its_magic: false })
|
188
|
+
expect(FaradayMiddlewareSubclasses::SubclassOneOption.default_options).to eq({ its_magic: false, some_other_option: true })
|
189
|
+
expect(FaradayMiddlewareSubclasses::SubclassTwoOptions.default_options).to eq({ its_magic: false, some_option: false, some_other_option: false })
|
190
|
+
end
|
191
|
+
|
192
|
+
it { expect(resp1.body).to eq('ok') }
|
193
|
+
it { expect(resp2.body).to eq('ok') }
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe 'default_options input validation' do
|
198
|
+
include_context 'reset @default_options'
|
199
|
+
|
200
|
+
it 'raises error if Faraday::Middleware option does not exist' do
|
201
|
+
expect { Faraday::Middleware.default_options = { something_special: true } }.to raise_error(Faraday::InitializationError) do |e|
|
202
|
+
expect(e.message).to eq('Invalid options provided. Keys not found in Faraday::Middleware::DEFAULT_OPTIONS: something_special')
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'raises error if subclass option does not exist' do
|
207
|
+
expect { subclass_one_option.default_options = { this_is_a_typo: true } }.to raise_error(Faraday::InitializationError) do |e|
|
208
|
+
expect(e.message).to eq('Invalid options provided. Keys not found in FaradayMiddlewareSubclasses::SubclassOneOption::DEFAULT_OPTIONS: this_is_a_typo')
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
52
213
|
end
|
@@ -27,14 +27,20 @@ RSpec.describe Faraday::Env do
|
|
27
27
|
expect(ssl.fetch(:verify, true)).to be_falsey
|
28
28
|
end
|
29
29
|
|
30
|
+
it 'handle verify_hostname when fetching' do
|
31
|
+
ssl = Faraday::SSLOptions.new
|
32
|
+
ssl.verify_hostname = true
|
33
|
+
expect(ssl.fetch(:verify_hostname, false)).to be_truthy
|
34
|
+
end
|
35
|
+
|
30
36
|
it 'retains custom members' do
|
31
37
|
env[:foo] = 'custom 1'
|
32
|
-
env[:bar] = :
|
38
|
+
env[:bar] = :custom2
|
33
39
|
env2 = Faraday::Env.from(env)
|
34
40
|
env2[:baz] = 'custom 3'
|
35
41
|
|
36
42
|
expect(env2[:foo]).to eq('custom 1')
|
37
|
-
expect(env2[:bar]).to eq(:
|
43
|
+
expect(env2[:bar]).to eq(:custom2)
|
38
44
|
expect(env[:baz]).to be_nil
|
39
45
|
end
|
40
46
|
|
@@ -32,6 +32,14 @@ RSpec.describe Faraday::ProxyOptions do
|
|
32
32
|
expect(proxy.user).to be_nil
|
33
33
|
expect(proxy.password).to be_nil
|
34
34
|
end
|
35
|
+
|
36
|
+
it 'treats empty string as nil' do
|
37
|
+
proxy = nil
|
38
|
+
proxy_string = proxy.to_s # => empty string
|
39
|
+
options = Faraday::ProxyOptions.from proxy_string
|
40
|
+
expect(options).to be_a_kind_of(Faraday::ProxyOptions)
|
41
|
+
expect(options.inspect).to eq('#<Faraday::ProxyOptions (empty)>')
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
45
|
it 'allows hash access' do
|
@@ -62,7 +62,8 @@ RSpec.describe Faraday::NestedParamsEncoder do
|
|
62
62
|
it 'encodes rack compat' do
|
63
63
|
params = { a: [{ one: '1', two: '2' }, '3', ''] }
|
64
64
|
result = Faraday::Utils.unescape(Faraday::NestedParamsEncoder.encode(params)).split('&')
|
65
|
-
|
65
|
+
escaped = Rack::Utils.build_nested_query(params)
|
66
|
+
expected = Rack::Utils.unescape(escaped).split('&')
|
66
67
|
expect(result).to match_array(expected)
|
67
68
|
end
|
68
69
|
|
@@ -102,6 +103,14 @@ RSpec.describe Faraday::NestedParamsEncoder do
|
|
102
103
|
Faraday::NestedParamsEncoder.sort_params = true
|
103
104
|
end
|
104
105
|
|
106
|
+
it 'encodes arrays indices when asked' do
|
107
|
+
params = { a: [0, 1, 2] }
|
108
|
+
expect(subject.encode(params)).to eq('a%5B%5D=0&a%5B%5D=1&a%5B%5D=2')
|
109
|
+
Faraday::NestedParamsEncoder.array_indices = true
|
110
|
+
expect(subject.encode(params)).to eq('a%5B0%5D=0&a%5B1%5D=1&a%5B2%5D=2')
|
111
|
+
Faraday::NestedParamsEncoder.array_indices = false
|
112
|
+
end
|
113
|
+
|
105
114
|
shared_examples 'a wrong decoding' do
|
106
115
|
it do
|
107
116
|
expect { subject.decode(query) }.to raise_error(TypeError) do |e|
|
@@ -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])
|
@@ -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
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
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
|
-
|
157
|
+
let(:cat) { subject.adapter.build }
|
189
158
|
|
190
|
-
it '
|
191
|
-
|
192
|
-
|
193
|
-
|
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
|
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
|
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::
|
226
|
+
Class.new(Faraday::Middleware) do
|
255
227
|
attr_accessor :name
|
256
228
|
|
257
229
|
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,73 @@ RSpec.describe Faraday::Request::Authorization do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe '
|
48
|
-
let(:auth_type) { :
|
47
|
+
describe 'authorization' do
|
48
|
+
let(:auth_type) { :Bearer }
|
49
49
|
|
50
|
-
context 'when passed
|
51
|
-
let(:auth_config) { '
|
50
|
+
context 'when passed a string' do
|
51
|
+
let(:auth_config) { ['custom'] }
|
52
52
|
|
53
|
-
it { expect(response.body).to eq('
|
53
|
+
it { expect(response.body).to eq('Bearer custom') }
|
54
54
|
|
55
55
|
include_examples 'does not interfere with existing authentication'
|
56
56
|
end
|
57
57
|
|
58
|
-
context 'when
|
59
|
-
let(:auth_config) { [
|
58
|
+
context 'when passed a proc' do
|
59
|
+
let(:auth_config) { [-> { 'custom_from_proc' }] }
|
60
60
|
|
61
|
-
it { expect(response.body).to
|
62
|
-
it { expect(response.body).to match(/token="baz"/) }
|
63
|
-
it { expect(response.body).to match(/foo="42"/) }
|
61
|
+
it { expect(response.body).to eq('Bearer custom_from_proc') }
|
64
62
|
|
65
63
|
include_examples 'does not interfere with existing authentication'
|
66
64
|
end
|
67
|
-
end
|
68
|
-
|
69
|
-
describe 'authorization' do
|
70
|
-
let(:auth_type) { :authorization }
|
71
65
|
|
72
|
-
context 'when passed
|
73
|
-
let(:
|
66
|
+
context 'when passed a callable' do
|
67
|
+
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
|
68
|
+
let(:auth_config) { [callable] }
|
74
69
|
|
75
|
-
it { expect(response.body).to eq('
|
70
|
+
it { expect(response.body).to eq('Bearer custom_from_callable') }
|
76
71
|
|
77
72
|
include_examples 'does not interfere with existing authentication'
|
78
73
|
end
|
79
74
|
|
80
|
-
context '
|
81
|
-
let(:
|
75
|
+
context 'with an argument' do
|
76
|
+
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
|
82
77
|
|
83
|
-
|
78
|
+
context 'when passed a proc' do
|
79
|
+
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
|
84
80
|
|
85
|
-
|
81
|
+
it { expect(response.body).to eq('Bearer proc crunchy surprise') }
|
82
|
+
|
83
|
+
include_examples 'does not interfere with existing authentication'
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when passed a lambda' do
|
87
|
+
let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }
|
88
|
+
|
89
|
+
it { expect(response.body).to eq('Bearer lambda crunchy surprise') }
|
90
|
+
|
91
|
+
include_examples 'does not interfere with existing authentication'
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when passed a callable with an argument' do
|
95
|
+
let(:callable) do
|
96
|
+
Class.new do
|
97
|
+
def call(env)
|
98
|
+
"callable #{env.request_headers['middle']}"
|
99
|
+
end
|
100
|
+
end.new
|
101
|
+
end
|
102
|
+
let(:auth_config) { [callable] }
|
103
|
+
|
104
|
+
it { expect(response.body).to eq('Bearer callable crunchy surprise') }
|
105
|
+
|
106
|
+
include_examples 'does not interfere with existing authentication'
|
107
|
+
end
|
86
108
|
end
|
87
109
|
|
88
|
-
context 'when passed
|
89
|
-
let(:auth_config) { [
|
110
|
+
context 'when passed too many arguments' do
|
111
|
+
let(:auth_config) { %w[baz foo] }
|
90
112
|
|
91
|
-
it { expect
|
113
|
+
it { expect { response }.to raise_error(ArgumentError) }
|
92
114
|
|
93
115
|
include_examples 'does not interfere with existing authentication'
|
94
116
|
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
|
@@ -73,6 +73,30 @@ RSpec.describe Faraday::Request::Json do
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
+
context 'true body' do
|
77
|
+
let(:result) { process(true) }
|
78
|
+
|
79
|
+
it 'encodes body' do
|
80
|
+
expect(result_body).to eq('true')
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'adds content type' do
|
84
|
+
expect(result_type).to eq('application/json')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'false body' do
|
89
|
+
let(:result) { process(false) }
|
90
|
+
|
91
|
+
it 'encodes body' do
|
92
|
+
expect(result_body).to eq('false')
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'adds content type' do
|
96
|
+
expect(result_type).to eq('application/json')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
76
100
|
context 'object body with json type' do
|
77
101
|
let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
|
78
102
|
|
@@ -108,4 +132,68 @@ RSpec.describe Faraday::Request::Json do
|
|
108
132
|
expect(result_type).to eq('application/xml; charset=utf-8')
|
109
133
|
end
|
110
134
|
end
|
135
|
+
|
136
|
+
context 'with encoder' do
|
137
|
+
let(:encoder) do
|
138
|
+
double('Encoder').tap do |e|
|
139
|
+
allow(e).to receive(:dump) { |s, opts| JSON.generate(s, opts) }
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
let(:result) { process(a: 1) }
|
144
|
+
|
145
|
+
context 'when encoder is passed as object' do
|
146
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }, { encoder: encoder }) }
|
147
|
+
|
148
|
+
it 'calls specified JSON encoder\'s dump method' do
|
149
|
+
expect(encoder).to receive(:dump).with({ a: 1 })
|
150
|
+
|
151
|
+
result
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'encodes body' do
|
155
|
+
expect(result_body).to eq('{"a":1}')
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'adds content type' do
|
159
|
+
expect(result_type).to eq('application/json')
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when encoder is passed as an object-method pair' do
|
164
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }, { encoder: [encoder, :dump] }) }
|
165
|
+
|
166
|
+
it 'calls specified JSON encoder' do
|
167
|
+
expect(encoder).to receive(:dump).with({ a: 1 })
|
168
|
+
|
169
|
+
result
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'encodes body' do
|
173
|
+
expect(result_body).to eq('{"a":1}')
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'adds content type' do
|
177
|
+
expect(result_type).to eq('application/json')
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'when encoder is not passed' do
|
182
|
+
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
183
|
+
|
184
|
+
it 'calls JSON.generate' do
|
185
|
+
expect(JSON).to receive(:generate).with({ a: 1 })
|
186
|
+
|
187
|
+
result
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'encodes body' do
|
191
|
+
expect(result_body).to eq('{"a":1}')
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'adds content type' do
|
195
|
+
expect(result_type).to eq('application/json')
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
111
199
|
end
|