faraday 0.9.1 → 0.17.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +212 -0
- data/LICENSE.md +1 -1
- data/README.md +203 -28
- data/Rakefile +6 -64
- data/lib/faraday/adapter/em_http.rb +17 -11
- data/lib/faraday/adapter/em_http_ssl_patch.rb +1 -1
- data/lib/faraday/adapter/em_synchrony.rb +19 -5
- data/lib/faraday/adapter/excon.rb +13 -11
- data/lib/faraday/adapter/httpclient.rb +31 -9
- data/lib/faraday/adapter/net_http.rb +36 -14
- data/lib/faraday/adapter/net_http_persistent.rb +37 -17
- data/lib/faraday/adapter/patron.rb +44 -21
- data/lib/faraday/adapter/rack.rb +1 -1
- data/lib/faraday/adapter/test.rb +79 -28
- data/lib/faraday/adapter/typhoeus.rb +4 -115
- data/lib/faraday/adapter.rb +10 -1
- data/lib/faraday/autoload.rb +1 -1
- data/lib/faraday/connection.rb +72 -20
- data/lib/faraday/deprecate.rb +107 -0
- data/lib/faraday/error.rb +132 -27
- data/lib/faraday/options.rb +45 -22
- data/lib/faraday/parameters.rb +56 -39
- data/lib/faraday/rack_builder.rb +29 -4
- data/lib/faraday/request/authorization.rb +1 -2
- data/lib/faraday/request/multipart.rb +7 -2
- data/lib/faraday/request/retry.rb +84 -19
- data/lib/faraday/request.rb +22 -0
- data/lib/faraday/response/logger.rb +29 -8
- data/lib/faraday/response/raise_error.rb +7 -3
- data/lib/faraday/response.rb +9 -5
- data/lib/faraday/utils.rb +32 -3
- data/lib/faraday.rb +15 -36
- data/spec/faraday/deprecate_spec.rb +69 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/spec_helper.rb +105 -0
- data/test/adapters/em_http_test.rb +10 -0
- data/test/adapters/em_synchrony_test.rb +22 -10
- data/test/adapters/excon_test.rb +10 -0
- data/test/adapters/httpclient_test.rb +14 -1
- data/test/adapters/integration.rb +17 -8
- data/test/adapters/logger_test.rb +65 -11
- data/test/adapters/net_http_persistent_test.rb +96 -2
- data/test/adapters/net_http_test.rb +67 -2
- data/test/adapters/patron_test.rb +28 -8
- data/test/adapters/rack_test.rb +8 -1
- data/test/adapters/test_middleware_test.rb +46 -3
- data/test/adapters/typhoeus_test.rb +19 -9
- data/test/composite_read_io_test.rb +16 -18
- data/test/connection_test.rb +294 -78
- data/test/env_test.rb +55 -5
- data/test/helper.rb +11 -17
- data/test/middleware/retry_test.rb +115 -10
- data/test/middleware_stack_test.rb +97 -10
- data/test/options_test.rb +97 -16
- data/test/parameters_test.rb +94 -1
- data/test/request_middleware_test.rb +24 -40
- data/test/response_middleware_test.rb +4 -4
- data/test/utils_test.rb +40 -0
- metadata +21 -66
- data/.document +0 -6
- data/CONTRIBUTING.md +0 -36
- data/Gemfile +0 -25
- data/faraday.gemspec +0 -34
- data/script/cached-bundle +0 -46
- data/script/console +0 -7
- data/script/generate_certs +0 -42
- data/script/package +0 -7
- data/script/proxy-server +0 -42
- data/script/release +0 -17
- data/script/s3-put +0 -71
- data/script/server +0 -36
- data/script/test +0 -172
data/test/connection_test.rb
CHANGED
@@ -1,53 +1,90 @@
|
|
1
1
|
require File.expand_path('../helper', __FILE__)
|
2
2
|
|
3
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
|
4
42
|
|
5
|
-
def with_env(key, proxy)
|
6
|
-
old_value = ENV.fetch(key, false)
|
7
|
-
ENV[key] = proxy
|
8
43
|
begin
|
9
44
|
yield
|
10
45
|
ensure
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
46
|
+
old_env.each do |key, value|
|
47
|
+
if value == false
|
48
|
+
ENV.delete key
|
49
|
+
else
|
50
|
+
ENV[key] = value
|
51
|
+
end
|
15
52
|
end
|
16
53
|
end
|
17
54
|
end
|
18
55
|
|
19
56
|
def test_initialize_parses_host_out_of_given_url
|
20
|
-
conn = Faraday::Connection.new
|
57
|
+
conn = Faraday::Connection.new 'http://sushi.com'
|
21
58
|
assert_equal 'sushi.com', conn.host
|
22
59
|
end
|
23
60
|
|
24
61
|
def test_initialize_inherits_default_port_out_of_given_url
|
25
|
-
conn = Faraday::Connection.new
|
62
|
+
conn = Faraday::Connection.new 'http://sushi.com'
|
26
63
|
assert_equal 80, conn.port
|
27
64
|
end
|
28
65
|
|
29
66
|
def test_initialize_parses_scheme_out_of_given_url
|
30
|
-
conn = Faraday::Connection.new
|
67
|
+
conn = Faraday::Connection.new 'http://sushi.com'
|
31
68
|
assert_equal 'http', conn.scheme
|
32
69
|
end
|
33
70
|
|
34
71
|
def test_initialize_parses_port_out_of_given_url
|
35
|
-
conn = Faraday::Connection.new
|
72
|
+
conn = Faraday::Connection.new 'http://sushi.com:815'
|
36
73
|
assert_equal 815, conn.port
|
37
74
|
end
|
38
75
|
|
39
76
|
def test_initialize_parses_nil_path_prefix_out_of_given_url
|
40
|
-
conn = Faraday::Connection.new
|
77
|
+
conn = Faraday::Connection.new 'http://sushi.com'
|
41
78
|
assert_equal '/', conn.path_prefix
|
42
79
|
end
|
43
80
|
|
44
81
|
def test_initialize_parses_path_prefix_out_of_given_url
|
45
|
-
conn = Faraday::Connection.new
|
82
|
+
conn = Faraday::Connection.new 'http://sushi.com/fish'
|
46
83
|
assert_equal '/fish', conn.path_prefix
|
47
84
|
end
|
48
85
|
|
49
86
|
def test_initialize_parses_path_prefix_out_of_given_url_option
|
50
|
-
conn = Faraday::Connection.new :url =>
|
87
|
+
conn = Faraday::Connection.new :url => 'http://sushi.com/fish'
|
51
88
|
assert_equal '/fish', conn.path_prefix
|
52
89
|
end
|
53
90
|
|
@@ -57,12 +94,12 @@ class TestConnection < Faraday::TestCase
|
|
57
94
|
end
|
58
95
|
|
59
96
|
def test_initialize_stores_default_params_from_uri
|
60
|
-
conn = Faraday::Connection.new
|
97
|
+
conn = Faraday::Connection.new 'http://sushi.com/fish?a=1'
|
61
98
|
assert_equal({'a' => '1'}, conn.params)
|
62
99
|
end
|
63
100
|
|
64
101
|
def test_initialize_stores_default_params_from_uri_and_options
|
65
|
-
conn = Faraday::Connection.new
|
102
|
+
conn = Faraday::Connection.new 'http://sushi.com/fish?a=1&b=2', :params => {'a' => 3}
|
66
103
|
assert_equal({'a' => 3, 'b' => '2'}, conn.params)
|
67
104
|
end
|
68
105
|
|
@@ -81,9 +118,9 @@ class TestConnection < Faraday::TestCase
|
|
81
118
|
end
|
82
119
|
|
83
120
|
def test_auto_parses_basic_auth_from_url_and_unescapes
|
84
|
-
conn = Faraday::Connection.new :url =>
|
121
|
+
conn = Faraday::Connection.new :url => 'http://foo%40bar.com:pass%20word@sushi.com/fish'
|
85
122
|
assert auth = conn.headers['Authorization']
|
86
|
-
assert_equal Faraday::Request::BasicAuthentication.header(
|
123
|
+
assert_equal Faraday::Request::BasicAuthentication.header('foo@bar.com', 'pass word'), auth
|
87
124
|
end
|
88
125
|
|
89
126
|
def test_token_auth_sets_header
|
@@ -100,162 +137,176 @@ class TestConnection < Faraday::TestCase
|
|
100
137
|
def test_build_exclusive_url_uses_connection_host_as_default_uri_host
|
101
138
|
conn = Faraday::Connection.new
|
102
139
|
conn.host = 'sushi.com'
|
103
|
-
uri = conn.build_exclusive_url(
|
140
|
+
uri = conn.build_exclusive_url('/sake.html')
|
104
141
|
assert_equal 'sushi.com', uri.host
|
105
142
|
end
|
106
143
|
|
107
144
|
def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
|
108
145
|
conn = Faraday::Connection.new
|
109
146
|
conn.port = 23
|
110
|
-
uri = conn.build_exclusive_url(
|
147
|
+
uri = conn.build_exclusive_url('http://sushi.com')
|
111
148
|
assert_equal 80, uri.port
|
112
149
|
end
|
113
150
|
|
114
151
|
def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
|
115
152
|
conn = Faraday::Connection.new 'http://sushi.com'
|
116
|
-
uri = conn.build_exclusive_url(
|
153
|
+
uri = conn.build_exclusive_url('/sake.html')
|
117
154
|
assert_equal 'http', uri.scheme
|
118
155
|
end
|
119
156
|
|
120
157
|
def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
|
121
158
|
conn = Faraday::Connection.new
|
122
159
|
conn.path_prefix = '/fish'
|
123
|
-
uri = conn.build_exclusive_url(
|
160
|
+
uri = conn.build_exclusive_url('sake.html')
|
124
161
|
assert_equal '/fish/sake.html', uri.path
|
125
162
|
end
|
126
163
|
|
127
164
|
def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
|
128
165
|
conn = Faraday::Connection.new
|
129
166
|
conn.path_prefix = '/'
|
130
|
-
uri = conn.build_exclusive_url(
|
167
|
+
uri = conn.build_exclusive_url('sake.html')
|
131
168
|
assert_equal '/sake.html', uri.path
|
132
169
|
end
|
133
170
|
|
134
171
|
def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
|
135
172
|
conn = Faraday::Connection.new
|
136
173
|
conn.path_prefix = 'fish'
|
137
|
-
uri = conn.build_exclusive_url(
|
174
|
+
uri = conn.build_exclusive_url('sake.html')
|
138
175
|
assert_equal '/fish/sake.html', uri.path
|
139
176
|
end
|
140
177
|
|
141
178
|
def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
|
142
179
|
conn = Faraday::Connection.new
|
143
180
|
conn.path_prefix = '/fish/'
|
144
|
-
uri = conn.build_exclusive_url(
|
181
|
+
uri = conn.build_exclusive_url('sake.html')
|
145
182
|
assert_equal '/fish/sake.html', uri.path
|
146
183
|
end
|
147
184
|
|
148
185
|
def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
|
149
186
|
conn = Faraday::Connection.new
|
150
187
|
conn.path_prefix = '/fish'
|
151
|
-
uri = conn.build_exclusive_url(
|
188
|
+
uri = conn.build_exclusive_url('/sake.html')
|
152
189
|
assert_equal '/sake.html', uri.path
|
153
190
|
end
|
154
191
|
|
155
192
|
def test_build_exclusive_url_parses_url_params_into_path
|
156
193
|
conn = Faraday::Connection.new
|
157
|
-
uri = conn.build_exclusive_url(
|
194
|
+
uri = conn.build_exclusive_url('http://sushi.com/sake.html')
|
158
195
|
assert_equal '/sake.html', uri.path
|
159
196
|
end
|
160
197
|
|
161
198
|
def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url
|
162
199
|
conn = Faraday::Connection.new
|
163
|
-
conn.url_prefix =
|
200
|
+
conn.url_prefix = 'http://sushi.com/nigiri'
|
164
201
|
uri = conn.build_exclusive_url
|
165
|
-
assert_equal
|
202
|
+
assert_equal '/nigiri', uri.path
|
166
203
|
end
|
167
204
|
|
168
205
|
def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url
|
169
206
|
conn = Faraday::Connection.new
|
170
|
-
conn.url_prefix =
|
207
|
+
conn.url_prefix = 'http://sushi.com/nigiri'
|
171
208
|
uri = conn.build_exclusive_url('')
|
172
|
-
assert_equal
|
209
|
+
assert_equal '/nigiri', uri.path
|
173
210
|
end
|
174
211
|
|
175
212
|
def test_build_exclusive_url_doesnt_use_connection_params
|
176
|
-
conn = Faraday::Connection.new
|
213
|
+
conn = Faraday::Connection.new 'http://sushi.com/nigiri'
|
177
214
|
conn.params = {:a => 1}
|
178
|
-
assert_equal
|
215
|
+
assert_equal 'http://sushi.com/nigiri', conn.build_exclusive_url.to_s
|
179
216
|
end
|
180
217
|
|
181
218
|
def test_build_exclusive_url_uses_argument_params
|
182
|
-
conn = Faraday::Connection.new
|
219
|
+
conn = Faraday::Connection.new 'http://sushi.com/nigiri'
|
183
220
|
conn.params = {:a => 1}
|
184
221
|
params = Faraday::Utils::ParamsHash.new
|
185
222
|
params[:a] = 2
|
186
223
|
url = conn.build_exclusive_url(nil, params)
|
187
|
-
assert_equal
|
224
|
+
assert_equal 'http://sushi.com/nigiri?a=2', url.to_s
|
188
225
|
end
|
189
226
|
|
190
227
|
def test_build_url_uses_params
|
191
|
-
conn = Faraday::Connection.new
|
228
|
+
conn = Faraday::Connection.new 'http://sushi.com/nigiri'
|
192
229
|
conn.params = {:a => 1, :b => 1}
|
193
|
-
assert_equal
|
230
|
+
assert_equal 'http://sushi.com/nigiri?a=1&b=1', conn.build_url.to_s
|
194
231
|
end
|
195
232
|
|
196
233
|
def test_build_url_merges_params
|
197
|
-
conn = Faraday::Connection.new
|
234
|
+
conn = Faraday::Connection.new 'http://sushi.com/nigiri'
|
198
235
|
conn.params = {:a => 1, :b => 1}
|
199
236
|
url = conn.build_url(nil, :b => 2, :c => 3)
|
200
|
-
assert_equal
|
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')
|
201
252
|
end
|
202
253
|
|
203
254
|
def test_env_url_parses_url_params_into_query
|
204
|
-
uri = env_url(
|
205
|
-
assert_equal
|
255
|
+
uri = env_url('http://sushi.com/sake.html', 'a[b]' => '1 + 2')
|
256
|
+
assert_equal 'a%5Bb%5D=1+%2B+2', uri.query
|
206
257
|
end
|
207
258
|
|
208
259
|
def test_env_url_escapes_per_spec
|
209
|
-
uri = env_url(
|
210
|
-
assert_equal
|
260
|
+
uri = env_url(nil, 'a' => '1+2 foo~bar.-baz')
|
261
|
+
assert_equal 'a=1%2B2+foo~bar.-baz', uri.query
|
211
262
|
end
|
212
263
|
|
213
264
|
def test_env_url_bracketizes_nested_params_in_query
|
214
265
|
url = env_url nil, 'a' => {'b' => 'c'}
|
215
|
-
assert_equal
|
266
|
+
assert_equal 'a%5Bb%5D=c', url.query
|
216
267
|
end
|
217
268
|
|
218
269
|
def test_env_url_bracketizes_repeated_params_in_query
|
219
|
-
uri = env_url(
|
220
|
-
assert_equal
|
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
|
221
272
|
end
|
222
273
|
|
223
274
|
def test_env_url_without_braketizing_repeated_params_in_query
|
224
275
|
uri = env_url 'http://sushi.com', 'a' => [1, 2] do |conn|
|
225
276
|
conn.options.params_encoder = Faraday::FlatParamsEncoder
|
226
277
|
end
|
227
|
-
assert_equal
|
278
|
+
assert_equal 'a=1&a=2', uri.query
|
228
279
|
end
|
229
280
|
|
230
281
|
def test_build_exclusive_url_parses_url
|
231
282
|
conn = Faraday::Connection.new
|
232
|
-
uri = conn.build_exclusive_url(
|
233
|
-
assert_equal
|
234
|
-
assert_equal
|
283
|
+
uri = conn.build_exclusive_url('http://sushi.com/sake.html')
|
284
|
+
assert_equal 'http', uri.scheme
|
285
|
+
assert_equal 'sushi.com', uri.host
|
235
286
|
assert_equal '/sake.html', uri.path
|
236
287
|
end
|
237
288
|
|
238
289
|
def test_build_exclusive_url_parses_url_and_changes_scheme
|
239
|
-
conn = Faraday::Connection.new :url =>
|
290
|
+
conn = Faraday::Connection.new :url => 'http://sushi.com/sushi'
|
240
291
|
conn.scheme = 'https'
|
241
|
-
uri = conn.build_exclusive_url(
|
292
|
+
uri = conn.build_exclusive_url('sake.html')
|
242
293
|
assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
|
243
294
|
end
|
244
295
|
|
245
296
|
def test_build_exclusive_url_joins_url_to_base_with_ending_slash
|
246
|
-
conn = Faraday::Connection.new :url =>
|
247
|
-
uri = conn.build_exclusive_url(
|
297
|
+
conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
|
298
|
+
uri = conn.build_exclusive_url('sake.html')
|
248
299
|
assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s
|
249
300
|
end
|
250
301
|
|
251
302
|
def test_build_exclusive_url_used_default_base_with_ending_slash
|
252
|
-
conn = Faraday::Connection.new :url =>
|
303
|
+
conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
|
253
304
|
uri = conn.build_exclusive_url
|
254
305
|
assert_equal 'http://sushi.com/sushi/', uri.to_s
|
255
306
|
end
|
256
307
|
|
257
308
|
def test_build_exclusive_url_overrides_base
|
258
|
-
conn = Faraday::Connection.new :url =>
|
309
|
+
conn = Faraday::Connection.new :url => 'http://sushi.com/sushi/'
|
259
310
|
uri = conn.build_exclusive_url('/sake/')
|
260
311
|
assert_equal 'http://sushi.com/sake/', uri.to_s
|
261
312
|
end
|
@@ -267,48 +318,48 @@ class TestConnection < Faraday::TestCase
|
|
267
318
|
end
|
268
319
|
|
269
320
|
def test_proxy_accepts_string
|
270
|
-
with_env 'http_proxy'
|
321
|
+
with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
|
271
322
|
conn = Faraday::Connection.new
|
272
|
-
conn.proxy 'http://proxy.com'
|
323
|
+
conn.proxy = 'http://proxy.com'
|
273
324
|
assert_equal 'proxy.com', conn.proxy.host
|
274
325
|
end
|
275
326
|
end
|
276
327
|
|
277
328
|
def test_proxy_accepts_uri
|
278
|
-
with_env 'http_proxy'
|
329
|
+
with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
|
279
330
|
conn = Faraday::Connection.new
|
280
|
-
conn.proxy URI.parse('http://proxy.com')
|
331
|
+
conn.proxy = URI.parse('http://proxy.com')
|
281
332
|
assert_equal 'proxy.com', conn.proxy.host
|
282
333
|
end
|
283
334
|
end
|
284
335
|
|
285
336
|
def test_proxy_accepts_hash_with_string_uri
|
286
|
-
with_env 'http_proxy'
|
337
|
+
with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
|
287
338
|
conn = Faraday::Connection.new
|
288
|
-
conn.proxy :uri => 'http://proxy.com', :user => 'rick'
|
339
|
+
conn.proxy = {:uri => 'http://proxy.com', :user => 'rick'}
|
289
340
|
assert_equal 'proxy.com', conn.proxy.host
|
290
341
|
assert_equal 'rick', conn.proxy.user
|
291
342
|
end
|
292
343
|
end
|
293
344
|
|
294
345
|
def test_proxy_accepts_hash
|
295
|
-
with_env 'http_proxy'
|
346
|
+
with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
|
296
347
|
conn = Faraday::Connection.new
|
297
|
-
conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
|
348
|
+
conn.proxy = {:uri => URI.parse('http://proxy.com'), :user => 'rick'}
|
298
349
|
assert_equal 'proxy.com', conn.proxy.host
|
299
350
|
assert_equal 'rick', conn.proxy.user
|
300
351
|
end
|
301
352
|
end
|
302
353
|
|
303
354
|
def test_proxy_accepts_http_env
|
304
|
-
with_env 'http_proxy'
|
355
|
+
with_env 'http_proxy' => 'http://duncan.proxy.com:80' do
|
305
356
|
conn = Faraday::Connection.new
|
306
357
|
assert_equal 'duncan.proxy.com', conn.proxy.host
|
307
358
|
end
|
308
359
|
end
|
309
360
|
|
310
361
|
def test_proxy_accepts_http_env_with_auth
|
311
|
-
with_env 'http_proxy'
|
362
|
+
with_env 'http_proxy' => 'http://a%40b:my%20pass@duncan.proxy.com:80' do
|
312
363
|
conn = Faraday::Connection.new
|
313
364
|
assert_equal 'a@b', conn.proxy.user
|
314
365
|
assert_equal 'my pass', conn.proxy.password
|
@@ -316,7 +367,7 @@ class TestConnection < Faraday::TestCase
|
|
316
367
|
end
|
317
368
|
|
318
369
|
def test_proxy_accepts_env_without_scheme
|
319
|
-
with_env 'http_proxy'
|
370
|
+
with_env 'http_proxy' => 'localhost:8888' do
|
320
371
|
uri = Faraday::Connection.new.proxy[:uri]
|
321
372
|
assert_equal 'localhost', uri.host
|
322
373
|
assert_equal 8888, uri.port
|
@@ -324,30 +375,123 @@ class TestConnection < Faraday::TestCase
|
|
324
375
|
end
|
325
376
|
|
326
377
|
def test_no_proxy_from_env
|
327
|
-
with_env 'http_proxy'
|
378
|
+
with_env 'http_proxy' => nil do
|
328
379
|
conn = Faraday::Connection.new
|
329
|
-
|
380
|
+
assert_nil conn.proxy
|
330
381
|
end
|
331
382
|
end
|
332
383
|
|
333
384
|
def test_no_proxy_from_blank_env
|
334
|
-
with_env 'http_proxy'
|
385
|
+
with_env 'http_proxy' => '' do
|
335
386
|
conn = Faraday::Connection.new
|
336
|
-
|
387
|
+
assert_nil conn.proxy
|
337
388
|
end
|
338
389
|
end
|
339
390
|
|
340
391
|
def test_proxy_doesnt_accept_uppercase_env
|
341
|
-
with_env 'HTTP_PROXY'
|
392
|
+
with_env 'HTTP_PROXY' => 'http://localhost:8888/' do
|
342
393
|
conn = Faraday::Connection.new
|
343
394
|
assert_nil conn.proxy
|
344
395
|
end
|
345
396
|
end
|
346
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
|
+
|
347
491
|
def test_proxy_requires_uri
|
348
492
|
conn = Faraday::Connection.new
|
349
493
|
assert_raises ArgumentError do
|
350
|
-
conn.proxy :uri => :bad_uri, :user => 'rick'
|
494
|
+
conn.proxy = {:uri => :bad_uri, :user => 'rick'}
|
351
495
|
end
|
352
496
|
end
|
353
497
|
|
@@ -355,7 +499,8 @@ class TestConnection < Faraday::TestCase
|
|
355
499
|
conn = Faraday::Connection.new 'http://sushi.com/foo',
|
356
500
|
:ssl => { :verify => :none },
|
357
501
|
:headers => {'content-type' => 'text/plain'},
|
358
|
-
:params => {'a'=>'1'}
|
502
|
+
:params => {'a'=>'1'},
|
503
|
+
:request => {:timeout => 5}
|
359
504
|
|
360
505
|
other = conn.dup
|
361
506
|
|
@@ -366,11 +511,14 @@ class TestConnection < Faraday::TestCase
|
|
366
511
|
other.basic_auth('', '')
|
367
512
|
other.headers['content-length'] = 12
|
368
513
|
other.params['b'] = '2'
|
514
|
+
other.options[:open_timeout] = 10
|
369
515
|
|
370
516
|
assert_equal 2, other.builder.handlers.size
|
371
517
|
assert_equal 2, conn.builder.handlers.size
|
372
518
|
assert !conn.headers.key?('content-length')
|
373
519
|
assert !conn.params.key?('b')
|
520
|
+
assert_equal 5, other.options[:timeout]
|
521
|
+
assert_nil conn.options[:open_timeout]
|
374
522
|
end
|
375
523
|
|
376
524
|
def test_initialize_with_false_option
|
@@ -393,6 +541,61 @@ class TestConnection < Faraday::TestCase
|
|
393
541
|
assert_equal '/omnom', conn.path_prefix
|
394
542
|
end
|
395
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
|
+
|
396
599
|
def env_url(url, params)
|
397
600
|
conn = Faraday::Connection.new(url, :params => params)
|
398
601
|
yield(conn) if block_given?
|
@@ -485,7 +688,7 @@ class TestRequestParams < Faraday::TestCase
|
|
485
688
|
with_default_params_encoder(nil) do
|
486
689
|
create_connection 'http://a.co/page1?color[]=red&color[]=blue'
|
487
690
|
query = get
|
488
|
-
assert_equal
|
691
|
+
assert_equal 'color%5B%5D=red&color%5B%5D=blue', query
|
489
692
|
end
|
490
693
|
end
|
491
694
|
|
@@ -493,7 +696,7 @@ class TestRequestParams < Faraday::TestCase
|
|
493
696
|
with_default_params_encoder(nil) do
|
494
697
|
create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
|
495
698
|
query = get
|
496
|
-
assert_equal
|
699
|
+
assert_equal 'color%5B%5D=red&color%5B%5D=blue', query
|
497
700
|
end
|
498
701
|
end
|
499
702
|
|
@@ -501,7 +704,7 @@ class TestRequestParams < Faraday::TestCase
|
|
501
704
|
with_default_params_encoder(Faraday::FlatParamsEncoder) do
|
502
705
|
create_connection 'http://a.co/page1?color=red&color=blue'
|
503
706
|
query = get
|
504
|
-
assert_equal
|
707
|
+
assert_equal 'color=red&color=blue', query
|
505
708
|
end
|
506
709
|
end
|
507
710
|
|
@@ -509,8 +712,21 @@ class TestRequestParams < Faraday::TestCase
|
|
509
712
|
with_default_params_encoder(Faraday::FlatParamsEncoder) do
|
510
713
|
create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
|
511
714
|
query = get
|
512
|
-
assert_equal
|
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
|
513
728
|
end
|
729
|
+
assert_equal ['COLOR-RED', 'FEELING-BLUE'], query.split(',').sort
|
514
730
|
end
|
515
731
|
|
516
732
|
def get(*args)
|