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
@@ -1,738 +0,0 @@
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