faraday 0.17.0 → 0.17.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +232 -0
- data/README.md +11 -0
- data/Rakefile +13 -0
- data/lib/faraday/adapter/em_http.rb +9 -9
- data/lib/faraday/adapter/em_synchrony.rb +5 -5
- data/lib/faraday/adapter/excon.rb +3 -3
- data/lib/faraday/adapter/httpclient.rb +4 -4
- data/lib/faraday/adapter/net_http.rb +3 -2
- data/lib/faraday/adapter/net_http_persistent.rb +4 -4
- data/lib/faraday/adapter/patron.rb +5 -5
- data/lib/faraday/adapter/rack.rb +1 -1
- data/lib/faraday/deprecate.rb +109 -0
- data/lib/faraday/error.rb +127 -35
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +2 -2
- data/lib/faraday/request/retry.rb +6 -5
- data/lib/faraday/request.rb +2 -0
- data/lib/faraday/response/raise_error.rb +7 -3
- data/lib/faraday/response.rb +3 -3
- data/lib/faraday.rb +2 -3
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/spec_helper.rb +105 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +30 -0
- data/test/adapters/em_synchrony_test.rb +32 -0
- data/test/adapters/excon_test.rb +30 -0
- data/test/adapters/httpclient_test.rb +34 -0
- data/test/adapters/integration.rb +263 -0
- data/test/adapters/logger_test.rb +136 -0
- data/test/adapters/net_http_persistent_test.rb +114 -0
- data/test/adapters/net_http_test.rb +79 -0
- data/test/adapters/patron_test.rb +40 -0
- data/test/adapters/rack_test.rb +38 -0
- data/test/adapters/test_middleware_test.rb +157 -0
- data/test/adapters/typhoeus_test.rb +38 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +109 -0
- data/test/connection_test.rb +738 -0
- data/test/env_test.rb +268 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +67 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +282 -0
- data/test/middleware_stack_test.rb +260 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +333 -0
- data/test/parameters_test.rb +157 -0
- data/test/request_middleware_test.rb +126 -0
- data/test/response_middleware_test.rb +72 -0
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +98 -0
- metadata +48 -6
@@ -0,0 +1,333 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class OptionsTest < Faraday::TestCase
|
4
|
+
SubOptions = Class.new(Faraday::Options.new(:sub_a, :sub_b))
|
5
|
+
class ParentOptions < Faraday::Options.new(:a, :b, :c)
|
6
|
+
options :c => SubOptions
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_deep_merge
|
10
|
+
sub_opts1 = SubOptions.from(sub_a: 3)
|
11
|
+
sub_opts2 = SubOptions.from(sub_b: 4)
|
12
|
+
opt1 = ParentOptions.from(a: 1, c: sub_opts1)
|
13
|
+
opt2 = ParentOptions.from(b: 2, c: sub_opts2)
|
14
|
+
|
15
|
+
merged = opt1.merge(opt2)
|
16
|
+
|
17
|
+
expected_sub_opts = SubOptions.from(sub_a: 3, sub_b: 4)
|
18
|
+
assert_equal merged, ParentOptions.from(a: 1, b: 2, c: expected_sub_opts)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_deep_merge_with_hash
|
22
|
+
sub_opts1 = SubOptions.from(sub_a: 3)
|
23
|
+
sub_opts2 = { sub_b: 4 }
|
24
|
+
opt1 = ParentOptions.from(a: 1, c: sub_opts1)
|
25
|
+
opt2 = { b: 2, c: sub_opts2 }
|
26
|
+
|
27
|
+
merged = opt1.merge(opt2)
|
28
|
+
|
29
|
+
expected_sub_opts = SubOptions.from(sub_a: 3, sub_b: 4)
|
30
|
+
assert_equal merged, ParentOptions.from(a: 1, b: 2, c: expected_sub_opts)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_deep_merge_with_nil
|
34
|
+
sub_opts = SubOptions.new(3, 4)
|
35
|
+
options = ParentOptions.new(1, 2, sub_opts)
|
36
|
+
assert_equal options.a, 1
|
37
|
+
assert_equal options.b, 2
|
38
|
+
assert_equal options.c.sub_a, 3
|
39
|
+
assert_equal options.c.sub_b, 4
|
40
|
+
|
41
|
+
options2 = ParentOptions.from(b: 5, c: nil)
|
42
|
+
|
43
|
+
merged = options.merge(options2)
|
44
|
+
|
45
|
+
assert_equal merged.b, 5
|
46
|
+
assert_equal merged.c, sub_opts
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_deep_merge_with_sub_nil
|
50
|
+
options = ParentOptions.from(a: 1)
|
51
|
+
|
52
|
+
sub_opts = SubOptions.new(3, 4)
|
53
|
+
options2 = ParentOptions.from(b: 2, c: sub_opts)
|
54
|
+
|
55
|
+
assert_equal options.a, 1
|
56
|
+
assert_equal options2.b, 2
|
57
|
+
assert_equal options2.c.sub_a, 3
|
58
|
+
assert_equal options2.c.sub_b, 4
|
59
|
+
|
60
|
+
merged = options.merge(options2)
|
61
|
+
|
62
|
+
assert_equal merged.c, sub_opts
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_dup_is_shallow
|
66
|
+
sub_opts = SubOptions.from(sub_a: 3)
|
67
|
+
opts = ParentOptions.from(b: 1, c: sub_opts)
|
68
|
+
|
69
|
+
duped = opts.dup
|
70
|
+
duped.b = 2
|
71
|
+
duped.c.sub_a = 4
|
72
|
+
|
73
|
+
assert_equal opts.b, 1
|
74
|
+
assert_equal opts.c.sub_a, 4
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_deep_dup
|
78
|
+
sub_opts = SubOptions.from(sub_a: 3)
|
79
|
+
opts = ParentOptions.from(b: 1, c: sub_opts)
|
80
|
+
|
81
|
+
duped = opts.deep_dup
|
82
|
+
duped.b = 2
|
83
|
+
duped.c.sub_a = 4
|
84
|
+
|
85
|
+
assert_equal opts.b, 1
|
86
|
+
assert_equal opts.c.sub_a, 3
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_clear
|
90
|
+
options = SubOptions.new(1)
|
91
|
+
assert !options.empty?
|
92
|
+
assert options.clear
|
93
|
+
assert options.empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_empty
|
97
|
+
options = SubOptions.new
|
98
|
+
assert options.empty?
|
99
|
+
options.sub_a = 1
|
100
|
+
assert !options.empty?
|
101
|
+
options.delete(:sub_a)
|
102
|
+
assert options.empty?
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_each_key
|
106
|
+
options = ParentOptions.new(1, 2, 3)
|
107
|
+
enum = options.each_key
|
108
|
+
assert_equal enum.next.to_sym, :a
|
109
|
+
assert_equal enum.next.to_sym, :b
|
110
|
+
assert_equal enum.next.to_sym, :c
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_key?
|
114
|
+
options = SubOptions.new
|
115
|
+
assert !options.key?(:sub_a)
|
116
|
+
options.sub_a = 1
|
117
|
+
assert options.key?(:sub_a)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_each_value
|
121
|
+
options = ParentOptions.new(1, 2, 3)
|
122
|
+
enum = options.each_value
|
123
|
+
assert_equal enum.next, 1
|
124
|
+
assert_equal enum.next, 2
|
125
|
+
assert_equal enum.next, 3
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_value?
|
129
|
+
options = SubOptions.new
|
130
|
+
assert !options.value?(1)
|
131
|
+
options.sub_a = 1
|
132
|
+
assert options.value?(1)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_request_proxy_setter
|
136
|
+
options = Faraday::RequestOptions.new
|
137
|
+
assert_nil options.proxy
|
138
|
+
|
139
|
+
assert_raises NoMethodError do
|
140
|
+
options[:proxy] = {:booya => 1}
|
141
|
+
end
|
142
|
+
|
143
|
+
options[:proxy] = {:user => 'user'}
|
144
|
+
assert_kind_of Faraday::ProxyOptions, options.proxy
|
145
|
+
assert_equal 'user', options.proxy.user
|
146
|
+
|
147
|
+
options.proxy = nil
|
148
|
+
assert_nil options.proxy
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_proxy_options_from_string
|
152
|
+
options = Faraday::ProxyOptions.from 'http://user:pass@example.org'
|
153
|
+
assert_equal 'user', options.user
|
154
|
+
assert_equal 'pass', options.password
|
155
|
+
assert_kind_of URI, options.uri
|
156
|
+
assert_equal '', options.path
|
157
|
+
assert_equal 80, options.port
|
158
|
+
assert_equal 'example.org', options.host
|
159
|
+
assert_equal 'http', options.scheme
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_proxy_options_from_nil
|
163
|
+
options = Faraday::ProxyOptions.from nil
|
164
|
+
assert_kind_of Faraday::ProxyOptions, options
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_proxy_options_hash_access
|
168
|
+
proxy = Faraday::ProxyOptions.from 'http://a%40b:pw%20d@example.org'
|
169
|
+
assert_equal 'a@b', proxy[:user]
|
170
|
+
assert_equal 'a@b', proxy.user
|
171
|
+
assert_equal 'pw d', proxy[:password]
|
172
|
+
assert_equal 'pw d', proxy.password
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_proxy_options_no_auth
|
176
|
+
proxy = Faraday::ProxyOptions.from 'http://example.org'
|
177
|
+
assert_nil proxy.user
|
178
|
+
assert_nil proxy.password
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_from_options
|
182
|
+
options = ParentOptions.new(1)
|
183
|
+
|
184
|
+
value = ParentOptions.from(options)
|
185
|
+
assert_equal 1, value.a
|
186
|
+
assert_nil value.b
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_from_options_with_sub_object
|
190
|
+
sub = SubOptions.new(1)
|
191
|
+
options = ParentOptions.from :a => 1, :c => sub
|
192
|
+
assert_kind_of ParentOptions, options
|
193
|
+
assert_equal 1, options.a
|
194
|
+
assert_nil options.b
|
195
|
+
assert_kind_of SubOptions, options.c
|
196
|
+
assert_equal 1, options.c.sub_a
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_from_hash
|
200
|
+
options = ParentOptions.from :a => 1
|
201
|
+
assert_kind_of ParentOptions, options
|
202
|
+
assert_equal 1, options.a
|
203
|
+
assert_nil options.b
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_from_hash_with_sub_object
|
207
|
+
options = ParentOptions.from :a => 1, :c => {:sub_a => 1}
|
208
|
+
assert_kind_of ParentOptions, options
|
209
|
+
assert_equal 1, options.a
|
210
|
+
assert_nil options.b
|
211
|
+
assert_kind_of SubOptions, options.c
|
212
|
+
assert_equal 1, options.c.sub_a
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_inheritance
|
216
|
+
subclass = Class.new(ParentOptions)
|
217
|
+
options = subclass.from(:c => {:sub_a => 'hello'})
|
218
|
+
assert_kind_of SubOptions, options.c
|
219
|
+
assert_equal 'hello', options.c.sub_a
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_from_deep_hash
|
223
|
+
hash = {:b => 1}
|
224
|
+
options = ParentOptions.from :a => hash
|
225
|
+
assert_equal 1, options.a[:b]
|
226
|
+
|
227
|
+
hash[:b] = 2
|
228
|
+
assert_equal 1, options.a[:b]
|
229
|
+
|
230
|
+
options.a[:b] = 3
|
231
|
+
assert_equal 2, hash[:b]
|
232
|
+
assert_equal 3, options.a[:b]
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_from_nil
|
236
|
+
options = ParentOptions.from(nil)
|
237
|
+
assert_kind_of ParentOptions, options
|
238
|
+
assert_nil options.a
|
239
|
+
assert_nil options.b
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_invalid_key
|
243
|
+
assert_raises NoMethodError do
|
244
|
+
ParentOptions.from :invalid => 1
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_update
|
249
|
+
options = ParentOptions.new(1)
|
250
|
+
assert_equal 1, options.a
|
251
|
+
assert_nil options.b
|
252
|
+
|
253
|
+
updated = options.update :a => 2, :b => 3
|
254
|
+
assert_equal 2, options.a
|
255
|
+
assert_equal 3, options.b
|
256
|
+
assert_equal options, updated
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_delete
|
260
|
+
options = ParentOptions.new(1)
|
261
|
+
assert_equal 1, options.a
|
262
|
+
assert_equal 1, options.delete(:a)
|
263
|
+
assert_nil options.a
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_merge
|
267
|
+
options = ParentOptions.new(1)
|
268
|
+
assert_equal 1, options.a
|
269
|
+
assert_nil options.b
|
270
|
+
|
271
|
+
dup = options.merge :a => 2, :b => 3
|
272
|
+
assert_equal 2, dup.a
|
273
|
+
assert_equal 3, dup.b
|
274
|
+
assert_equal 1, options.a
|
275
|
+
assert_nil options.b
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_env_access_member
|
279
|
+
e = Faraday::Env.new
|
280
|
+
assert_nil e.method
|
281
|
+
e.method = :get
|
282
|
+
assert_equal :get, e.method
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_env_access_symbol_non_member
|
286
|
+
e = Faraday::Env.new
|
287
|
+
assert_nil e[:custom]
|
288
|
+
e[:custom] = :boom
|
289
|
+
assert_equal :boom, e[:custom]
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_env_access_string_non_member
|
293
|
+
e = Faraday::Env.new
|
294
|
+
assert_nil e["custom"]
|
295
|
+
e["custom"] = :boom
|
296
|
+
assert_equal :boom, e["custom"]
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_env_fetch_ignores_false
|
300
|
+
ssl = Faraday::SSLOptions.new
|
301
|
+
ssl.verify = false
|
302
|
+
assert !ssl.fetch(:verify, true)
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_fetch_grabs_value
|
306
|
+
opt = Faraday::SSLOptions.new
|
307
|
+
opt.verify = 1
|
308
|
+
assert_equal 1, opt.fetch(:verify, false) { |k| :blah }
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_fetch_uses_falsey_default
|
312
|
+
opt = Faraday::SSLOptions.new
|
313
|
+
assert_equal false, opt.fetch(:verify, false) { |k| :blah }
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_fetch_accepts_block
|
317
|
+
opt = Faraday::SSLOptions.new
|
318
|
+
assert_equal "yo :verify", opt.fetch(:verify) { |k| "yo #{k.inspect}"}
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_fetch_needs_a_default_if_key_is_missing
|
322
|
+
opt = Faraday::SSLOptions.new
|
323
|
+
assert_raises Faraday::Options.fetch_error_class do
|
324
|
+
opt.fetch :verify
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_fetch_works_with_key
|
329
|
+
opt = Faraday::SSLOptions.new
|
330
|
+
opt.verify = 1
|
331
|
+
assert_equal 1, opt.fetch(:verify)
|
332
|
+
end
|
333
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require File.expand_path("../helper", __FILE__)
|
2
|
+
require "rack/utils"
|
3
|
+
|
4
|
+
class TestParameters < Faraday::TestCase
|
5
|
+
# emulates ActiveSupport::SafeBuffer#gsub
|
6
|
+
FakeSafeBuffer = Struct.new(:string) do
|
7
|
+
def to_s() self end
|
8
|
+
def gsub(regex)
|
9
|
+
string.gsub(regex) {
|
10
|
+
match, = $&, '' =~ /a/
|
11
|
+
yield(match)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_escaping_safe_buffer_nested
|
17
|
+
monies = FakeSafeBuffer.new("$32,000.00")
|
18
|
+
assert_equal "a=%2432%2C000.00", Faraday::NestedParamsEncoder.encode("a" => monies)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_escaping_safe_buffer_flat
|
22
|
+
monies = FakeSafeBuffer.new("$32,000.00")
|
23
|
+
assert_equal "a=%2432%2C000.00", Faraday::FlatParamsEncoder.encode("a" => monies)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_raises_typeerror_nested
|
27
|
+
error = assert_raises TypeError do
|
28
|
+
Faraday::NestedParamsEncoder.encode("")
|
29
|
+
end
|
30
|
+
assert_equal "Can't convert String into Hash.", error.message
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_raises_typeerror_flat
|
34
|
+
error = assert_raises TypeError do
|
35
|
+
Faraday::FlatParamsEncoder.encode("")
|
36
|
+
end
|
37
|
+
assert_equal "Can't convert String into Hash.", error.message
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_decode_array_nested
|
41
|
+
query = "a[1]=one&a[2]=two&a[3]=three"
|
42
|
+
expected = {"a" => ["one", "two", "three"]}
|
43
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_decode_array_flat
|
47
|
+
query = "a=one&a=two&a=three"
|
48
|
+
expected = {"a" => ["one", "two", "three"]}
|
49
|
+
assert_equal expected, Faraday::FlatParamsEncoder.decode(query)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_nested_decode_hash
|
53
|
+
query = "a[b1]=one&a[b2]=two&a[b][c]=foo"
|
54
|
+
expected = {"a" => {"b1" => "one", "b2" => "two", "b" => {"c" => "foo"}}}
|
55
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_encode_nil_nested
|
59
|
+
assert_equal "a", Faraday::NestedParamsEncoder.encode("a" => nil)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_encode_nil_flat
|
63
|
+
assert_equal "a", Faraday::FlatParamsEncoder.encode("a" => nil)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_decode_nested_array_rack_compat
|
67
|
+
query = "a[][one]=1&a[][two]=2&a[][one]=3&a[][two]=4"
|
68
|
+
expected = Rack::Utils.parse_nested_query(query)
|
69
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_decode_nested_array_mixed_types
|
73
|
+
query = "a[][one]=1&a[]=2&a[]=&a[]"
|
74
|
+
expected = Rack::Utils.parse_nested_query(query)
|
75
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_decode_nested_ignores_invalid_array
|
79
|
+
query = "[][a]=1&b=2"
|
80
|
+
expected = {"a" => "1", "b" => "2"}
|
81
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_decode_nested_ignores_repeated_array_notation
|
85
|
+
query = "a[][][]=1"
|
86
|
+
expected = {"a" => ["1"]}
|
87
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_decode_nested_ignores_malformed_keys
|
91
|
+
query = "=1&[]=2"
|
92
|
+
expected = {}
|
93
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_decode_nested_subkeys_dont_have_to_be_in_brackets
|
97
|
+
query = "a[b]c[d]e=1"
|
98
|
+
expected = {"a" => {"b" => {"c" => {"d" => {"e" => "1"}}}}}
|
99
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_decode_nested_raises_error_when_expecting_hash
|
103
|
+
error = assert_raises TypeError do
|
104
|
+
Faraday::NestedParamsEncoder.decode("a=1&a[b]=2")
|
105
|
+
end
|
106
|
+
assert_equal "expected Hash (got String) for param `a'", error.message
|
107
|
+
|
108
|
+
error = assert_raises TypeError do
|
109
|
+
Faraday::NestedParamsEncoder.decode("a[]=1&a[b]=2")
|
110
|
+
end
|
111
|
+
assert_equal "expected Hash (got Array) for param `a'", error.message
|
112
|
+
|
113
|
+
error = assert_raises TypeError do
|
114
|
+
Faraday::NestedParamsEncoder.decode("a[b]=1&a[]=2")
|
115
|
+
end
|
116
|
+
assert_equal "expected Array (got Hash) for param `a'", error.message
|
117
|
+
|
118
|
+
error = assert_raises TypeError do
|
119
|
+
Faraday::NestedParamsEncoder.decode("a=1&a[]=2")
|
120
|
+
end
|
121
|
+
assert_equal "expected Array (got String) for param `a'", error.message
|
122
|
+
|
123
|
+
error = assert_raises TypeError do
|
124
|
+
Faraday::NestedParamsEncoder.decode("a[b]=1&a[b][c]=2")
|
125
|
+
end
|
126
|
+
assert_equal "expected Hash (got String) for param `b'", error.message
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_decode_nested_final_value_overrides_any_type
|
130
|
+
query = "a[b][c]=1&a[b]=2"
|
131
|
+
expected = {"a" => {"b" => "2"}}
|
132
|
+
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_encode_rack_compat_nested
|
136
|
+
params = { :a => [{:one => "1", :two => "2"}, "3", ""] }
|
137
|
+
expected = Rack::Utils.build_nested_query(params)
|
138
|
+
assert_equal expected.split("&").sort,
|
139
|
+
Faraday::Utils.unescape(Faraday::NestedParamsEncoder.encode(params)).split("&").sort
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_encode_empty_string_array_value
|
143
|
+
expected = 'baz=&foo%5Bbar%5D='
|
144
|
+
assert_equal expected, Faraday::NestedParamsEncoder.encode(foo: {bar: ''}, baz: '')
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_encode_nil_array_value
|
148
|
+
expected = 'baz&foo%5Bbar%5D'
|
149
|
+
assert_equal expected, Faraday::NestedParamsEncoder.encode(foo: {bar: nil}, baz: nil)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_encode_empty_array_value
|
153
|
+
expected = 'baz%5B%5D&foo%5Bbar%5D%5B%5D'
|
154
|
+
Faraday::NestedParamsEncoder.encode(foo: { bar: [] }, baz: [])
|
155
|
+
assert_equal expected, Faraday::NestedParamsEncoder.encode(foo: { bar: [] }, baz: [])
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../helper', __FILE__)
|
3
|
+
|
4
|
+
Faraday::CompositeReadIO.class_eval { attr_reader :ios }
|
5
|
+
|
6
|
+
class RequestMiddlewareTest < Faraday::TestCase
|
7
|
+
def conn
|
8
|
+
Faraday.new do |b|
|
9
|
+
b.request :multipart
|
10
|
+
b.request :url_encoded
|
11
|
+
b.adapter :test do |stub|
|
12
|
+
stub.post('/echo') do |env|
|
13
|
+
posted_as = env[:request_headers]['Content-Type']
|
14
|
+
[200, {'Content-Type' => posted_as}, env[:body]]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_does_nothing_without_payload
|
21
|
+
response = conn.post('/echo')
|
22
|
+
assert_nil response.headers['Content-Type']
|
23
|
+
assert response.body.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_ignores_custom_content_type
|
27
|
+
response = conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
|
28
|
+
assert_equal 'application/x-foo', response.headers['Content-Type']
|
29
|
+
assert_equal({ :some => 'data' }, response.body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_url_encoded_no_header
|
33
|
+
response = conn.post('/echo', { :fruit => %w[apples oranges] })
|
34
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
35
|
+
assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_url_encoded_with_header
|
39
|
+
response = conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
|
40
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
41
|
+
assert_equal 'a=123', response.body
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_url_encoded_nested
|
45
|
+
response = conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
|
46
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
47
|
+
expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
|
48
|
+
assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_url_encoded_non_nested
|
52
|
+
response = conn.post('/echo', { :dimensions => ['date', 'location']}) do |req|
|
53
|
+
req.options.params_encoder = Faraday::FlatParamsEncoder
|
54
|
+
end
|
55
|
+
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
56
|
+
expected = { 'dimensions' => ['date', 'location'] }
|
57
|
+
assert_equal expected, Faraday::Utils.parse_query(response.body)
|
58
|
+
assert_equal 'dimensions=date&dimensions=location', response.body
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_url_encoded_unicode
|
62
|
+
err = capture_warnings {
|
63
|
+
response = conn.post('/echo', {:str => "eé cç aã aâ"})
|
64
|
+
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
65
|
+
}
|
66
|
+
assert err.empty?, "stderr did include: #{err}"
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_url_encoded_nested_keys
|
70
|
+
response = conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
|
71
|
+
assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_multipart
|
75
|
+
# assume params are out of order
|
76
|
+
regexes = [
|
77
|
+
/name\=\"a\"/,
|
78
|
+
/name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
79
|
+
/name=\"b\[d\]\"/]
|
80
|
+
|
81
|
+
payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
|
82
|
+
response = conn.post('/echo', payload)
|
83
|
+
|
84
|
+
assert_kind_of Faraday::CompositeReadIO, response.body
|
85
|
+
assert response.headers['Content-Type'].start_with?(
|
86
|
+
"multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX,
|
87
|
+
)
|
88
|
+
|
89
|
+
response.body.send(:ios).map{|io| io.read}.each do |io|
|
90
|
+
if re = regexes.detect { |r| io =~ r }
|
91
|
+
regexes.delete re
|
92
|
+
end
|
93
|
+
end
|
94
|
+
assert_equal [], regexes
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_multipart_with_arrays
|
98
|
+
# assume params are out of order
|
99
|
+
regexes = [
|
100
|
+
/name\=\"a\"/,
|
101
|
+
/name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
102
|
+
/name=\"b\[\]\[d\]\"/]
|
103
|
+
|
104
|
+
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
105
|
+
response = conn.post('/echo', payload)
|
106
|
+
|
107
|
+
assert_kind_of Faraday::CompositeReadIO, response.body
|
108
|
+
assert response.headers['Content-Type'].start_with?(
|
109
|
+
"multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX,
|
110
|
+
)
|
111
|
+
|
112
|
+
response.body.send(:ios).map{|io| io.read}.each do |io|
|
113
|
+
if re = regexes.detect { |r| io =~ r }
|
114
|
+
regexes.delete re
|
115
|
+
end
|
116
|
+
end
|
117
|
+
assert_equal [], regexes
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_multipart_unique_boundary
|
121
|
+
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
122
|
+
response1 = conn.post('/echo', payload)
|
123
|
+
response2 = conn.post('/echo', payload)
|
124
|
+
assert response1.headers['Content-Type'] != response2.headers['Content-Type']
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class ResponseMiddlewareTest < Faraday::TestCase
|
4
|
+
def setup
|
5
|
+
@conn = Faraday.new do |b|
|
6
|
+
b.response :raise_error
|
7
|
+
b.adapter :test do |stub|
|
8
|
+
stub.get('ok') { [200, {'Content-Type' => 'text/html'}, '<body></body>'] }
|
9
|
+
stub.get('not-found') { [404, {'X-Reason' => 'because'}, 'keep looking'] }
|
10
|
+
stub.get('error') { [500, {'X-Error' => 'bailout'}, 'fail'] }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ResponseUpcaser < Faraday::Response::Middleware
|
16
|
+
def parse(body)
|
17
|
+
body.upcase
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_success
|
22
|
+
assert @conn.get('ok')
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_raises_not_found
|
26
|
+
error = assert_raises Faraday::ResourceNotFound do
|
27
|
+
@conn.get('not-found')
|
28
|
+
end
|
29
|
+
assert_equal 'the server responded with status 404', error.message
|
30
|
+
assert_equal 'because', error.response[:headers]['X-Reason']
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_raises_error
|
34
|
+
error = assert_raises Faraday::ClientError do
|
35
|
+
@conn.get('error')
|
36
|
+
end
|
37
|
+
assert_equal 'the server responded with status 500', error.message
|
38
|
+
assert_equal 'bailout', error.response[:headers]['X-Error']
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_upcase
|
42
|
+
@conn.builder.insert(0, ResponseUpcaser)
|
43
|
+
assert_equal '<BODY></BODY>', @conn.get('ok').body
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class ResponseNoBodyMiddleWareTest < Faraday::TestCase
|
48
|
+
def setup
|
49
|
+
@conn = Faraday.new do |b|
|
50
|
+
b.response :raise_error
|
51
|
+
b.adapter :test do |stub|
|
52
|
+
stub.get('not_modified') { [304, nil, nil] }
|
53
|
+
stub.get('no_content') { [204, nil, nil] }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
@conn.builder.insert(0, NotCalled)
|
57
|
+
end
|
58
|
+
|
59
|
+
class NotCalled < Faraday::Response::Middleware
|
60
|
+
def parse(body)
|
61
|
+
raise "this should not be called"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_204
|
66
|
+
assert_nil @conn.get('no_content').body
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_304
|
70
|
+
assert_nil @conn.get('not_modified').body
|
71
|
+
end
|
72
|
+
end
|
data/test/strawberry.rb
ADDED