faraday 1.4.1 → 2.9.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 +4 -4
  2. data/CHANGELOG.md +197 -3
  3. data/LICENSE.md +1 -1
  4. data/README.md +34 -20
  5. data/Rakefile +3 -1
  6. data/examples/client_spec.rb +67 -13
  7. data/examples/client_test.rb +80 -15
  8. data/lib/faraday/adapter/test.rb +117 -52
  9. data/lib/faraday/adapter.rb +5 -14
  10. data/lib/faraday/connection.rb +70 -130
  11. data/lib/faraday/encoders/nested_params_encoder.rb +14 -7
  12. data/lib/faraday/error.rb +20 -11
  13. data/lib/faraday/logging/formatter.rb +28 -15
  14. data/lib/faraday/middleware.rb +3 -1
  15. data/lib/faraday/middleware_registry.rb +17 -63
  16. data/lib/faraday/options/connection_options.rb +7 -6
  17. data/lib/faraday/options/env.rb +85 -62
  18. data/lib/faraday/options/proxy_options.rb +11 -3
  19. data/lib/faraday/options/request_options.rb +7 -6
  20. data/lib/faraday/options/ssl_options.rb +56 -45
  21. data/lib/faraday/options.rb +7 -6
  22. data/lib/faraday/rack_builder.rb +23 -21
  23. data/lib/faraday/request/authorization.rb +37 -38
  24. data/lib/faraday/request/instrumentation.rb +5 -1
  25. data/lib/faraday/request/json.rb +70 -0
  26. data/lib/faraday/request/url_encoded.rb +5 -1
  27. data/lib/faraday/request.rb +20 -37
  28. data/lib/faraday/response/json.rb +73 -0
  29. data/lib/faraday/response/logger.rb +8 -4
  30. data/lib/faraday/response/raise_error.rb +33 -6
  31. data/lib/faraday/response.rb +10 -20
  32. data/lib/faraday/utils/headers.rb +7 -2
  33. data/lib/faraday/utils.rb +11 -7
  34. data/lib/faraday/version.rb +1 -1
  35. data/lib/faraday.rb +10 -31
  36. data/spec/faraday/adapter/test_spec.rb +182 -0
  37. data/spec/faraday/connection_spec.rb +177 -90
  38. data/spec/faraday/error_spec.rb +31 -6
  39. data/spec/faraday/middleware_registry_spec.rb +31 -0
  40. data/spec/faraday/middleware_spec.rb +18 -0
  41. data/spec/faraday/options/env_spec.rb +8 -2
  42. data/spec/faraday/options/options_spec.rb +1 -1
  43. data/spec/faraday/options/proxy_options_spec.rb +15 -0
  44. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  45. data/spec/faraday/rack_builder_spec.rb +26 -54
  46. data/spec/faraday/request/authorization_spec.rb +54 -24
  47. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  48. data/spec/faraday/request/json_spec.rb +199 -0
  49. data/spec/faraday/request/url_encoded_spec.rb +12 -2
  50. data/spec/faraday/request_spec.rb +5 -15
  51. data/spec/faraday/response/json_spec.rb +189 -0
  52. data/spec/faraday/response/logger_spec.rb +38 -0
  53. data/spec/faraday/response/raise_error_spec.rb +47 -5
  54. data/spec/faraday/response_spec.rb +3 -1
  55. data/spec/faraday/utils/headers_spec.rb +22 -4
  56. data/spec/faraday/utils_spec.rb +63 -1
  57. data/spec/faraday_spec.rb +8 -4
  58. data/spec/spec_helper.rb +6 -5
  59. data/spec/support/fake_safe_buffer.rb +1 -1
  60. data/spec/support/helper_methods.rb +0 -37
  61. data/spec/support/shared_examples/adapter.rb +2 -2
  62. data/spec/support/shared_examples/request_method.rb +22 -21
  63. metadata +14 -94
  64. data/lib/faraday/adapter/em_http.rb +0 -289
  65. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  66. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  67. data/lib/faraday/adapter/em_synchrony.rb +0 -153
  68. data/lib/faraday/adapter/httpclient.rb +0 -152
  69. data/lib/faraday/adapter/patron.rb +0 -132
  70. data/lib/faraday/adapter/rack.rb +0 -75
  71. data/lib/faraday/adapter/typhoeus.rb +0 -15
  72. data/lib/faraday/autoload.rb +0 -92
  73. data/lib/faraday/dependency_loader.rb +0 -37
  74. data/lib/faraday/file_part.rb +0 -128
  75. data/lib/faraday/param_part.rb +0 -53
  76. data/lib/faraday/request/basic_authentication.rb +0 -20
  77. data/lib/faraday/request/multipart.rb +0 -106
  78. data/lib/faraday/request/retry.rb +0 -239
  79. data/lib/faraday/request/token_authentication.rb +0 -20
  80. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  81. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  82. data/spec/faraday/adapter/excon_spec.rb +0 -49
  83. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  84. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  85. data/spec/faraday/adapter/patron_spec.rb +0 -18
  86. data/spec/faraday/adapter/rack_spec.rb +0 -8
  87. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  88. data/spec/faraday/composite_read_io_spec.rb +0 -80
  89. data/spec/faraday/request/multipart_spec.rb +0 -302
  90. data/spec/faraday/request/retry_spec.rb +0 -242
  91. data/spec/faraday/response/middleware_spec.rb +0 -68
  92. 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
@@ -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] = :custom_2
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(:custom_2)
43
+ expect(env2[:bar]).to eq(:custom2)
38
44
  expect(env[:baz]).to be_nil
39
45
  end
40
46
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  RSpec.describe Faraday::Options do
4
4
  SubOptions = Class.new(Faraday::Options.new(:sub_a, :sub_b))
5
- class ParentOptions < Faraday::Options.new(:a, :b, :c)
5
+ ParentOptions = Faraday::Options.new(:a, :b, :c) do
6
6
  options c: SubOptions
7
7
  end
8
8
 
@@ -14,6 +14,13 @@ RSpec.describe Faraday::ProxyOptions do
14
14
  expect(options.inspect).to match('#<Faraday::ProxyOptions uri=')
15
15
  end
16
16
 
17
+ it 'defaults to http' do
18
+ options = Faraday::ProxyOptions.from 'example.org'
19
+ expect(options.port).to eq(80)
20
+ expect(options.host).to eq('example.org')
21
+ expect(options.scheme).to eq('http')
22
+ end
23
+
17
24
  it 'works with nil' do
18
25
  options = Faraday::ProxyOptions.from nil
19
26
  expect(options).to be_a_kind_of(Faraday::ProxyOptions)
@@ -25,6 +32,14 @@ RSpec.describe Faraday::ProxyOptions do
25
32
  expect(proxy.user).to be_nil
26
33
  expect(proxy.password).to be_nil
27
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
28
43
  end
29
44
 
30
45
  it 'allows hash access' do
@@ -102,6 +102,14 @@ RSpec.describe Faraday::NestedParamsEncoder do
102
102
  Faraday::NestedParamsEncoder.sort_params = true
103
103
  end
104
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
+
105
113
  shared_examples 'a wrong decoding' do
106
114
  it do
107
115
  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 Broken < Faraday::Middleware
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 having a handler with broken dependency' do
180
- let(:conn) do
181
- Faraday::Connection.new do |builder|
182
- builder.adapter :test do |stub|
183
- stub.get('/') { |_| [200, {}, ''] }
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
- before { subject.use(Broken) }
157
+ let(:cat) { subject.adapter.build }
189
158
 
190
- it 'raises an error while making a request' do
191
- expect { conn.get('/') }.to raise_error(RuntimeError) do |err|
192
- expect(err.message).to match(%r{missing dependency for Broken: .+ -- zomg/i_dont/exist})
193
- end
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 request adapter is added with named arguments' do
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 response adapter is added with named arguments' do
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::Response::Middleware) do
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: 'Token token="bar"') }
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('Token token="bar"')
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) { :basic_auth }
28
+ let(:auth_type) { :basic }
29
29
 
30
30
  context 'when passed correct params' do
31
31
  let(:auth_config) { %w[aladdin opensesame] }
@@ -44,43 +44,73 @@ RSpec.describe Faraday::Request::Authorization do
44
44
  end
45
45
  end
46
46
 
47
- describe 'token_auth' do
48
- let(:auth_type) { :token_auth }
47
+ describe 'authorization' do
48
+ let(:auth_type) { :Bearer }
49
49
 
50
- context 'when passed correct params' do
51
- let(:auth_config) { 'quux' }
50
+ context 'when passed a string' do
51
+ let(:auth_config) { ['custom'] }
52
52
 
53
- it { expect(response.body).to eq('Token token="quux"') }
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 other values are provided' do
59
- let(:auth_config) { ['baz', { foo: 42 }] }
58
+ context 'when passed a proc' do
59
+ let(:auth_config) { [-> { 'custom_from_proc' }] }
60
60
 
61
- it { expect(response.body).to match(/^Token /) }
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 two strings' do
73
- let(:auth_config) { ['custom', 'abc def'] }
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('custom abc def') }
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 'when passed a string and a hash' do
81
- let(:auth_config) { ['baz', { foo: 42 }] }
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] }
82
112
 
83
- it { expect(response.body).to eq('baz foo="42"') }
113
+ it { expect { response }.to raise_error(ArgumentError) }
84
114
 
85
115
  include_examples 'does not interfere with existing authentication'
86
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
- begin
34
- res = options.instrumenter
35
- rescue NameError => e
36
- expect(e.to_s).to match('ActiveSupport')
37
- else
38
- expect(res).to eq(ActiveSupport::Notifications)
39
- end
33
+ res = options.instrumenter
34
+ rescue NameError => e
35
+ expect(e.to_s).to match('ActiveSupport')
36
+ else
37
+ expect(res).to eq(ActiveSupport::Notifications)
40
38
  end
41
39
 
42
40
  it 'instruments with default name' do
@@ -0,0 +1,199 @@
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
@@ -1,14 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'stringio'
4
+
3
5
  RSpec.describe Faraday::Request::UrlEncoded do
4
6
  let(:conn) do
5
7
  Faraday.new do |b|
6
- b.request :multipart
7
8
  b.request :url_encoded
8
9
  b.adapter :test do |stub|
9
10
  stub.post('/echo') do |env|
10
11
  posted_as = env[:request_headers]['Content-Type']
11
- [200, { 'Content-Type' => posted_as }, env[:body]]
12
+ body = env[:body]
13
+ if body.respond_to?(:read)
14
+ body = body.read
15
+ end
16
+ [200, { 'Content-Type' => posted_as }, body]
12
17
  end
13
18
  end
14
19
  end
@@ -68,6 +73,11 @@ RSpec.describe Faraday::Request::UrlEncoded do
68
73
  expect(response.body).to eq('a%5Bb%5D%5Bc%5D%5B%5D=d')
69
74
  end
70
75
 
76
+ it 'works with files' do
77
+ response = conn.post('/echo', StringIO.new('str=apple'))
78
+ expect(response.body).to eq('str=apple')
79
+ end
80
+
71
81
  context 'customising default_space_encoding' do
72
82
  around do |example|
73
83
  Faraday::Utils.default_space_encoding = '%20'