faraday 0.14.0 → 0.17.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +232 -0
  3. data/README.md +21 -7
  4. data/Rakefile +13 -0
  5. data/lib/faraday/adapter/em_http.rb +9 -9
  6. data/lib/faraday/adapter/em_synchrony.rb +5 -5
  7. data/lib/faraday/adapter/excon.rb +6 -3
  8. data/lib/faraday/adapter/httpclient.rb +4 -4
  9. data/lib/faraday/adapter/net_http.rb +25 -7
  10. data/lib/faraday/adapter/net_http_persistent.rb +33 -19
  11. data/lib/faraday/adapter/patron.rb +7 -12
  12. data/lib/faraday/adapter/rack.rb +1 -1
  13. data/lib/faraday/adapter.rb +2 -0
  14. data/lib/faraday/deprecate.rb +109 -0
  15. data/lib/faraday/error.rb +129 -34
  16. data/lib/faraday/options.rb +6 -5
  17. data/lib/faraday/parameters.rb +2 -1
  18. data/lib/faraday/rack_builder.rb +2 -2
  19. data/lib/faraday/request/retry.rb +65 -16
  20. data/lib/faraday/request.rb +22 -0
  21. data/lib/faraday/response/logger.rb +3 -3
  22. data/lib/faraday/response/raise_error.rb +7 -3
  23. data/lib/faraday/response.rb +3 -3
  24. data/lib/faraday/upload_io.rb +16 -6
  25. data/lib/faraday.rb +2 -3
  26. data/spec/faraday/deprecate_spec.rb +147 -0
  27. data/spec/faraday/error_spec.rb +102 -0
  28. data/spec/faraday/response/raise_error_spec.rb +106 -0
  29. data/spec/spec_helper.rb +105 -0
  30. data/test/adapters/default_test.rb +14 -0
  31. data/test/adapters/em_http_test.rb +30 -0
  32. data/test/adapters/em_synchrony_test.rb +32 -0
  33. data/test/adapters/excon_test.rb +30 -0
  34. data/test/adapters/httpclient_test.rb +34 -0
  35. data/test/adapters/integration.rb +263 -0
  36. data/test/adapters/logger_test.rb +136 -0
  37. data/test/adapters/net_http_persistent_test.rb +114 -0
  38. data/test/adapters/net_http_test.rb +79 -0
  39. data/test/adapters/patron_test.rb +40 -0
  40. data/test/adapters/rack_test.rb +38 -0
  41. data/test/adapters/test_middleware_test.rb +157 -0
  42. data/test/adapters/typhoeus_test.rb +38 -0
  43. data/test/authentication_middleware_test.rb +65 -0
  44. data/test/composite_read_io_test.rb +109 -0
  45. data/test/connection_test.rb +738 -0
  46. data/test/env_test.rb +268 -0
  47. data/test/helper.rb +75 -0
  48. data/test/live_server.rb +67 -0
  49. data/test/middleware/instrumentation_test.rb +88 -0
  50. data/test/middleware/retry_test.rb +282 -0
  51. data/test/middleware_stack_test.rb +260 -0
  52. data/test/multibyte.txt +1 -0
  53. data/test/options_test.rb +333 -0
  54. data/test/parameters_test.rb +157 -0
  55. data/test/request_middleware_test.rb +126 -0
  56. data/test/response_middleware_test.rb +72 -0
  57. data/test/strawberry.rb +2 -0
  58. data/test/utils_test.rb +98 -0
  59. metadata +48 -7
@@ -0,0 +1,738 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class TestConnection < Faraday::TestCase
4
+ def teardown
5
+ Faraday.default_connection_options = nil
6
+ end
7
+
8
+ def with_test_conn
9
+ old_conn = Faraday.default_connection
10
+ Faraday.default_connection = Faraday::Connection.new do |builder|
11
+ builder.adapter :test do |stub|
12
+ stub.get('/') do |_|
13
+ [200, nil, nil]
14
+ end
15
+ end
16
+ end
17
+
18
+ begin
19
+ yield
20
+ ensure
21
+ Faraday.default_connection = old_conn
22
+ end
23
+ end
24
+
25
+ def with_env_proxy_disabled
26
+ Faraday.ignore_env_proxy = true
27
+
28
+ begin
29
+ yield
30
+ ensure
31
+ Faraday.ignore_env_proxy = false
32
+ end
33
+ end
34
+
35
+ def with_env(new_env)
36
+ old_env = {}
37
+
38
+ new_env.each do |key, value|
39
+ old_env[key] = ENV.fetch(key, false)
40
+ ENV[key] = value
41
+ end
42
+
43
+ begin
44
+ yield
45
+ ensure
46
+ old_env.each do |key, value|
47
+ if value == false
48
+ ENV.delete key
49
+ else
50
+ ENV[key] = value
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ def test_initialize_parses_host_out_of_given_url
57
+ conn = Faraday::Connection.new 'http://sushi.com'
58
+ assert_equal 'sushi.com', conn.host
59
+ end
60
+
61
+ def test_initialize_inherits_default_port_out_of_given_url
62
+ conn = Faraday::Connection.new 'http://sushi.com'
63
+ assert_equal 80, conn.port
64
+ end
65
+
66
+ def test_initialize_parses_scheme_out_of_given_url
67
+ conn = Faraday::Connection.new 'http://sushi.com'
68
+ assert_equal 'http', conn.scheme
69
+ end
70
+
71
+ def test_initialize_parses_port_out_of_given_url
72
+ conn = Faraday::Connection.new 'http://sushi.com:815'
73
+ assert_equal 815, conn.port
74
+ end
75
+
76
+ def test_initialize_parses_nil_path_prefix_out_of_given_url
77
+ conn = Faraday::Connection.new 'http://sushi.com'
78
+ assert_equal '/', conn.path_prefix
79
+ end
80
+
81
+ def test_initialize_parses_path_prefix_out_of_given_url
82
+ conn = Faraday::Connection.new 'http://sushi.com/fish'
83
+ assert_equal '/fish', conn.path_prefix
84
+ end
85
+
86
+ def test_initialize_parses_path_prefix_out_of_given_url_option
87
+ conn = Faraday::Connection.new :url => 'http://sushi.com/fish'
88
+ assert_equal '/fish', conn.path_prefix
89
+ end
90
+
91
+ def test_initialize_stores_default_params_from_options
92
+ conn = Faraday::Connection.new :params => {:a => 1}
93
+ assert_equal({'a' => 1}, conn.params)
94
+ end
95
+
96
+ def test_initialize_stores_default_params_from_uri
97
+ conn = Faraday::Connection.new 'http://sushi.com/fish?a=1'
98
+ assert_equal({'a' => '1'}, conn.params)
99
+ end
100
+
101
+ def test_initialize_stores_default_params_from_uri_and_options
102
+ conn = Faraday::Connection.new 'http://sushi.com/fish?a=1&b=2', :params => {'a' => 3}
103
+ assert_equal({'a' => 3, 'b' => '2'}, conn.params)
104
+ end
105
+
106
+ def test_initialize_stores_default_headers_from_options
107
+ conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
108
+ assert_equal 'Faraday', conn.headers['User-agent']
109
+ end
110
+
111
+ def test_basic_auth_sets_header
112
+ conn = Faraday::Connection.new
113
+ assert_nil conn.headers['Authorization']
114
+
115
+ conn.basic_auth 'Aladdin', 'open sesame'
116
+ assert auth = conn.headers['Authorization']
117
+ assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
118
+ end
119
+
120
+ def test_auto_parses_basic_auth_from_url_and_unescapes
121
+ conn = Faraday::Connection.new :url => 'http://foo%40bar.com:pass%20word@sushi.com/fish'
122
+ assert auth = conn.headers['Authorization']
123
+ assert_equal Faraday::Request::BasicAuthentication.header('foo@bar.com', 'pass word'), auth
124
+ end
125
+
126
+ def test_token_auth_sets_header
127
+ conn = Faraday::Connection.new
128
+ assert_nil conn.headers['Authorization']
129
+
130
+ conn.token_auth 'abcdef', :nonce => 'abc'
131
+ assert auth = conn.headers['Authorization']
132
+ assert_match(/^Token /, auth)
133
+ assert_match(/token="abcdef"/, auth)
134
+ assert_match(/nonce="abc"/, auth)
135
+ end
136
+
137
+ def test_build_exclusive_url_uses_connection_host_as_default_uri_host
138
+ conn = Faraday::Connection.new
139
+ conn.host = 'sushi.com'
140
+ uri = conn.build_exclusive_url('/sake.html')
141
+ assert_equal 'sushi.com', uri.host
142
+ end
143
+
144
+ def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
145
+ conn = Faraday::Connection.new
146
+ conn.port = 23
147
+ uri = conn.build_exclusive_url('http://sushi.com')
148
+ assert_equal 80, uri.port
149
+ end
150
+
151
+ def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
152
+ conn = Faraday::Connection.new 'http://sushi.com'
153
+ uri = conn.build_exclusive_url('/sake.html')
154
+ assert_equal 'http', uri.scheme
155
+ end
156
+
157
+ def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
158
+ conn = Faraday::Connection.new
159
+ conn.path_prefix = '/fish'
160
+ uri = conn.build_exclusive_url('sake.html')
161
+ assert_equal '/fish/sake.html', uri.path
162
+ end
163
+
164
+ def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
165
+ conn = Faraday::Connection.new
166
+ conn.path_prefix = '/'
167
+ uri = conn.build_exclusive_url('sake.html')
168
+ assert_equal '/sake.html', uri.path
169
+ end
170
+
171
+ def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
172
+ conn = Faraday::Connection.new
173
+ conn.path_prefix = 'fish'
174
+ uri = conn.build_exclusive_url('sake.html')
175
+ assert_equal '/fish/sake.html', uri.path
176
+ end
177
+
178
+ def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
179
+ conn = Faraday::Connection.new
180
+ conn.path_prefix = '/fish/'
181
+ uri = conn.build_exclusive_url('sake.html')
182
+ assert_equal '/fish/sake.html', uri.path
183
+ end
184
+
185
+ def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
186
+ conn = Faraday::Connection.new
187
+ conn.path_prefix = '/fish'
188
+ uri = conn.build_exclusive_url('/sake.html')
189
+ assert_equal '/sake.html', uri.path
190
+ end
191
+
192
+ def test_build_exclusive_url_parses_url_params_into_path
193
+ conn = Faraday::Connection.new
194
+ uri = conn.build_exclusive_url('http://sushi.com/sake.html')
195
+ assert_equal '/sake.html', uri.path
196
+ end
197
+
198
+ def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url
199
+ conn = Faraday::Connection.new
200
+ conn.url_prefix = 'http://sushi.com/nigiri'
201
+ uri = conn.build_exclusive_url
202
+ assert_equal '/nigiri', uri.path
203
+ end
204
+
205
+ def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url
206
+ conn = Faraday::Connection.new
207
+ conn.url_prefix = 'http://sushi.com/nigiri'
208
+ uri = conn.build_exclusive_url('')
209
+ assert_equal '/nigiri', uri.path
210
+ end
211
+
212
+ def test_build_exclusive_url_doesnt_use_connection_params
213
+ conn = Faraday::Connection.new 'http://sushi.com/nigiri'
214
+ conn.params = {:a => 1}
215
+ assert_equal 'http://sushi.com/nigiri', conn.build_exclusive_url.to_s
216
+ end
217
+
218
+ def test_build_exclusive_url_uses_argument_params
219
+ conn = Faraday::Connection.new 'http://sushi.com/nigiri'
220
+ conn.params = {:a => 1}
221
+ params = Faraday::Utils::ParamsHash.new
222
+ params[:a] = 2
223
+ url = conn.build_exclusive_url(nil, params)
224
+ assert_equal 'http://sushi.com/nigiri?a=2', url.to_s
225
+ end
226
+
227
+ def test_build_url_uses_params
228
+ conn = Faraday::Connection.new 'http://sushi.com/nigiri'
229
+ conn.params = {:a => 1, :b => 1}
230
+ assert_equal 'http://sushi.com/nigiri?a=1&b=1', conn.build_url.to_s
231
+ end
232
+
233
+ def test_build_url_merges_params
234
+ conn = Faraday::Connection.new 'http://sushi.com/nigiri'
235
+ conn.params = {:a => 1, :b => 1}
236
+ url = conn.build_url(nil, :b => 2, :c => 3)
237
+ assert_equal 'http://sushi.com/nigiri?a=1&b=2&c=3', url.to_s
238
+ end
239
+
240
+ def test_request_header_change_does_not_modify_connection_header
241
+ connection = Faraday.new(:url => 'https://asushi.com/sake.html')
242
+ connection.headers = {'Authorization' => 'token abc123'}
243
+
244
+ request = connection.build_request(:get)
245
+ request.headers.delete('Authorization')
246
+
247
+ assert_equal connection.headers.keys.sort, ['Authorization']
248
+ assert connection.headers.include?('Authorization')
249
+
250
+ assert_equal request.headers.keys.sort, []
251
+ assert !request.headers.include?('Authorization')
252
+ end
253
+
254
+ def test_env_url_parses_url_params_into_query
255
+ uri = env_url('http://sushi.com/sake.html', 'a[b]' => '1 + 2')
256
+ assert_equal 'a%5Bb%5D=1+%2B+2', uri.query
257
+ end
258
+
259
+ def test_env_url_escapes_per_spec
260
+ uri = env_url(nil, 'a' => '1+2 foo~bar.-baz')
261
+ assert_equal 'a=1%2B2+foo~bar.-baz', uri.query
262
+ end
263
+
264
+ def test_env_url_bracketizes_nested_params_in_query
265
+ url = env_url nil, 'a' => {'b' => 'c'}
266
+ assert_equal 'a%5Bb%5D=c', url.query
267
+ end
268
+
269
+ def test_env_url_bracketizes_repeated_params_in_query
270
+ uri = env_url('http://sushi.com/sake.html', 'a' => [1, 2])
271
+ assert_equal 'a%5B%5D=1&a%5B%5D=2', uri.query
272
+ end
273
+
274
+ def test_env_url_without_braketizing_repeated_params_in_query
275
+ uri = env_url 'http://sushi.com', 'a' => [1, 2] do |conn|
276
+ conn.options.params_encoder = Faraday::FlatParamsEncoder
277
+ end
278
+ assert_equal 'a=1&a=2', uri.query
279
+ end
280
+
281
+ def test_build_exclusive_url_parses_url
282
+ conn = Faraday::Connection.new
283
+ uri = conn.build_exclusive_url('http://sushi.com/sake.html')
284
+ assert_equal 'http', uri.scheme
285
+ assert_equal 'sushi.com', uri.host
286
+ assert_equal '/sake.html', uri.path
287
+ end
288
+
289
+ def test_build_exclusive_url_parses_url_and_changes_scheme
290
+ conn = Faraday::Connection.new :url => 'http://sushi.com/sushi'
291
+ conn.scheme = 'https'
292
+ uri = conn.build_exclusive_url('sake.html')
293
+ assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
294
+ end
295
+
296
+ def test_build_exclusive_url_joins_url_to_base_with_ending_slash
297
+ conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
298
+ uri = conn.build_exclusive_url('sake.html')
299
+ assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s
300
+ end
301
+
302
+ def test_build_exclusive_url_used_default_base_with_ending_slash
303
+ conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
304
+ uri = conn.build_exclusive_url
305
+ assert_equal 'http://sushi.com/sushi/', uri.to_s
306
+ end
307
+
308
+ def test_build_exclusive_url_overrides_base
309
+ conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
310
+ uri = conn.build_exclusive_url('/sake/')
311
+ assert_equal 'http://sushi.com/sake/', uri.to_s
312
+ end
313
+
314
+ def test_build_exclusive_url_handles_uri_instances
315
+ conn = Faraday::Connection.new
316
+ uri = conn.build_exclusive_url(URI('/sake.html'))
317
+ assert_equal '/sake.html', uri.path
318
+ end
319
+
320
+ def test_proxy_accepts_string
321
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
322
+ conn = Faraday::Connection.new
323
+ conn.proxy = 'http://proxy.com'
324
+ assert_equal 'proxy.com', conn.proxy.host
325
+ end
326
+ end
327
+
328
+ def test_proxy_accepts_uri
329
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
330
+ conn = Faraday::Connection.new
331
+ conn.proxy = URI.parse('http://proxy.com')
332
+ assert_equal 'proxy.com', conn.proxy.host
333
+ end
334
+ end
335
+
336
+ def test_proxy_accepts_hash_with_string_uri
337
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
338
+ conn = Faraday::Connection.new
339
+ conn.proxy = {:uri => 'http://proxy.com', :user => 'rick'}
340
+ assert_equal 'proxy.com', conn.proxy.host
341
+ assert_equal 'rick', conn.proxy.user
342
+ end
343
+ end
344
+
345
+ def test_proxy_accepts_hash
346
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
347
+ conn = Faraday::Connection.new
348
+ conn.proxy = {:uri => URI.parse('http://proxy.com'), :user => 'rick'}
349
+ assert_equal 'proxy.com', conn.proxy.host
350
+ assert_equal 'rick', conn.proxy.user
351
+ end
352
+ end
353
+
354
+ def test_proxy_accepts_http_env
355
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
356
+ conn = Faraday::Connection.new
357
+ assert_equal 'duncan.proxy.com', conn.proxy.host
358
+ end
359
+ end
360
+
361
+ def test_proxy_accepts_http_env_with_auth
362
+ with_env 'http_proxy' => 'http://a%40b:my%20pass@duncan.proxy.com:80' do
363
+ conn = Faraday::Connection.new
364
+ assert_equal 'a@b', conn.proxy.user
365
+ assert_equal 'my pass', conn.proxy.password
366
+ end
367
+ end
368
+
369
+ def test_proxy_accepts_env_without_scheme
370
+ with_env 'http_proxy' => 'localhost:8888' do
371
+ uri = Faraday::Connection.new.proxy[:uri]
372
+ assert_equal 'localhost', uri.host
373
+ assert_equal 8888, uri.port
374
+ end
375
+ end
376
+
377
+ def test_no_proxy_from_env
378
+ with_env 'http_proxy' => nil do
379
+ conn = Faraday::Connection.new
380
+ assert_nil conn.proxy
381
+ end
382
+ end
383
+
384
+ def test_no_proxy_from_blank_env
385
+ with_env 'http_proxy' => '' do
386
+ conn = Faraday::Connection.new
387
+ assert_nil conn.proxy
388
+ end
389
+ end
390
+
391
+ def test_proxy_doesnt_accept_uppercase_env
392
+ with_env 'HTTP_PROXY' => 'http://localhost:8888/' do
393
+ conn = Faraday::Connection.new
394
+ assert_nil conn.proxy
395
+ end
396
+ end
397
+
398
+ def test_dynamic_proxy
399
+ with_test_conn do
400
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
401
+ Faraday.get('http://google.co.uk')
402
+ assert_equal 'duncan.proxy.com', Faraday.default_connection.instance_variable_get('@temp_proxy').host
403
+ end
404
+ Faraday.get('http://google.co.uk')
405
+ assert_nil Faraday.default_connection.instance_variable_get('@temp_proxy')
406
+ end
407
+ end
408
+
409
+ def test_ignore_env_proxy
410
+ with_env_proxy_disabled do
411
+ with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
412
+ conn = Faraday::Connection.new(proxy: nil)
413
+ assert_nil conn.proxy
414
+ end
415
+ end
416
+ end
417
+
418
+ if URI.parse('').respond_to?(:find_proxy)
419
+ def test_proxy_allowed_when_url_in_no_proxy_list
420
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
421
+ conn = Faraday::Connection.new('http://example.com')
422
+ assert_nil conn.proxy
423
+ end
424
+ end
425
+
426
+ def test_proxy_allowed_when_prefixed_url_is_not_in_no_proxy_list
427
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
428
+ conn = Faraday::Connection.new('http://prefixedexample.com')
429
+ assert_equal 'proxy.com', conn.proxy.host
430
+ end
431
+ end
432
+
433
+ def test_proxy_allowed_when_subdomain_url_is_in_no_proxy_list
434
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
435
+ conn = Faraday::Connection.new('http://subdomain.example.com')
436
+ assert_nil conn.proxy
437
+ end
438
+ end
439
+
440
+ def test_proxy_allowed_when_url_not_in_no_proxy_list
441
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example2.com' do
442
+ conn = Faraday::Connection.new('http://example.com')
443
+ assert_equal 'proxy.com', conn.proxy.host
444
+ end
445
+ end
446
+
447
+ def test_proxy_allowed_when_ip_address_is_not_in_no_proxy_list_but_url_is
448
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'localhost' do
449
+ conn = Faraday::Connection.new('http://127.0.0.1')
450
+ assert_nil conn.proxy
451
+ end
452
+ end
453
+
454
+ def test_proxy_allowed_when_url_is_not_in_no_proxy_list_but_ip_address_is
455
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => '127.0.0.1' do
456
+ conn = Faraday::Connection.new('http://localhost')
457
+ assert_nil conn.proxy
458
+ end
459
+ end
460
+
461
+ def test_proxy_allowed_in_multi_element_no_proxy_list
462
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example0.com,example.com,example1.com' do
463
+ assert_nil Faraday::Connection.new('http://example0.com').proxy
464
+ assert_nil Faraday::Connection.new('http://example.com').proxy
465
+ assert_nil Faraday::Connection.new('http://example1.com').proxy
466
+ assert_equal 'proxy.com', Faraday::Connection.new('http://example2.com').proxy.host
467
+ end
468
+ end
469
+
470
+ def test_dynamic_no_proxy
471
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
472
+ conn = Faraday.new
473
+
474
+ assert_equal 'proxy.com', conn.instance_variable_get('@temp_proxy').host
475
+ conn.get('https://google.co.uk')
476
+ assert_nil conn.instance_variable_get('@temp_proxy')
477
+ end
478
+ end
479
+
480
+ def test_issue
481
+ with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'google.co.uk' do
482
+ conn = Faraday.new
483
+ conn.proxy = 'http://proxy2.com'
484
+
485
+ assert_equal true, conn.instance_variable_get('@manual_proxy')
486
+ assert_equal 'proxy2.com', conn.proxy_for_request('https://google.co.uk').host
487
+ end
488
+ end
489
+ end
490
+
491
+ def test_proxy_requires_uri
492
+ conn = Faraday::Connection.new
493
+ assert_raises ArgumentError do
494
+ conn.proxy = {:uri => :bad_uri, :user => 'rick'}
495
+ end
496
+ end
497
+
498
+ def test_dups_connection_object
499
+ conn = Faraday::Connection.new 'http://sushi.com/foo',
500
+ :ssl => { :verify => :none },
501
+ :headers => {'content-type' => 'text/plain'},
502
+ :params => {'a'=>'1'},
503
+ :request => {:timeout => 5}
504
+
505
+ other = conn.dup
506
+
507
+ assert_equal conn.build_exclusive_url, other.build_exclusive_url
508
+ assert_equal 'text/plain', other.headers['content-type']
509
+ assert_equal '1', other.params['a']
510
+
511
+ other.basic_auth('', '')
512
+ other.headers['content-length'] = 12
513
+ other.params['b'] = '2'
514
+ other.options[:open_timeout] = 10
515
+
516
+ assert_equal 2, other.builder.handlers.size
517
+ assert_equal 2, conn.builder.handlers.size
518
+ assert !conn.headers.key?('content-length')
519
+ assert !conn.params.key?('b')
520
+ assert_equal 5, other.options[:timeout]
521
+ assert_nil conn.options[:open_timeout]
522
+ end
523
+
524
+ def test_initialize_with_false_option
525
+ conn = Faraday::Connection.new :ssl => {:verify => false}
526
+ assert !conn.ssl.verify?
527
+ end
528
+
529
+ def test_init_with_block
530
+ conn = Faraday::Connection.new { }
531
+ assert_equal 0, conn.builder.handlers.size
532
+ end
533
+
534
+ def test_init_with_block_yields_connection
535
+ conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
536
+ faraday.adapter :net_http
537
+ faraday.url_prefix = 'http://sushi.com/omnom'
538
+ assert_equal '1', faraday.params['a']
539
+ }
540
+ assert_equal 1, conn.builder.handlers.size
541
+ assert_equal '/omnom', conn.path_prefix
542
+ end
543
+
544
+ def test_respond_to
545
+ assert Faraday.respond_to?(:get)
546
+ assert Faraday.respond_to?(:post)
547
+ end
548
+
549
+ def test_default_connection_options
550
+ Faraday.default_connection_options.request.timeout = 10
551
+ conn = Faraday.new 'http://sushi.com/foo'
552
+ assert_equal 10, conn.options.timeout
553
+ end
554
+
555
+ def test_default_connection_options_without_url
556
+ Faraday.default_connection_options.request.timeout = 10
557
+ conn = Faraday.new :url => 'http://sushi.com/foo'
558
+ assert_equal 10, conn.options.timeout
559
+ end
560
+
561
+ def test_default_connection_options_as_hash
562
+ Faraday.default_connection_options = { request: { timeout: 10 } }
563
+ conn = Faraday.new 'http://sushi.com/foo'
564
+ assert_equal 10, conn.options.timeout
565
+ end
566
+
567
+ def test_default_connection_options_as_hash_without_url
568
+ Faraday.default_connection_options = { request: { timeout: 10 } }
569
+ conn = Faraday.new :url => 'http://sushi.com/foo'
570
+ assert_equal 10, conn.options.timeout
571
+ end
572
+
573
+ def test_default_connection_options_as_hash_with_instance_connection_options
574
+ Faraday.default_connection_options = { request: { timeout: 10 } }
575
+ conn = Faraday.new 'http://sushi.com/foo', request: { open_timeout: 1 }
576
+ assert_equal 1, conn.options.open_timeout
577
+ assert_equal 10, conn.options.timeout
578
+ end
579
+
580
+ def test_default_connection_options_persist_with_an_instance_overriding
581
+ Faraday.default_connection_options.request.timeout = 10
582
+ conn = Faraday.new 'http://nigiri.com/bar'
583
+ conn.options.timeout = 1
584
+ assert_equal 10, Faraday.default_connection_options.request.timeout
585
+
586
+ other = Faraday.new :url => 'https://sushi.com/foo'
587
+ other.options.timeout = 1
588
+
589
+ assert_equal 10, Faraday.default_connection_options.request.timeout
590
+ end
591
+
592
+ def test_default_connection_uses_default_connection_options
593
+ Faraday.default_connection_options.request.timeout = 10
594
+ default_conn = Faraday.default_connection
595
+
596
+ assert_equal 10, default_conn.options.timeout
597
+ end
598
+
599
+ def env_url(url, params)
600
+ conn = Faraday::Connection.new(url, :params => params)
601
+ yield(conn) if block_given?
602
+ req = conn.build_request(:get)
603
+ req.to_env(conn).url
604
+ end
605
+ end
606
+
607
+ class TestRequestParams < Faraday::TestCase
608
+ def create_connection(*args)
609
+ @conn = Faraday::Connection.new(*args) do |conn|
610
+ yield(conn) if block_given?
611
+ class << conn.builder
612
+ undef app
613
+ def app() lambda { |env| env } end
614
+ end
615
+ end
616
+ end
617
+
618
+ def assert_query_equal(expected, query)
619
+ assert_equal expected, query.split('&').sort
620
+ end
621
+
622
+ def with_default_params_encoder(encoder)
623
+ old_encoder = Faraday::Utils.default_params_encoder
624
+ begin
625
+ Faraday::Utils.default_params_encoder = encoder
626
+ yield
627
+ ensure
628
+ Faraday::Utils.default_params_encoder = old_encoder
629
+ end
630
+ end
631
+
632
+ def test_merges_connection_and_request_params
633
+ create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
634
+ query = get '?page=1', :limit => 5
635
+ assert_query_equal %w[format=json limit=5 page=1 token=abc], query
636
+ end
637
+
638
+ def test_overrides_connection_params
639
+ create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
640
+ conn.params[:b] = 'B'
641
+ assert_equal 'c', conn.params[:c]
642
+ end
643
+ assert_query_equal %w[a=A b=B c=c], get
644
+ end
645
+
646
+ def test_all_overrides_connection_params
647
+ create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
648
+ conn.params = {'b' => 'b'}
649
+ end
650
+ assert_query_equal %w[b=b], get
651
+ end
652
+
653
+ def test_overrides_request_params
654
+ create_connection
655
+ query = get '?p=1&a=a', :p => 2
656
+ assert_query_equal %w[a=a p=2], query
657
+ end
658
+
659
+ def test_overrides_request_params_block
660
+ create_connection
661
+ query = get '?p=1&a=a', :p => 2 do |req|
662
+ req.params[:p] = 3
663
+ end
664
+ assert_query_equal %w[a=a p=3], query
665
+ end
666
+
667
+ def test_overrides_request_params_block_url
668
+ create_connection
669
+ query = get nil, :p => 2 do |req|
670
+ req.url '?p=1&a=a', 'p' => 3
671
+ end
672
+ assert_query_equal %w[a=a p=3], query
673
+ end
674
+
675
+ def test_overrides_all_request_params
676
+ create_connection :params => {:c => 'c'}
677
+ query = get '?p=1&a=a', :p => 2 do |req|
678
+ assert_equal 'a', req.params[:a]
679
+ assert_equal 'c', req.params['c']
680
+ assert_equal 2, req.params['p']
681
+ req.params = {:b => 'b'}
682
+ assert_equal 'b', req.params['b']
683
+ end
684
+ assert_query_equal %w[b=b], query
685
+ end
686
+
687
+ def test_array_params_in_url
688
+ with_default_params_encoder(nil) do
689
+ create_connection 'http://a.co/page1?color[]=red&color[]=blue'
690
+ query = get
691
+ assert_equal 'color%5B%5D=red&color%5B%5D=blue', query
692
+ end
693
+ end
694
+
695
+ def test_array_params_in_params
696
+ with_default_params_encoder(nil) do
697
+ create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
698
+ query = get
699
+ assert_equal 'color%5B%5D=red&color%5B%5D=blue', query
700
+ end
701
+ end
702
+
703
+ def test_array_params_in_url_with_flat_params
704
+ with_default_params_encoder(Faraday::FlatParamsEncoder) do
705
+ create_connection 'http://a.co/page1?color=red&color=blue'
706
+ query = get
707
+ assert_equal 'color=red&color=blue', query
708
+ end
709
+ end
710
+
711
+ def test_array_params_in_params_with_flat_params
712
+ with_default_params_encoder(Faraday::FlatParamsEncoder) do
713
+ create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
714
+ query = get
715
+ assert_equal 'color=red&color=blue', query
716
+ end
717
+ end
718
+
719
+ def test_params_with_connection_options
720
+ encoder = Object.new
721
+ def encoder.encode(params)
722
+ params.map { |k,v| "#{k.upcase}-#{v.upcase}" }.join(',')
723
+ end
724
+
725
+ create_connection :params => {:color => 'red'}
726
+ query = get('', :feeling => 'blue') do |req|
727
+ req.options.params_encoder = encoder
728
+ end
729
+ assert_equal ['COLOR-RED', 'FEELING-BLUE'], query.split(',').sort
730
+ end
731
+
732
+ def get(*args)
733
+ env = @conn.get(*args) do |req|
734
+ yield(req) if block_given?
735
+ end
736
+ env[:url].query
737
+ end
738
+ end