faraday 0.17.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +52 -8
- data/LICENSE.md +1 -1
- data/README.md +18 -358
- data/Rakefile +1 -7
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday.rb +94 -175
- data/lib/faraday/adapter.rb +82 -22
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +98 -56
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http.rb +130 -63
- data/lib/faraday/adapter/net_http_persistent.rb +50 -27
- data/lib/faraday/adapter/patron.rb +80 -43
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +312 -182
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +9 -35
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +12 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options.rb +32 -183
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request.rb +68 -36
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +79 -48
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/response.rb +24 -14
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +38 -18
- data/lib/faraday/utils.rb +36 -245
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +0 -57
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +34 -0
- data/spec/faraday/params_encoders/nested_spec.rb +134 -0
- data/spec/faraday/rack_builder_spec.rb +196 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +274 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +15 -15
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -36
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +66 -38
- data/lib/faraday/deprecate.rb +0 -107
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
data/test/multibyte.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ファイル
|
data/test/options_test.rb
DELETED
@@ -1,333 +0,0 @@
|
|
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
|
data/test/parameters_test.rb
DELETED
@@ -1,157 +0,0 @@
|
|
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
|