faraday 0.17.3 → 2.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +350 -8
- data/LICENSE.md +1 -1
- data/README.md +27 -367
- data/Rakefile +1 -7
- data/examples/client_spec.rb +119 -0
- data/examples/client_test.rb +144 -0
- data/lib/faraday/adapter/test.rb +170 -72
- data/lib/faraday/adapter.rb +69 -23
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +309 -233
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/lib/faraday/error.rb +31 -42
- data/lib/faraday/logging/formatter.rb +122 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +21 -25
- data/lib/faraday/middleware_registry.rb +83 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +205 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +69 -0
- data/lib/faraday/options.rb +38 -193
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +91 -76
- data/lib/faraday/request/authorization.rb +42 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +48 -24
- data/lib/faraday/request.rb +64 -42
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +24 -67
- data/lib/faraday/response/raise_error.rb +57 -18
- data/lib/faraday/response.rb +26 -32
- data/lib/faraday/utils/headers.rb +144 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -251
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +108 -198
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +442 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +808 -0
- data/spec/faraday/error_spec.rb +12 -54
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +70 -0
- data/spec/faraday/options/env_spec.rb +76 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +150 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +118 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/spec/faraday/request_spec.rb +110 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +248 -0
- data/spec/faraday/response/raise_error_spec.rb +81 -15
- data/spec/faraday/response_spec.rb +77 -0
- data/spec/faraday/utils/headers_spec.rb +100 -0
- data/spec/faraday/utils_spec.rb +118 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -36
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +263 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +76 -59
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -82
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -152
- data/lib/faraday/adapter/net_http_persistent.rb +0 -68
- data/lib/faraday/adapter/patron.rb +0 -95
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -12
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/deprecate.rb +0 -107
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -213
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -0,0 +1,150 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack/utils'
|
4
|
+
|
5
|
+
RSpec.describe Faraday::NestedParamsEncoder do
|
6
|
+
it_behaves_like 'a params encoder'
|
7
|
+
|
8
|
+
it 'decodes arrays' do
|
9
|
+
query = 'a[1]=one&a[2]=two&a[3]=three'
|
10
|
+
expected = { 'a' => %w[one two three] }
|
11
|
+
expect(subject.decode(query)).to eq(expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'decodes hashes' do
|
15
|
+
query = 'a[b1]=one&a[b2]=two&a[b][c]=foo'
|
16
|
+
expected = { 'a' => { 'b1' => 'one', 'b2' => 'two', 'b' => { 'c' => 'foo' } } }
|
17
|
+
expect(subject.decode(query)).to eq(expected)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'decodes nested arrays rack compat' do
|
21
|
+
query = 'a[][one]=1&a[][two]=2&a[][one]=3&a[][two]=4'
|
22
|
+
expected = Rack::Utils.parse_nested_query(query)
|
23
|
+
expect(subject.decode(query)).to eq(expected)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'decodes nested array mixed types' do
|
27
|
+
query = 'a[][one]=1&a[]=2&a[]=&a[]'
|
28
|
+
expected = Rack::Utils.parse_nested_query(query)
|
29
|
+
expect(subject.decode(query)).to eq(expected)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'decodes nested ignores invalid array' do
|
33
|
+
query = '[][a]=1&b=2'
|
34
|
+
expected = { 'a' => '1', 'b' => '2' }
|
35
|
+
expect(subject.decode(query)).to eq(expected)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'decodes nested ignores repeated array notation' do
|
39
|
+
query = 'a[][][]=1'
|
40
|
+
expected = { 'a' => ['1'] }
|
41
|
+
expect(subject.decode(query)).to eq(expected)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'decodes nested ignores malformed keys' do
|
45
|
+
query = '=1&[]=2'
|
46
|
+
expected = {}
|
47
|
+
expect(subject.decode(query)).to eq(expected)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'decodes nested subkeys dont have to be in brackets' do
|
51
|
+
query = 'a[b]c[d]e=1'
|
52
|
+
expected = { 'a' => { 'b' => { 'c' => { 'd' => { 'e' => '1' } } } } }
|
53
|
+
expect(subject.decode(query)).to eq(expected)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'decodes nested final value overrides any type' do
|
57
|
+
query = 'a[b][c]=1&a[b]=2'
|
58
|
+
expected = { 'a' => { 'b' => '2' } }
|
59
|
+
expect(subject.decode(query)).to eq(expected)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'encodes rack compat' do
|
63
|
+
params = { a: [{ one: '1', two: '2' }, '3', ''] }
|
64
|
+
result = Faraday::Utils.unescape(Faraday::NestedParamsEncoder.encode(params)).split('&')
|
65
|
+
expected = Rack::Utils.build_nested_query(params).split('&')
|
66
|
+
expect(result).to match_array(expected)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'encodes empty string array value' do
|
70
|
+
expected = 'baz=&foo%5Bbar%5D='
|
71
|
+
result = Faraday::NestedParamsEncoder.encode(foo: { bar: '' }, baz: '')
|
72
|
+
expect(result).to eq(expected)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'encodes nil array value' do
|
76
|
+
expected = 'baz&foo%5Bbar%5D'
|
77
|
+
result = Faraday::NestedParamsEncoder.encode(foo: { bar: nil }, baz: nil)
|
78
|
+
expect(result).to eq(expected)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'encodes empty array value' do
|
82
|
+
expected = 'baz%5B%5D&foo%5Bbar%5D%5B%5D'
|
83
|
+
result = Faraday::NestedParamsEncoder.encode(foo: { bar: [] }, baz: [])
|
84
|
+
expect(result).to eq(expected)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'encodes boolean values' do
|
88
|
+
params = { a: true, b: false }
|
89
|
+
expect(subject.encode(params)).to eq('a=true&b=false')
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'encodes boolean values in array' do
|
93
|
+
params = { a: [true, false] }
|
94
|
+
expect(subject.encode(params)).to eq('a%5B%5D=true&a%5B%5D=false')
|
95
|
+
end
|
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
|
+
|
105
|
+
it 'encodes arrays indices when asked' do
|
106
|
+
params = { a: [0, 1, 2] }
|
107
|
+
expect(subject.encode(params)).to eq('a%5B%5D=0&a%5B%5D=1&a%5B%5D=2')
|
108
|
+
Faraday::NestedParamsEncoder.array_indices = true
|
109
|
+
expect(subject.encode(params)).to eq('a%5B0%5D=0&a%5B1%5D=1&a%5B2%5D=2')
|
110
|
+
Faraday::NestedParamsEncoder.array_indices = false
|
111
|
+
end
|
112
|
+
|
113
|
+
shared_examples 'a wrong decoding' do
|
114
|
+
it do
|
115
|
+
expect { subject.decode(query) }.to raise_error(TypeError) do |e|
|
116
|
+
expect(e.message).to eq(error_message)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when expecting hash but getting string' do
|
122
|
+
let(:query) { 'a=1&a[b]=2' }
|
123
|
+
let(:error_message) { "expected Hash (got String) for param `a'" }
|
124
|
+
it_behaves_like 'a wrong decoding'
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when expecting hash but getting array' do
|
128
|
+
let(:query) { 'a[]=1&a[b]=2' }
|
129
|
+
let(:error_message) { "expected Hash (got Array) for param `a'" }
|
130
|
+
it_behaves_like 'a wrong decoding'
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'when expecting nested hash but getting non nested' do
|
134
|
+
let(:query) { 'a[b]=1&a[b][c]=2' }
|
135
|
+
let(:error_message) { "expected Hash (got String) for param `b'" }
|
136
|
+
it_behaves_like 'a wrong decoding'
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when expecting array but getting hash' do
|
140
|
+
let(:query) { 'a[b]=1&a[]=2' }
|
141
|
+
let(:error_message) { "expected Array (got Hash) for param `a'" }
|
142
|
+
it_behaves_like 'a wrong decoding'
|
143
|
+
end
|
144
|
+
|
145
|
+
context 'when expecting array but getting string' do
|
146
|
+
let(:query) { 'a=1&a[]=2' }
|
147
|
+
let(:error_message) { "expected Array (got String) for param `a'" }
|
148
|
+
it_behaves_like 'a wrong decoding'
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,317 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::RackBuilder do
|
4
|
+
# mock handler classes
|
5
|
+
(Handler = Struct.new(:app)).class_eval do
|
6
|
+
def call(env)
|
7
|
+
env[:request_headers]['X-Middleware'] ||= ''
|
8
|
+
env[:request_headers]['X-Middleware'] += ":#{self.class.name.split('::').last}"
|
9
|
+
app.call(env)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Apple < Handler
|
14
|
+
end
|
15
|
+
|
16
|
+
class Orange < Handler
|
17
|
+
end
|
18
|
+
|
19
|
+
class Banana < Handler
|
20
|
+
end
|
21
|
+
|
22
|
+
subject { conn.builder }
|
23
|
+
before { Faraday.default_adapter = :test }
|
24
|
+
after { Faraday.default_adapter = nil }
|
25
|
+
|
26
|
+
context 'with default stack' do
|
27
|
+
let(:conn) { Faraday::Connection.new }
|
28
|
+
|
29
|
+
it { expect(subject[0]).to eq(Faraday::Request.lookup_middleware(:url_encoded)) }
|
30
|
+
it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(Faraday.default_adapter)) }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with custom empty block' do
|
34
|
+
let(:conn) { Faraday::Connection.new {} }
|
35
|
+
|
36
|
+
it { expect(subject[0]).to be_nil }
|
37
|
+
it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(Faraday.default_adapter)) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with custom adapter only' do
|
41
|
+
let(:conn) do
|
42
|
+
Faraday::Connection.new do |builder|
|
43
|
+
builder.adapter :test do |stub|
|
44
|
+
stub.get('/') { |_| [200, {}, ''] }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it { expect(subject[0]).to be_nil }
|
50
|
+
it { expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(:test)) }
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with custom handler and adapter' do
|
54
|
+
let(:conn) do
|
55
|
+
Faraday::Connection.new do |builder|
|
56
|
+
builder.use Apple
|
57
|
+
builder.adapter :test do |stub|
|
58
|
+
stub.get('/') { |_| [200, {}, ''] }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'locks the stack after making a request' do
|
64
|
+
expect(subject.locked?).to be_falsey
|
65
|
+
conn.get('/')
|
66
|
+
expect(subject.locked?).to be_truthy
|
67
|
+
expect { subject.use(Orange) }.to raise_error(Faraday::RackBuilder::StackLocked)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'dup stack is unlocked' do
|
71
|
+
expect(subject.locked?).to be_falsey
|
72
|
+
subject.lock!
|
73
|
+
expect(subject.locked?).to be_truthy
|
74
|
+
dup = subject.dup
|
75
|
+
expect(dup).to eq(subject)
|
76
|
+
expect(dup.locked?).to be_falsey
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'allows to compare handlers' do
|
80
|
+
expect(subject.handlers.first).to eq(Faraday::RackBuilder::Handler.new(Apple))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when having a single handler' do
|
85
|
+
let(:conn) { Faraday::Connection.new {} }
|
86
|
+
|
87
|
+
before { subject.use(Apple) }
|
88
|
+
|
89
|
+
it { expect(subject.handlers).to eq([Apple]) }
|
90
|
+
|
91
|
+
it 'allows use' do
|
92
|
+
subject.use(Orange)
|
93
|
+
expect(subject.handlers).to eq([Apple, Orange])
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'allows insert_before' do
|
97
|
+
subject.insert_before(Apple, Orange)
|
98
|
+
expect(subject.handlers).to eq([Orange, Apple])
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'allows insert_after' do
|
102
|
+
subject.insert_after(Apple, Orange)
|
103
|
+
expect(subject.handlers).to eq([Apple, Orange])
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'raises an error trying to use an unregistered symbol' do
|
107
|
+
expect { subject.use(:apple) }.to raise_error(Faraday::Error) do |err|
|
108
|
+
expect(err.message).to eq(':apple is not registered on Faraday::Middleware')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when having two handlers' do
|
114
|
+
let(:conn) { Faraday::Connection.new {} }
|
115
|
+
|
116
|
+
before do
|
117
|
+
subject.use(Apple)
|
118
|
+
subject.use(Orange)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'allows insert_before' do
|
122
|
+
subject.insert_before(Orange, Banana)
|
123
|
+
expect(subject.handlers).to eq([Apple, Banana, Orange])
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'allows insert_after' do
|
127
|
+
subject.insert_after(Apple, Banana)
|
128
|
+
expect(subject.handlers).to eq([Apple, Banana, Orange])
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'allows to swap handlers' do
|
132
|
+
subject.swap(Apple, Banana)
|
133
|
+
expect(subject.handlers).to eq([Banana, Orange])
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'allows to delete a handler' do
|
137
|
+
subject.delete(Apple)
|
138
|
+
expect(subject.handlers).to eq([Orange])
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
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
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
let(:cat) { subject.adapter.build }
|
158
|
+
|
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'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'when middleware is added with named arguments' do
|
170
|
+
let(:conn) { Faraday::Connection.new {} }
|
171
|
+
|
172
|
+
let(:dog_middleware) do
|
173
|
+
Class.new(Faraday::Middleware) do
|
174
|
+
attr_accessor :name
|
175
|
+
|
176
|
+
def initialize(app, name:)
|
177
|
+
super(app)
|
178
|
+
@name = name
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
let(:dog) do
|
183
|
+
subject.handlers.find { |handler| handler == dog_middleware }.build
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'adds a handler to construct middleware with options passed to use' do
|
187
|
+
subject.use dog_middleware, name: 'Rex'
|
188
|
+
expect { dog }.to_not output(
|
189
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
190
|
+
).to_stderr
|
191
|
+
expect(dog.name).to eq('Rex')
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'when a middleware is added with named arguments' do
|
196
|
+
let(:conn) { Faraday::Connection.new {} }
|
197
|
+
|
198
|
+
let(:cat_request) do
|
199
|
+
Class.new(Faraday::Middleware) do
|
200
|
+
attr_accessor :name
|
201
|
+
|
202
|
+
def initialize(app, name:)
|
203
|
+
super(app)
|
204
|
+
@name = name
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
let(:cat) do
|
209
|
+
subject.handlers.find { |handler| handler == cat_request }.build
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'adds a handler to construct request adapter with options passed to request' do
|
213
|
+
Faraday::Request.register_middleware cat_request: cat_request
|
214
|
+
subject.request :cat_request, name: 'Felix'
|
215
|
+
expect { cat }.to_not output(
|
216
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
217
|
+
).to_stderr
|
218
|
+
expect(cat.name).to eq('Felix')
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'when a middleware is added with named arguments' do
|
223
|
+
let(:conn) { Faraday::Connection.new {} }
|
224
|
+
|
225
|
+
let(:fish_response) do
|
226
|
+
Class.new(Faraday::Middleware) do
|
227
|
+
attr_accessor :name
|
228
|
+
|
229
|
+
def initialize(app, name:)
|
230
|
+
super(app)
|
231
|
+
@name = name
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
let(:fish) do
|
236
|
+
subject.handlers.find { |handler| handler == fish_response }.build
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'adds a handler to construct response adapter with options passed to response' do
|
240
|
+
Faraday::Response.register_middleware fish_response: fish_response
|
241
|
+
subject.response :fish_response, name: 'Bubbles'
|
242
|
+
expect { fish }.to_not output(
|
243
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
244
|
+
).to_stderr
|
245
|
+
expect(fish.name).to eq('Bubbles')
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context 'when a plain adapter is added with named arguments' do
|
250
|
+
let(:conn) { Faraday::Connection.new {} }
|
251
|
+
|
252
|
+
let(:rabbit_adapter) do
|
253
|
+
Class.new(Faraday::Adapter) do
|
254
|
+
attr_accessor :name
|
255
|
+
|
256
|
+
def initialize(app, name:)
|
257
|
+
super(app)
|
258
|
+
@name = name
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
let(:rabbit) do
|
263
|
+
subject.adapter.build
|
264
|
+
end
|
265
|
+
|
266
|
+
it 'adds a handler to construct adapter with options passed to adapter' do
|
267
|
+
Faraday::Adapter.register_middleware rabbit_adapter: rabbit_adapter
|
268
|
+
subject.adapter :rabbit_adapter, name: 'Thumper'
|
269
|
+
expect { rabbit }.to_not output(
|
270
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
271
|
+
).to_stderr
|
272
|
+
expect(rabbit.name).to eq('Thumper')
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
context 'when handlers are directly added or updated' do
|
277
|
+
let(:conn) { Faraday::Connection.new {} }
|
278
|
+
|
279
|
+
let(:rock_handler) do
|
280
|
+
Class.new do
|
281
|
+
attr_accessor :name
|
282
|
+
|
283
|
+
def initialize(_app, name:)
|
284
|
+
@name = name
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
let(:rock) do
|
289
|
+
subject.handlers.find { |handler| handler == rock_handler }.build
|
290
|
+
end
|
291
|
+
|
292
|
+
it 'adds a handler to construct adapter with options passed to insert' do
|
293
|
+
subject.insert 0, rock_handler, name: 'Stony'
|
294
|
+
expect { rock }.to_not output(
|
295
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
296
|
+
).to_stderr
|
297
|
+
expect(rock.name).to eq('Stony')
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'adds a handler with options passed to insert_after' do
|
301
|
+
subject.insert_after 0, rock_handler, name: 'Rocky'
|
302
|
+
expect { rock }.to_not output(
|
303
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
304
|
+
).to_stderr
|
305
|
+
expect(rock.name).to eq('Rocky')
|
306
|
+
end
|
307
|
+
|
308
|
+
it 'adds a handler with options passed to swap' do
|
309
|
+
subject.insert 0, rock_handler, name: 'Flint'
|
310
|
+
subject.swap 0, rock_handler, name: 'Chert'
|
311
|
+
expect { rock }.to_not output(
|
312
|
+
/warning: Using the last argument as keyword parameters is deprecated/
|
313
|
+
).to_stderr
|
314
|
+
expect(rock.name).to eq('Chert')
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Request::Authorization do
|
4
|
+
let(:conn) do
|
5
|
+
Faraday.new do |b|
|
6
|
+
b.request :authorization, auth_type, *auth_config
|
7
|
+
b.adapter :test do |stub|
|
8
|
+
stub.get('/auth-echo') do |env|
|
9
|
+
[200, {}, env[:request_headers]['Authorization']]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
shared_examples 'does not interfere with existing authentication' do
|
16
|
+
context 'and request already has an authentication header' do
|
17
|
+
let(:response) { conn.get('/auth-echo', nil, authorization: 'OAuth oauth_token') }
|
18
|
+
|
19
|
+
it 'does not interfere with existing authorization' do
|
20
|
+
expect(response.body).to eq('OAuth oauth_token')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:response) { conn.get('/auth-echo') }
|
26
|
+
|
27
|
+
describe 'basic_auth' do
|
28
|
+
let(:auth_type) { :basic }
|
29
|
+
|
30
|
+
context 'when passed correct params' do
|
31
|
+
let(:auth_config) { %w[aladdin opensesame] }
|
32
|
+
|
33
|
+
it { expect(response.body).to eq('Basic YWxhZGRpbjpvcGVuc2VzYW1l') }
|
34
|
+
|
35
|
+
include_examples 'does not interfere with existing authentication'
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when passed very long values' do
|
39
|
+
let(:auth_config) { ['A' * 255, ''] }
|
40
|
+
|
41
|
+
it { expect(response.body).to eq("Basic #{'QUFB' * 85}Og==") }
|
42
|
+
|
43
|
+
include_examples 'does not interfere with existing authentication'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'authorization' do
|
48
|
+
let(:auth_type) { :Bearer }
|
49
|
+
|
50
|
+
context 'when passed a string' do
|
51
|
+
let(:auth_config) { ['custom'] }
|
52
|
+
|
53
|
+
it { expect(response.body).to eq('Bearer custom') }
|
54
|
+
|
55
|
+
include_examples 'does not interfere with existing authentication'
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when passed a proc' do
|
59
|
+
let(:auth_config) { [-> { 'custom_from_proc' }] }
|
60
|
+
|
61
|
+
it { expect(response.body).to eq('Bearer custom_from_proc') }
|
62
|
+
|
63
|
+
include_examples 'does not interfere with existing authentication'
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when passed a callable' do
|
67
|
+
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
|
68
|
+
let(:auth_config) { [callable] }
|
69
|
+
|
70
|
+
it { expect(response.body).to eq('Bearer custom_from_callable') }
|
71
|
+
|
72
|
+
include_examples 'does not interfere with existing authentication'
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'with an argument' do
|
76
|
+
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
|
77
|
+
|
78
|
+
context 'when passed a proc' do
|
79
|
+
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
|
80
|
+
|
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
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'when passed too many arguments' do
|
111
|
+
let(:auth_config) { %w[baz foo] }
|
112
|
+
|
113
|
+
it { expect { response }.to raise_error(ArgumentError) }
|
114
|
+
|
115
|
+
include_examples 'does not interfere with existing authentication'
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Request::Instrumentation do
|
4
|
+
class FakeInstrumenter
|
5
|
+
attr_reader :instrumentations
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@instrumentations = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def instrument(name, env)
|
12
|
+
@instrumentations << [name, env]
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:config) { {} }
|
18
|
+
let(:options) { Faraday::Request::Instrumentation::Options.from config }
|
19
|
+
let(:instrumenter) { FakeInstrumenter.new }
|
20
|
+
let(:conn) do
|
21
|
+
Faraday.new do |f|
|
22
|
+
f.request :instrumentation, config.merge(instrumenter: instrumenter)
|
23
|
+
f.adapter :test do |stub|
|
24
|
+
stub.get '/' do
|
25
|
+
[200, {}, 'ok']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it { expect(options.name).to eq('request.faraday') }
|
32
|
+
it 'defaults to ActiveSupport::Notifications' do
|
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)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'instruments with default name' do
|
41
|
+
expect(instrumenter.instrumentations.size).to eq(0)
|
42
|
+
|
43
|
+
res = conn.get '/'
|
44
|
+
expect(res.body).to eq('ok')
|
45
|
+
expect(instrumenter.instrumentations.size).to eq(1)
|
46
|
+
|
47
|
+
name, env = instrumenter.instrumentations.first
|
48
|
+
expect(name).to eq('request.faraday')
|
49
|
+
expect(env[:url].path).to eq('/')
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with custom name' do
|
53
|
+
let(:config) { { name: 'custom' } }
|
54
|
+
|
55
|
+
it { expect(options.name).to eq('custom') }
|
56
|
+
it 'instruments with custom name' do
|
57
|
+
expect(instrumenter.instrumentations.size).to eq(0)
|
58
|
+
|
59
|
+
res = conn.get '/'
|
60
|
+
expect(res.body).to eq('ok')
|
61
|
+
expect(instrumenter.instrumentations.size).to eq(1)
|
62
|
+
|
63
|
+
name, env = instrumenter.instrumentations.first
|
64
|
+
expect(name).to eq('custom')
|
65
|
+
expect(env[:url].path).to eq('/')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'with custom instrumenter' do
|
70
|
+
let(:config) { { instrumenter: :custom } }
|
71
|
+
|
72
|
+
it { expect(options.instrumenter).to eq(:custom) }
|
73
|
+
end
|
74
|
+
end
|