faraday 2.15.0 → 2.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +11 -5
  3. data/lib/faraday/connection.rb +13 -6
  4. data/lib/faraday/encoders/nested_params_encoder.rb +1 -1
  5. data/lib/faraday/error.rb +29 -2
  6. data/lib/faraday/logging/formatter.rb +1 -1
  7. data/lib/faraday/options.rb +2 -1
  8. data/lib/faraday/rack_builder.rb +3 -0
  9. data/lib/faraday/response/raise_error.rb +7 -1
  10. data/lib/faraday/response.rb +5 -2
  11. data/lib/faraday/utils/headers.rb +41 -7
  12. data/lib/faraday/utils.rb +15 -3
  13. data/lib/faraday/version.rb +1 -1
  14. data/spec/external_adapters/faraday_specs_setup.rb +2 -0
  15. data/spec/support/integration/test_pki.rb +77 -0
  16. data/spec/support/integration/tls_server.rb +92 -0
  17. data/spec/support/integration_helper.rb +71 -0
  18. metadata +5 -29
  19. data/spec/faraday/adapter/test_spec.rb +0 -460
  20. data/spec/faraday/adapter_registry_spec.rb +0 -28
  21. data/spec/faraday/adapter_spec.rb +0 -55
  22. data/spec/faraday/connection_spec.rb +0 -897
  23. data/spec/faraday/error_spec.rb +0 -175
  24. data/spec/faraday/middleware_registry_spec.rb +0 -31
  25. data/spec/faraday/middleware_spec.rb +0 -213
  26. data/spec/faraday/options/env_spec.rb +0 -76
  27. data/spec/faraday/options/options_spec.rb +0 -331
  28. data/spec/faraday/options/proxy_options_spec.rb +0 -85
  29. data/spec/faraday/options/request_options_spec.rb +0 -19
  30. data/spec/faraday/params_encoders/flat_spec.rb +0 -42
  31. data/spec/faraday/params_encoders/nested_spec.rb +0 -179
  32. data/spec/faraday/rack_builder_spec.rb +0 -323
  33. data/spec/faraday/request/authorization_spec.rb +0 -118
  34. data/spec/faraday/request/instrumentation_spec.rb +0 -74
  35. data/spec/faraday/request/json_spec.rb +0 -199
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -93
  37. data/spec/faraday/request_spec.rb +0 -119
  38. data/spec/faraday/response/json_spec.rb +0 -206
  39. data/spec/faraday/response/logger_spec.rb +0 -299
  40. data/spec/faraday/response/raise_error_spec.rb +0 -286
  41. data/spec/faraday/response_spec.rb +0 -84
  42. data/spec/faraday/utils/headers_spec.rb +0 -109
  43. data/spec/faraday/utils_spec.rb +0 -120
  44. data/spec/faraday_spec.rb +0 -69
  45. data/spec/spec_helper.rb +0 -142
@@ -1,323 +0,0 @@
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 'rejects adapter changes after locking the stack' do
71
- conn.get('/')
72
- expect { subject.adapter(:net_http) }.to raise_error(Faraday::RackBuilder::StackLocked)
73
- expect(subject.adapter).to eq(Faraday::Adapter.lookup_middleware(:test))
74
- end
75
-
76
- it 'dup stack is unlocked' do
77
- expect(subject.locked?).to be_falsey
78
- subject.lock!
79
- expect(subject.locked?).to be_truthy
80
- dup = subject.dup
81
- expect(dup).to eq(subject)
82
- expect(dup.locked?).to be_falsey
83
- end
84
-
85
- it 'allows to compare handlers' do
86
- expect(subject.handlers.first).to eq(Faraday::RackBuilder::Handler.new(Apple))
87
- end
88
- end
89
-
90
- context 'when having a single handler' do
91
- let(:conn) { Faraday::Connection.new {} }
92
-
93
- before { subject.use(Apple) }
94
-
95
- it { expect(subject.handlers).to eq([Apple]) }
96
-
97
- it 'allows use' do
98
- subject.use(Orange)
99
- expect(subject.handlers).to eq([Apple, Orange])
100
- end
101
-
102
- it 'allows insert_before' do
103
- subject.insert_before(Apple, Orange)
104
- expect(subject.handlers).to eq([Orange, Apple])
105
- end
106
-
107
- it 'allows insert_after' do
108
- subject.insert_after(Apple, Orange)
109
- expect(subject.handlers).to eq([Apple, Orange])
110
- end
111
-
112
- it 'raises an error trying to use an unregistered symbol' do
113
- expect { subject.use(:apple) }.to raise_error(Faraday::Error) do |err|
114
- expect(err.message).to eq(':apple is not registered on Faraday::Middleware')
115
- end
116
- end
117
- end
118
-
119
- context 'when having two handlers' do
120
- let(:conn) { Faraday::Connection.new {} }
121
-
122
- before do
123
- subject.use(Apple)
124
- subject.use(Orange)
125
- end
126
-
127
- it 'allows insert_before' do
128
- subject.insert_before(Orange, Banana)
129
- expect(subject.handlers).to eq([Apple, Banana, Orange])
130
- end
131
-
132
- it 'allows insert_after' do
133
- subject.insert_after(Apple, Banana)
134
- expect(subject.handlers).to eq([Apple, Banana, Orange])
135
- end
136
-
137
- it 'allows to swap handlers' do
138
- subject.swap(Apple, Banana)
139
- expect(subject.handlers).to eq([Banana, Orange])
140
- end
141
-
142
- it 'allows to delete a handler' do
143
- subject.delete(Apple)
144
- expect(subject.handlers).to eq([Orange])
145
- end
146
- end
147
-
148
- context 'when adapter is added with named options' do
149
- after { Faraday.default_adapter_options = {} }
150
- let(:conn) { Faraday::Connection.new {} }
151
-
152
- let(:cat_adapter) do
153
- Class.new(Faraday::Adapter) do
154
- attr_accessor :name
155
-
156
- def initialize(app, name:)
157
- super(app)
158
- @name = name
159
- end
160
- end
161
- end
162
-
163
- let(:cat) { subject.adapter.build }
164
-
165
- it 'adds a handler to construct adapter with named options' do
166
- Faraday.default_adapter = cat_adapter
167
- Faraday.default_adapter_options = { name: 'Chloe' }
168
- expect { cat }.to_not output(
169
- /warning: Using the last argument as keyword parameters is deprecated/
170
- ).to_stderr
171
- expect(cat.name).to eq 'Chloe'
172
- end
173
- end
174
-
175
- context 'when middleware is added with named arguments' do
176
- let(:conn) { Faraday::Connection.new {} }
177
-
178
- let(:dog_middleware) do
179
- Class.new(Faraday::Middleware) do
180
- attr_accessor :name
181
-
182
- def initialize(app, name:)
183
- super(app)
184
- @name = name
185
- end
186
- end
187
- end
188
- let(:dog) do
189
- subject.handlers.find { |handler| handler == dog_middleware }.build
190
- end
191
-
192
- it 'adds a handler to construct middleware with options passed to use' do
193
- subject.use dog_middleware, name: 'Rex'
194
- expect { dog }.to_not output(
195
- /warning: Using the last argument as keyword parameters is deprecated/
196
- ).to_stderr
197
- expect(dog.name).to eq('Rex')
198
- end
199
- end
200
-
201
- context 'when a middleware is added with named arguments' do
202
- let(:conn) { Faraday::Connection.new {} }
203
-
204
- let(:cat_request) do
205
- Class.new(Faraday::Middleware) do
206
- attr_accessor :name
207
-
208
- def initialize(app, name:)
209
- super(app)
210
- @name = name
211
- end
212
- end
213
- end
214
- let(:cat) do
215
- subject.handlers.find { |handler| handler == cat_request }.build
216
- end
217
-
218
- it 'adds a handler to construct request adapter with options passed to request' do
219
- Faraday::Request.register_middleware cat_request: cat_request
220
- subject.request :cat_request, name: 'Felix'
221
- expect { cat }.to_not output(
222
- /warning: Using the last argument as keyword parameters is deprecated/
223
- ).to_stderr
224
- expect(cat.name).to eq('Felix')
225
- end
226
- end
227
-
228
- context 'when a middleware is added with named arguments' do
229
- let(:conn) { Faraday::Connection.new {} }
230
-
231
- let(:fish_response) do
232
- Class.new(Faraday::Middleware) do
233
- attr_accessor :name
234
-
235
- def initialize(app, name:)
236
- super(app)
237
- @name = name
238
- end
239
- end
240
- end
241
- let(:fish) do
242
- subject.handlers.find { |handler| handler == fish_response }.build
243
- end
244
-
245
- it 'adds a handler to construct response adapter with options passed to response' do
246
- Faraday::Response.register_middleware fish_response: fish_response
247
- subject.response :fish_response, name: 'Bubbles'
248
- expect { fish }.to_not output(
249
- /warning: Using the last argument as keyword parameters is deprecated/
250
- ).to_stderr
251
- expect(fish.name).to eq('Bubbles')
252
- end
253
- end
254
-
255
- context 'when a plain adapter is added with named arguments' do
256
- let(:conn) { Faraday::Connection.new {} }
257
-
258
- let(:rabbit_adapter) do
259
- Class.new(Faraday::Adapter) do
260
- attr_accessor :name
261
-
262
- def initialize(app, name:)
263
- super(app)
264
- @name = name
265
- end
266
- end
267
- end
268
- let(:rabbit) do
269
- subject.adapter.build
270
- end
271
-
272
- it 'adds a handler to construct adapter with options passed to adapter' do
273
- Faraday::Adapter.register_middleware rabbit_adapter: rabbit_adapter
274
- subject.adapter :rabbit_adapter, name: 'Thumper'
275
- expect { rabbit }.to_not output(
276
- /warning: Using the last argument as keyword parameters is deprecated/
277
- ).to_stderr
278
- expect(rabbit.name).to eq('Thumper')
279
- end
280
- end
281
-
282
- context 'when handlers are directly added or updated' do
283
- let(:conn) { Faraday::Connection.new {} }
284
-
285
- let(:rock_handler) do
286
- Class.new do
287
- attr_accessor :name
288
-
289
- def initialize(_app, name:)
290
- @name = name
291
- end
292
- end
293
- end
294
- let(:rock) do
295
- subject.handlers.find { |handler| handler == rock_handler }.build
296
- end
297
-
298
- it 'adds a handler to construct adapter with options passed to insert' do
299
- subject.insert 0, rock_handler, name: 'Stony'
300
- expect { rock }.to_not output(
301
- /warning: Using the last argument as keyword parameters is deprecated/
302
- ).to_stderr
303
- expect(rock.name).to eq('Stony')
304
- end
305
-
306
- it 'adds a handler with options passed to insert_after' do
307
- subject.insert_after 0, rock_handler, name: 'Rocky'
308
- expect { rock }.to_not output(
309
- /warning: Using the last argument as keyword parameters is deprecated/
310
- ).to_stderr
311
- expect(rock.name).to eq('Rocky')
312
- end
313
-
314
- it 'adds a handler with options passed to swap' do
315
- subject.insert 0, rock_handler, name: 'Flint'
316
- subject.swap 0, rock_handler, name: 'Chert'
317
- expect { rock }.to_not output(
318
- /warning: Using the last argument as keyword parameters is deprecated/
319
- ).to_stderr
320
- expect(rock.name).to eq('Chert')
321
- end
322
- end
323
- end
@@ -1,118 +0,0 @@
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
@@ -1,74 +0,0 @@
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
@@ -1,199 +0,0 @@
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 '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
-
100
- context 'object body with json type' do
101
- let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
102
-
103
- it 'encodes 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/json; charset=utf-8')
109
- end
110
- end
111
-
112
- context 'object body with vendor json type' do
113
- let(:result) { process({ a: 1 }, 'application/vnd.myapp.v1+json; charset=utf-8') }
114
-
115
- it 'encodes body' do
116
- expect(result_body).to eq('{"a":1}')
117
- end
118
-
119
- it "doesn't change content type" do
120
- expect(result_type).to eq('application/vnd.myapp.v1+json; charset=utf-8')
121
- end
122
- end
123
-
124
- context 'object body with incompatible type' do
125
- let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
126
-
127
- it "doesn't change body" do
128
- expect(result_body).to eq(a: 1)
129
- end
130
-
131
- it "doesn't change content type" do
132
- expect(result_type).to eq('application/xml; charset=utf-8')
133
- end
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
199
- end