faraday 0.13.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +496 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -328
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +127 -68
  9. data/lib/faraday/adapter.rb +71 -22
  10. data/lib/faraday/adapter_registry.rb +30 -0
  11. data/lib/faraday/connection.rb +314 -226
  12. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  13. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  14. data/lib/faraday/error.rb +121 -37
  15. data/lib/faraday/logging/formatter.rb +106 -0
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +18 -25
  18. data/lib/faraday/middleware_registry.rb +65 -0
  19. data/lib/faraday/options/connection_options.rb +22 -0
  20. data/lib/faraday/options/env.rb +181 -0
  21. data/lib/faraday/options/proxy_options.rb +32 -0
  22. data/lib/faraday/options/request_options.rb +22 -0
  23. data/lib/faraday/options/ssl_options.rb +59 -0
  24. data/lib/faraday/options.rb +41 -195
  25. data/lib/faraday/parameters.rb +4 -196
  26. data/lib/faraday/rack_builder.rb +91 -74
  27. data/lib/faraday/request/authorization.rb +37 -29
  28. data/lib/faraday/request/instrumentation.rb +47 -27
  29. data/lib/faraday/request/json.rb +55 -0
  30. data/lib/faraday/request/url_encoded.rb +45 -23
  31. data/lib/faraday/request.rb +74 -32
  32. data/lib/faraday/response/json.rb +54 -0
  33. data/lib/faraday/response/logger.rb +22 -69
  34. data/lib/faraday/response/raise_error.rb +57 -14
  35. data/lib/faraday/response.rb +26 -33
  36. data/lib/faraday/utils/headers.rb +139 -0
  37. data/lib/faraday/utils/params_hash.rb +61 -0
  38. data/lib/faraday/utils.rb +47 -251
  39. data/lib/faraday/version.rb +5 -0
  40. data/lib/faraday.rb +104 -197
  41. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  42. data/spec/faraday/adapter/test_spec.rb +377 -0
  43. data/spec/faraday/adapter_registry_spec.rb +28 -0
  44. data/spec/faraday/adapter_spec.rb +55 -0
  45. data/spec/faraday/connection_spec.rb +787 -0
  46. data/spec/faraday/error_spec.rb +60 -0
  47. data/spec/faraday/middleware_spec.rb +52 -0
  48. data/spec/faraday/options/env_spec.rb +70 -0
  49. data/spec/faraday/options/options_spec.rb +297 -0
  50. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  51. data/spec/faraday/options/request_options_spec.rb +19 -0
  52. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  53. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  54. data/spec/faraday/rack_builder_spec.rb +302 -0
  55. data/spec/faraday/request/authorization_spec.rb +83 -0
  56. data/spec/faraday/request/instrumentation_spec.rb +74 -0
  57. data/spec/faraday/request/json_spec.rb +111 -0
  58. data/spec/faraday/request/url_encoded_spec.rb +82 -0
  59. data/spec/faraday/request_spec.rb +109 -0
  60. data/spec/faraday/response/json_spec.rb +117 -0
  61. data/spec/faraday/response/logger_spec.rb +220 -0
  62. data/spec/faraday/response/raise_error_spec.rb +172 -0
  63. data/spec/faraday/response_spec.rb +75 -0
  64. data/spec/faraday/utils/headers_spec.rb +82 -0
  65. data/spec/faraday/utils_spec.rb +117 -0
  66. data/spec/faraday_spec.rb +37 -0
  67. data/spec/spec_helper.rb +132 -0
  68. data/spec/support/disabling_stub.rb +14 -0
  69. data/spec/support/fake_safe_buffer.rb +15 -0
  70. data/spec/support/helper_methods.rb +96 -0
  71. data/spec/support/shared_examples/adapter.rb +104 -0
  72. data/spec/support/shared_examples/params_encoder.rb +18 -0
  73. data/spec/support/shared_examples/request_method.rb +249 -0
  74. data/spec/support/streaming_response_checker.rb +35 -0
  75. metadata +71 -34
  76. data/lib/faraday/adapter/em_http.rb +0 -243
  77. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  78. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  79. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  80. data/lib/faraday/adapter/excon.rb +0 -80
  81. data/lib/faraday/adapter/httpclient.rb +0 -128
  82. data/lib/faraday/adapter/net_http.rb +0 -135
  83. data/lib/faraday/adapter/net_http_persistent.rb +0 -54
  84. data/lib/faraday/adapter/patron.rb +0 -83
  85. data/lib/faraday/adapter/rack.rb +0 -58
  86. data/lib/faraday/adapter/typhoeus.rb +0 -123
  87. data/lib/faraday/autoload.rb +0 -84
  88. data/lib/faraday/request/basic_authentication.rb +0 -13
  89. data/lib/faraday/request/multipart.rb +0 -68
  90. data/lib/faraday/request/retry.rb +0 -164
  91. data/lib/faraday/request/token_authentication.rb +0 -15
  92. data/lib/faraday/upload_io.rb +0 -67
@@ -0,0 +1,142 @@
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
+ shared_examples 'a wrong decoding' do
106
+ it do
107
+ expect { subject.decode(query) }.to raise_error(TypeError) do |e|
108
+ expect(e.message).to eq(error_message)
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'when expecting hash but getting string' do
114
+ let(:query) { 'a=1&a[b]=2' }
115
+ let(:error_message) { "expected Hash (got String) for param `a'" }
116
+ it_behaves_like 'a wrong decoding'
117
+ end
118
+
119
+ context 'when expecting hash but getting array' do
120
+ let(:query) { 'a[]=1&a[b]=2' }
121
+ let(:error_message) { "expected Hash (got Array) for param `a'" }
122
+ it_behaves_like 'a wrong decoding'
123
+ end
124
+
125
+ context 'when expecting nested hash but getting non nested' do
126
+ let(:query) { 'a[b]=1&a[b][c]=2' }
127
+ let(:error_message) { "expected Hash (got String) for param `b'" }
128
+ it_behaves_like 'a wrong decoding'
129
+ end
130
+
131
+ context 'when expecting array but getting hash' do
132
+ let(:query) { 'a[b]=1&a[]=2' }
133
+ let(:error_message) { "expected Array (got Hash) for param `a'" }
134
+ it_behaves_like 'a wrong decoding'
135
+ end
136
+
137
+ context 'when expecting array but getting string' do
138
+ let(:query) { 'a=1&a[]=2' }
139
+ let(:error_message) { "expected Array (got String) for param `a'" }
140
+ it_behaves_like 'a wrong decoding'
141
+ end
142
+ end
@@ -0,0 +1,302 @@
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 'with custom registered middleware' do
114
+ let(:conn) { Faraday::Connection.new {} }
115
+
116
+ after { Faraday::Middleware.unregister_middleware(:apple) }
117
+
118
+ it 'allows to register with constant' do
119
+ Faraday::Middleware.register_middleware(apple: Apple)
120
+ subject.use(:apple)
121
+ expect(subject.handlers).to eq([Apple])
122
+ end
123
+ end
124
+
125
+ context 'when having two handlers' do
126
+ let(:conn) { Faraday::Connection.new {} }
127
+
128
+ before do
129
+ subject.use(Apple)
130
+ subject.use(Orange)
131
+ end
132
+
133
+ it 'allows insert_before' do
134
+ subject.insert_before(Orange, Banana)
135
+ expect(subject.handlers).to eq([Apple, Banana, Orange])
136
+ end
137
+
138
+ it 'allows insert_after' do
139
+ subject.insert_after(Apple, Banana)
140
+ expect(subject.handlers).to eq([Apple, Banana, Orange])
141
+ end
142
+
143
+ it 'allows to swap handlers' do
144
+ subject.swap(Apple, Banana)
145
+ expect(subject.handlers).to eq([Banana, Orange])
146
+ end
147
+
148
+ it 'allows to delete a handler' do
149
+ subject.delete(Apple)
150
+ expect(subject.handlers).to eq([Orange])
151
+ end
152
+ end
153
+
154
+ context 'when middleware is added with named arguments' do
155
+ let(:conn) { Faraday::Connection.new {} }
156
+
157
+ let(:dog_middleware) do
158
+ Class.new(Faraday::Middleware) do
159
+ attr_accessor :name
160
+
161
+ def initialize(app, name:)
162
+ super(app)
163
+ @name = name
164
+ end
165
+ end
166
+ end
167
+ let(:dog) do
168
+ subject.handlers.find { |handler| handler == dog_middleware }.build
169
+ end
170
+
171
+ it 'adds a handler to construct middleware with options passed to use' do
172
+ subject.use dog_middleware, name: 'Rex'
173
+ expect { dog }.to_not output(
174
+ /warning: Using the last argument as keyword parameters is deprecated/
175
+ ).to_stderr
176
+ expect(dog.name).to eq('Rex')
177
+ end
178
+ end
179
+
180
+ context 'when a middleware is added with named arguments' do
181
+ let(:conn) { Faraday::Connection.new {} }
182
+
183
+ let(:cat_request) do
184
+ Class.new(Faraday::Middleware) do
185
+ attr_accessor :name
186
+
187
+ def initialize(app, name:)
188
+ super(app)
189
+ @name = name
190
+ end
191
+ end
192
+ end
193
+ let(:cat) do
194
+ subject.handlers.find { |handler| handler == cat_request }.build
195
+ end
196
+
197
+ it 'adds a handler to construct request adapter with options passed to request' do
198
+ Faraday::Request.register_middleware cat_request: cat_request
199
+ subject.request :cat_request, name: 'Felix'
200
+ expect { cat }.to_not output(
201
+ /warning: Using the last argument as keyword parameters is deprecated/
202
+ ).to_stderr
203
+ expect(cat.name).to eq('Felix')
204
+ end
205
+ end
206
+
207
+ context 'when a middleware is added with named arguments' do
208
+ let(:conn) { Faraday::Connection.new {} }
209
+
210
+ let(:fish_response) do
211
+ Class.new(Faraday::Middleware) do
212
+ attr_accessor :name
213
+
214
+ def initialize(app, name:)
215
+ super(app)
216
+ @name = name
217
+ end
218
+ end
219
+ end
220
+ let(:fish) do
221
+ subject.handlers.find { |handler| handler == fish_response }.build
222
+ end
223
+
224
+ it 'adds a handler to construct response adapter with options passed to response' do
225
+ Faraday::Response.register_middleware fish_response: fish_response
226
+ subject.response :fish_response, name: 'Bubbles'
227
+ expect { fish }.to_not output(
228
+ /warning: Using the last argument as keyword parameters is deprecated/
229
+ ).to_stderr
230
+ expect(fish.name).to eq('Bubbles')
231
+ end
232
+ end
233
+
234
+ context 'when a plain adapter is added with named arguments' do
235
+ let(:conn) { Faraday::Connection.new {} }
236
+
237
+ let(:rabbit_adapter) do
238
+ Class.new(Faraday::Adapter) do
239
+ attr_accessor :name
240
+
241
+ def initialize(app, name:)
242
+ super(app)
243
+ @name = name
244
+ end
245
+ end
246
+ end
247
+ let(:rabbit) do
248
+ subject.adapter.build
249
+ end
250
+
251
+ it 'adds a handler to construct adapter with options passed to adapter' do
252
+ Faraday::Adapter.register_middleware rabbit_adapter: rabbit_adapter
253
+ subject.adapter :rabbit_adapter, name: 'Thumper'
254
+ expect { rabbit }.to_not output(
255
+ /warning: Using the last argument as keyword parameters is deprecated/
256
+ ).to_stderr
257
+ expect(rabbit.name).to eq('Thumper')
258
+ end
259
+ end
260
+
261
+ context 'when handlers are directly added or updated' do
262
+ let(:conn) { Faraday::Connection.new {} }
263
+
264
+ let(:rock_handler) do
265
+ Class.new do
266
+ attr_accessor :name
267
+
268
+ def initialize(_app, name:)
269
+ @name = name
270
+ end
271
+ end
272
+ end
273
+ let(:rock) do
274
+ subject.handlers.find { |handler| handler == rock_handler }.build
275
+ end
276
+
277
+ it 'adds a handler to construct adapter with options passed to insert' do
278
+ subject.insert 0, rock_handler, name: 'Stony'
279
+ expect { rock }.to_not output(
280
+ /warning: Using the last argument as keyword parameters is deprecated/
281
+ ).to_stderr
282
+ expect(rock.name).to eq('Stony')
283
+ end
284
+
285
+ it 'adds a handler with options passed to insert_after' do
286
+ subject.insert_after 0, rock_handler, name: 'Rocky'
287
+ expect { rock }.to_not output(
288
+ /warning: Using the last argument as keyword parameters is deprecated/
289
+ ).to_stderr
290
+ expect(rock.name).to eq('Rocky')
291
+ end
292
+
293
+ it 'adds a handler with options passed to swap' do
294
+ subject.insert 0, rock_handler, name: 'Flint'
295
+ subject.swap 0, rock_handler, name: 'Chert'
296
+ expect { rock }.to_not output(
297
+ /warning: Using the last argument as keyword parameters is deprecated/
298
+ ).to_stderr
299
+ expect(rock.name).to eq('Chert')
300
+ end
301
+ end
302
+ end
@@ -0,0 +1,83 @@
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 'when passed too many arguments' do
76
+ let(:auth_config) { %w[baz foo] }
77
+
78
+ it { expect { response }.to raise_error(ArgumentError) }
79
+
80
+ include_examples 'does not interfere with existing authentication'
81
+ end
82
+ end
83
+ 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
@@ -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