faraday 0.17.4 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +156 -8
  3. data/LICENSE.md +1 -1
  4. data/README.md +16 -358
  5. data/Rakefile +1 -7
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +118 -69
  9. data/lib/faraday/adapter/typhoeus.rb +4 -1
  10. data/lib/faraday/adapter.rb +72 -22
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +343 -185
  14. data/lib/faraday/dependency_loader.rb +37 -0
  15. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  16. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  17. data/lib/faraday/error.rb +29 -35
  18. data/lib/faraday/file_part.rb +128 -0
  19. data/lib/faraday/logging/formatter.rb +105 -0
  20. data/lib/faraday/methods.rb +6 -0
  21. data/lib/faraday/middleware.rb +19 -25
  22. data/lib/faraday/middleware_registry.rb +129 -0
  23. data/lib/faraday/options/connection_options.rb +22 -0
  24. data/lib/faraday/options/env.rb +181 -0
  25. data/lib/faraday/options/proxy_options.rb +32 -0
  26. data/lib/faraday/options/request_options.rb +22 -0
  27. data/lib/faraday/options/ssl_options.rb +59 -0
  28. data/lib/faraday/options.rb +36 -191
  29. data/lib/faraday/param_part.rb +53 -0
  30. data/lib/faraday/parameters.rb +4 -197
  31. data/lib/faraday/rack_builder.rb +76 -64
  32. data/lib/faraday/request/authorization.rb +51 -30
  33. data/lib/faraday/request/basic_authentication.rb +14 -7
  34. data/lib/faraday/request/instrumentation.rb +45 -27
  35. data/lib/faraday/request/multipart.rb +86 -48
  36. data/lib/faraday/request/retry.rb +197 -171
  37. data/lib/faraday/request/token_authentication.rb +15 -10
  38. data/lib/faraday/request/url_encoded.rb +43 -23
  39. data/lib/faraday/request.rb +86 -44
  40. data/lib/faraday/response/logger.rb +22 -69
  41. data/lib/faraday/response/raise_error.rb +49 -18
  42. data/lib/faraday/response.rb +24 -20
  43. data/lib/faraday/utils/headers.rb +139 -0
  44. data/lib/faraday/utils/params_hash.rb +61 -0
  45. data/lib/faraday/utils.rb +38 -247
  46. data/lib/faraday/version.rb +5 -0
  47. data/lib/faraday.rb +127 -189
  48. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  49. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  50. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  51. data/spec/faraday/adapter/excon_spec.rb +49 -0
  52. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  53. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  54. data/spec/faraday/adapter/patron_spec.rb +18 -0
  55. data/spec/faraday/adapter/rack_spec.rb +8 -0
  56. data/spec/faraday/adapter/test_spec.rb +377 -0
  57. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  58. data/spec/faraday/adapter_registry_spec.rb +28 -0
  59. data/spec/faraday/adapter_spec.rb +55 -0
  60. data/spec/faraday/composite_read_io_spec.rb +80 -0
  61. data/spec/faraday/connection_spec.rb +736 -0
  62. data/spec/faraday/error_spec.rb +12 -54
  63. data/spec/faraday/middleware_spec.rb +52 -0
  64. data/spec/faraday/options/env_spec.rb +70 -0
  65. data/spec/faraday/options/options_spec.rb +297 -0
  66. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  67. data/spec/faraday/options/request_options_spec.rb +19 -0
  68. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  69. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  70. data/spec/faraday/rack_builder_spec.rb +345 -0
  71. data/spec/faraday/request/authorization_spec.rb +96 -0
  72. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  73. data/spec/faraday/request/multipart_spec.rb +302 -0
  74. data/spec/faraday/request/retry_spec.rb +242 -0
  75. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  76. data/spec/faraday/request_spec.rb +120 -0
  77. data/spec/faraday/response/logger_spec.rb +220 -0
  78. data/spec/faraday/response/middleware_spec.rb +68 -0
  79. data/spec/faraday/response/raise_error_spec.rb +78 -15
  80. data/spec/faraday/response_spec.rb +75 -0
  81. data/spec/faraday/utils/headers_spec.rb +82 -0
  82. data/spec/faraday/utils_spec.rb +56 -0
  83. data/spec/faraday_spec.rb +37 -0
  84. data/spec/spec_helper.rb +63 -36
  85. data/spec/support/disabling_stub.rb +14 -0
  86. data/spec/support/fake_safe_buffer.rb +15 -0
  87. data/spec/support/helper_methods.rb +133 -0
  88. data/spec/support/shared_examples/adapter.rb +105 -0
  89. data/spec/support/shared_examples/params_encoder.rb +18 -0
  90. data/spec/support/shared_examples/request_method.rb +262 -0
  91. data/spec/support/streaming_response_checker.rb +35 -0
  92. data/spec/support/webmock_rack_app.rb +68 -0
  93. metadata +194 -48
  94. data/lib/faraday/adapter/em_http.rb +0 -243
  95. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  96. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  97. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  98. data/lib/faraday/adapter/excon.rb +0 -82
  99. data/lib/faraday/adapter/httpclient.rb +0 -128
  100. data/lib/faraday/adapter/net_http.rb +0 -153
  101. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  102. data/lib/faraday/adapter/patron.rb +0 -95
  103. data/lib/faraday/adapter/rack.rb +0 -58
  104. data/lib/faraday/deprecate.rb +0 -109
  105. data/lib/faraday/upload_io.rb +0 -67
  106. data/spec/faraday/deprecate_spec.rb +0 -147
  107. data/test/adapters/default_test.rb +0 -14
  108. data/test/adapters/em_http_test.rb +0 -30
  109. data/test/adapters/em_synchrony_test.rb +0 -32
  110. data/test/adapters/excon_test.rb +0 -30
  111. data/test/adapters/httpclient_test.rb +0 -34
  112. data/test/adapters/integration.rb +0 -263
  113. data/test/adapters/logger_test.rb +0 -136
  114. data/test/adapters/net_http_persistent_test.rb +0 -114
  115. data/test/adapters/net_http_test.rb +0 -79
  116. data/test/adapters/patron_test.rb +0 -40
  117. data/test/adapters/rack_test.rb +0 -38
  118. data/test/adapters/test_middleware_test.rb +0 -157
  119. data/test/adapters/typhoeus_test.rb +0 -38
  120. data/test/authentication_middleware_test.rb +0 -65
  121. data/test/composite_read_io_test.rb +0 -109
  122. data/test/connection_test.rb +0 -738
  123. data/test/env_test.rb +0 -268
  124. data/test/helper.rb +0 -75
  125. data/test/live_server.rb +0 -67
  126. data/test/middleware/instrumentation_test.rb +0 -88
  127. data/test/middleware/retry_test.rb +0 -282
  128. data/test/middleware_stack_test.rb +0 -260
  129. data/test/multibyte.txt +0 -1
  130. data/test/options_test.rb +0 -333
  131. data/test/parameters_test.rb +0 -157
  132. data/test/request_middleware_test.rb +0 -126
  133. data/test/response_middleware_test.rb +0 -72
  134. data/test/strawberry.rb +0 -2
  135. data/test/utils_test.rb +0 -98
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
@@ -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
@@ -1,126 +0,0 @@
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
@@ -1,72 +0,0 @@
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 DELETED
@@ -1,2 +0,0 @@
1
- class MiddlewareStackTest::Strawberry < MiddlewareStackTest::Handler
2
- end