faraday 0.6.0 → 0.9.0
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.
- checksums.yaml +7 -0
- data/.document +6 -0
- data/CHANGELOG.md +15 -0
- data/CONTRIBUTING.md +36 -0
- data/Gemfile +25 -15
- data/{LICENSE → LICENSE.md} +1 -1
- data/README.md +187 -137
- data/Rakefile +38 -101
- data/faraday.gemspec +26 -81
- data/lib/faraday/adapter/em_http.rb +237 -0
- data/lib/faraday/adapter/em_http_ssl_patch.rb +56 -0
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
- data/lib/faraday/adapter/em_synchrony.rb +65 -38
- data/lib/faraday/adapter/excon.rb +60 -9
- data/lib/faraday/adapter/httpclient.rb +106 -0
- data/lib/faraday/adapter/net_http.rb +89 -38
- data/lib/faraday/adapter/net_http_persistent.rb +47 -0
- data/lib/faraday/adapter/patron.rb +46 -9
- data/lib/faraday/adapter/rack.rb +58 -0
- data/lib/faraday/adapter/test.rb +68 -28
- data/lib/faraday/adapter/typhoeus.rb +94 -16
- data/lib/faraday/adapter.rb +33 -25
- data/lib/faraday/autoload.rb +85 -0
- data/lib/faraday/connection.rb +342 -134
- data/lib/faraday/error.rb +44 -29
- data/lib/faraday/middleware.rb +18 -10
- data/lib/faraday/options.rb +350 -0
- data/lib/faraday/parameters.rb +193 -0
- data/lib/faraday/rack_builder.rb +212 -0
- data/lib/faraday/request/authorization.rb +42 -0
- data/lib/faraday/request/basic_authentication.rb +13 -0
- data/lib/faraday/request/instrumentation.rb +36 -0
- data/lib/faraday/request/multipart.rb +15 -15
- data/lib/faraday/request/retry.rb +118 -0
- data/lib/faraday/request/token_authentication.rb +15 -0
- data/lib/faraday/request/url_encoded.rb +8 -9
- data/lib/faraday/request.rb +46 -45
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +8 -3
- data/lib/faraday/response.rb +20 -25
- data/lib/faraday/upload_io.rb +51 -7
- data/lib/faraday/utils.rb +240 -44
- data/lib/faraday.rb +218 -38
- data/script/console +7 -0
- data/script/generate_certs +42 -0
- data/script/package +7 -0
- data/script/proxy-server +42 -0
- data/script/release +17 -0
- data/script/server +36 -0
- data/script/test +172 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +20 -0
- data/test/adapters/em_synchrony_test.rb +20 -0
- data/test/adapters/excon_test.rb +20 -0
- data/test/adapters/httpclient_test.rb +21 -0
- data/test/adapters/integration.rb +254 -0
- data/test/adapters/logger_test.rb +3 -3
- data/test/adapters/net_http_persistent_test.rb +20 -0
- data/test/adapters/net_http_test.rb +6 -25
- data/test/adapters/patron_test.rb +20 -0
- data/test/adapters/rack_test.rb +31 -0
- data/test/adapters/test_middleware_test.rb +74 -3
- data/test/adapters/typhoeus_test.rb +28 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +111 -0
- data/test/connection_test.rb +381 -104
- data/test/env_test.rb +116 -40
- data/test/helper.rb +61 -25
- data/test/live_server.rb +55 -29
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +109 -0
- data/test/middleware_stack_test.rb +87 -5
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +252 -0
- data/test/request_middleware_test.rb +78 -21
- data/test/response_middleware_test.rb +32 -7
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +58 -0
- metadata +116 -117
- data/lib/faraday/adapter/action_dispatch.rb +0 -30
- data/lib/faraday/builder.rb +0 -137
- data/lib/faraday/request/json.rb +0 -31
- data/test/adapters/live_test.rb +0 -186
data/test/connection_test.rb
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
|
-
require File.expand_path(
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
|
2
2
|
|
|
3
3
|
class TestConnection < Faraday::TestCase
|
|
4
|
+
|
|
5
|
+
def with_env(key, proxy)
|
|
6
|
+
old_value = ENV.fetch(key, false)
|
|
7
|
+
ENV[key] = proxy
|
|
8
|
+
begin
|
|
9
|
+
yield
|
|
10
|
+
ensure
|
|
11
|
+
if old_value == false
|
|
12
|
+
ENV.delete key
|
|
13
|
+
else
|
|
14
|
+
ENV[key] = old_value
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
4
19
|
def test_initialize_parses_host_out_of_given_url
|
|
5
20
|
conn = Faraday::Connection.new "http://sushi.com"
|
|
6
21
|
assert_equal 'sushi.com', conn.host
|
|
7
22
|
end
|
|
8
23
|
|
|
9
|
-
def
|
|
24
|
+
def test_initialize_inherits_default_port_out_of_given_url
|
|
10
25
|
conn = Faraday::Connection.new "http://sushi.com"
|
|
11
|
-
|
|
26
|
+
assert_equal 80, conn.port
|
|
12
27
|
end
|
|
13
28
|
|
|
14
29
|
def test_initialize_parses_scheme_out_of_given_url
|
|
@@ -38,179 +53,295 @@ class TestConnection < Faraday::TestCase
|
|
|
38
53
|
|
|
39
54
|
def test_initialize_stores_default_params_from_options
|
|
40
55
|
conn = Faraday::Connection.new :params => {:a => 1}
|
|
41
|
-
assert_equal 1, conn.params
|
|
56
|
+
assert_equal({'a' => 1}, conn.params)
|
|
42
57
|
end
|
|
43
58
|
|
|
44
59
|
def test_initialize_stores_default_params_from_uri
|
|
45
|
-
conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
|
|
46
|
-
assert_equal '1', conn.params
|
|
47
|
-
assert_equal '2', conn.params['b']
|
|
60
|
+
conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
|
|
61
|
+
assert_equal({'a' => '1'}, conn.params)
|
|
48
62
|
end
|
|
49
63
|
|
|
50
|
-
def
|
|
51
|
-
conn = Faraday::Connection.new :
|
|
52
|
-
assert_equal '
|
|
64
|
+
def test_initialize_stores_default_params_from_uri_and_options
|
|
65
|
+
conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}
|
|
66
|
+
assert_equal({'a' => 3, 'b' => '2'}, conn.params)
|
|
53
67
|
end
|
|
54
68
|
|
|
55
|
-
def
|
|
56
|
-
conn = Faraday::Connection.new
|
|
57
|
-
|
|
58
|
-
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
|
|
69
|
+
def test_initialize_stores_default_headers_from_options
|
|
70
|
+
conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
|
|
71
|
+
assert_equal 'Faraday', conn.headers['User-agent']
|
|
59
72
|
end
|
|
60
73
|
|
|
61
|
-
def
|
|
74
|
+
def test_basic_auth_sets_header
|
|
62
75
|
conn = Faraday::Connection.new
|
|
63
|
-
conn.
|
|
64
|
-
assert_equal "Basic #{'QUFB' * 85}Og==", conn.headers['Authorization']
|
|
65
|
-
end
|
|
76
|
+
assert_nil conn.headers['Authorization']
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
assert_equal 'Basic
|
|
78
|
+
conn.basic_auth 'Aladdin', 'open sesame'
|
|
79
|
+
assert auth = conn.headers['Authorization']
|
|
80
|
+
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
|
|
70
81
|
end
|
|
71
82
|
|
|
72
|
-
def
|
|
73
|
-
conn = Faraday::Connection.new
|
|
74
|
-
conn.
|
|
75
|
-
assert_equal
|
|
83
|
+
def test_auto_parses_basic_auth_from_url_and_unescapes
|
|
84
|
+
conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
|
|
85
|
+
assert auth = conn.headers['Authorization']
|
|
86
|
+
assert_equal Faraday::Request::BasicAuthentication.header("foo@bar.com", "pass word"), auth
|
|
76
87
|
end
|
|
77
88
|
|
|
78
|
-
def
|
|
89
|
+
def test_token_auth_sets_header
|
|
79
90
|
conn = Faraday::Connection.new
|
|
91
|
+
assert_nil conn.headers['Authorization']
|
|
92
|
+
|
|
80
93
|
conn.token_auth 'abcdef', :nonce => 'abc'
|
|
81
|
-
|
|
82
|
-
|
|
94
|
+
assert auth = conn.headers['Authorization']
|
|
95
|
+
assert_match(/^Token /, auth)
|
|
96
|
+
assert_match(/token="abcdef"/, auth)
|
|
97
|
+
assert_match(/nonce="abc"/, auth)
|
|
83
98
|
end
|
|
84
99
|
|
|
85
|
-
def
|
|
100
|
+
def test_build_exclusive_url_uses_connection_host_as_default_uri_host
|
|
86
101
|
conn = Faraday::Connection.new
|
|
87
102
|
conn.host = 'sushi.com'
|
|
88
|
-
uri = conn.
|
|
103
|
+
uri = conn.build_exclusive_url("/sake.html")
|
|
89
104
|
assert_equal 'sushi.com', uri.host
|
|
90
105
|
end
|
|
91
106
|
|
|
92
|
-
def
|
|
107
|
+
def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
|
|
93
108
|
conn = Faraday::Connection.new
|
|
94
109
|
conn.port = 23
|
|
95
|
-
uri = conn.
|
|
96
|
-
assert_equal
|
|
110
|
+
uri = conn.build_exclusive_url("http://sushi.com")
|
|
111
|
+
assert_equal 80, uri.port
|
|
97
112
|
end
|
|
98
113
|
|
|
99
|
-
def
|
|
114
|
+
def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
|
|
100
115
|
conn = Faraday::Connection.new 'http://sushi.com'
|
|
101
|
-
uri = conn.
|
|
116
|
+
uri = conn.build_exclusive_url("/sake.html")
|
|
102
117
|
assert_equal 'http', uri.scheme
|
|
103
118
|
end
|
|
104
119
|
|
|
105
|
-
def
|
|
120
|
+
def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
|
|
106
121
|
conn = Faraday::Connection.new
|
|
107
122
|
conn.path_prefix = '/fish'
|
|
108
|
-
uri = conn.
|
|
123
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
109
124
|
assert_equal '/fish/sake.html', uri.path
|
|
110
125
|
end
|
|
111
126
|
|
|
112
|
-
def
|
|
127
|
+
def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
|
|
113
128
|
conn = Faraday::Connection.new
|
|
114
129
|
conn.path_prefix = '/'
|
|
115
|
-
uri = conn.
|
|
130
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
116
131
|
assert_equal '/sake.html', uri.path
|
|
117
132
|
end
|
|
118
133
|
|
|
119
|
-
def
|
|
134
|
+
def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
|
|
120
135
|
conn = Faraday::Connection.new
|
|
121
136
|
conn.path_prefix = 'fish'
|
|
122
|
-
uri = conn.
|
|
137
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
123
138
|
assert_equal '/fish/sake.html', uri.path
|
|
124
139
|
end
|
|
125
140
|
|
|
126
|
-
def
|
|
141
|
+
def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
|
|
127
142
|
conn = Faraday::Connection.new
|
|
128
143
|
conn.path_prefix = '/fish/'
|
|
129
|
-
uri = conn.
|
|
144
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
130
145
|
assert_equal '/fish/sake.html', uri.path
|
|
131
146
|
end
|
|
132
147
|
|
|
133
|
-
def
|
|
148
|
+
def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
|
|
134
149
|
conn = Faraday::Connection.new
|
|
135
150
|
conn.path_prefix = '/fish'
|
|
136
|
-
uri = conn.
|
|
151
|
+
uri = conn.build_exclusive_url("/sake.html")
|
|
137
152
|
assert_equal '/sake.html', uri.path
|
|
138
153
|
end
|
|
139
154
|
|
|
140
|
-
def
|
|
155
|
+
def test_build_exclusive_url_parses_url_params_into_path
|
|
141
156
|
conn = Faraday::Connection.new
|
|
142
|
-
uri = conn.
|
|
157
|
+
uri = conn.build_exclusive_url("http://sushi.com/sake.html")
|
|
143
158
|
assert_equal '/sake.html', uri.path
|
|
144
159
|
end
|
|
145
160
|
|
|
146
|
-
def
|
|
161
|
+
def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url
|
|
162
|
+
conn = Faraday::Connection.new
|
|
163
|
+
conn.url_prefix = "http://sushi.com/nigiri"
|
|
164
|
+
uri = conn.build_exclusive_url
|
|
165
|
+
assert_equal "/nigiri", uri.path
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url
|
|
147
169
|
conn = Faraday::Connection.new
|
|
148
|
-
|
|
149
|
-
|
|
170
|
+
conn.url_prefix = "http://sushi.com/nigiri"
|
|
171
|
+
uri = conn.build_exclusive_url('')
|
|
172
|
+
assert_equal "/nigiri", uri.path
|
|
150
173
|
end
|
|
151
174
|
|
|
152
|
-
def
|
|
153
|
-
conn = Faraday::Connection.new
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
assert_match /page=1/, url.query
|
|
157
|
-
assert_match /format=json/, url.query
|
|
158
|
-
assert_match /token=abc/, url.query
|
|
175
|
+
def test_build_exclusive_url_doesnt_use_connection_params
|
|
176
|
+
conn = Faraday::Connection.new "http://sushi.com/nigiri"
|
|
177
|
+
conn.params = {:a => 1}
|
|
178
|
+
assert_equal "http://sushi.com/nigiri", conn.build_exclusive_url.to_s
|
|
159
179
|
end
|
|
160
180
|
|
|
161
|
-
def
|
|
162
|
-
conn = Faraday::Connection.new
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
181
|
+
def test_build_exclusive_url_uses_argument_params
|
|
182
|
+
conn = Faraday::Connection.new "http://sushi.com/nigiri"
|
|
183
|
+
conn.params = {:a => 1}
|
|
184
|
+
params = Faraday::Utils::ParamsHash.new
|
|
185
|
+
params[:a] = 2
|
|
186
|
+
url = conn.build_exclusive_url(nil, params)
|
|
187
|
+
assert_equal "http://sushi.com/nigiri?a=2", url.to_s
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def test_build_url_uses_params
|
|
191
|
+
conn = Faraday::Connection.new "http://sushi.com/nigiri"
|
|
192
|
+
conn.params = {:a => 1, :b => 1}
|
|
193
|
+
assert_equal "http://sushi.com/nigiri?a=1&b=1", conn.build_url.to_s
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def test_build_url_merges_params
|
|
197
|
+
conn = Faraday::Connection.new "http://sushi.com/nigiri"
|
|
198
|
+
conn.params = {:a => 1, :b => 1}
|
|
199
|
+
url = conn.build_url(nil, :b => 2, :c => 3)
|
|
200
|
+
assert_equal "http://sushi.com/nigiri?a=1&b=2&c=3", url.to_s
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def test_env_url_parses_url_params_into_query
|
|
204
|
+
uri = env_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
|
|
205
|
+
assert_equal "a%5Bb%5D=1+%2B+2", uri.query
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def test_env_url_escapes_per_spec
|
|
209
|
+
uri = env_url('http:/', 'a' => '1+2 foo~bar.-baz')
|
|
210
|
+
assert_equal "a=1%2B2+foo~bar.-baz", uri.query
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def test_env_url_bracketizes_nested_params_in_query
|
|
214
|
+
url = env_url nil, 'a' => {'b' => 'c'}
|
|
215
|
+
assert_equal "a%5Bb%5D=c", url.query
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def test_env_url_bracketizes_repeated_params_in_query
|
|
219
|
+
uri = env_url("http://sushi.com/sake.html", 'a' => [1, 2])
|
|
220
|
+
assert_equal "a%5B%5D=1&a%5B%5D=2", uri.query
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def test_env_url_without_braketizing_repeated_params_in_query
|
|
224
|
+
uri = env_url 'http://sushi.com', 'a' => [1, 2] do |conn|
|
|
225
|
+
conn.options.params_encoder = Faraday::FlatParamsEncoder
|
|
226
|
+
end
|
|
227
|
+
assert_equal "a=1&a=2", uri.query
|
|
170
228
|
end
|
|
171
229
|
|
|
172
|
-
def
|
|
230
|
+
def test_build_exclusive_url_parses_url
|
|
173
231
|
conn = Faraday::Connection.new
|
|
174
|
-
uri = conn.
|
|
175
|
-
assert_equal "http",
|
|
176
|
-
assert_equal "sushi.com",
|
|
232
|
+
uri = conn.build_exclusive_url("http://sushi.com/sake.html")
|
|
233
|
+
assert_equal "http", uri.scheme
|
|
234
|
+
assert_equal "sushi.com", uri.host
|
|
177
235
|
assert_equal '/sake.html', uri.path
|
|
178
|
-
assert_nil uri.port
|
|
179
236
|
end
|
|
180
237
|
|
|
181
|
-
def
|
|
238
|
+
def test_build_exclusive_url_parses_url_and_changes_scheme
|
|
182
239
|
conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
|
|
183
240
|
conn.scheme = 'https'
|
|
184
|
-
uri = conn.
|
|
241
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
185
242
|
assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
|
|
186
243
|
end
|
|
187
244
|
|
|
188
|
-
def
|
|
245
|
+
def test_build_exclusive_url_joins_url_to_base_with_ending_slash
|
|
246
|
+
conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
|
|
247
|
+
uri = conn.build_exclusive_url("sake.html")
|
|
248
|
+
assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def test_build_exclusive_url_used_default_base_with_ending_slash
|
|
252
|
+
conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
|
|
253
|
+
uri = conn.build_exclusive_url
|
|
254
|
+
assert_equal 'http://sushi.com/sushi/', uri.to_s
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def test_build_exclusive_url_overrides_base
|
|
258
|
+
conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
|
|
259
|
+
uri = conn.build_exclusive_url('/sake/')
|
|
260
|
+
assert_equal 'http://sushi.com/sake/', uri.to_s
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def test_build_exclusive_url_handles_uri_instances
|
|
189
264
|
conn = Faraday::Connection.new
|
|
190
|
-
conn.
|
|
191
|
-
assert_equal '
|
|
192
|
-
|
|
265
|
+
uri = conn.build_exclusive_url(URI('/sake.html'))
|
|
266
|
+
assert_equal '/sake.html', uri.path
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def test_proxy_accepts_string
|
|
270
|
+
with_env 'http_proxy', "http://duncan.proxy.com:80" do
|
|
271
|
+
conn = Faraday::Connection.new
|
|
272
|
+
conn.proxy 'http://proxy.com'
|
|
273
|
+
assert_equal 'proxy.com', conn.proxy.host
|
|
274
|
+
end
|
|
193
275
|
end
|
|
194
276
|
|
|
195
277
|
def test_proxy_accepts_uri
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
278
|
+
with_env 'http_proxy', "http://duncan.proxy.com:80" do
|
|
279
|
+
conn = Faraday::Connection.new
|
|
280
|
+
conn.proxy URI.parse('http://proxy.com')
|
|
281
|
+
assert_equal 'proxy.com', conn.proxy.host
|
|
282
|
+
end
|
|
200
283
|
end
|
|
201
284
|
|
|
202
285
|
def test_proxy_accepts_hash_with_string_uri
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
286
|
+
with_env 'http_proxy', "http://duncan.proxy.com:80" do
|
|
287
|
+
conn = Faraday::Connection.new
|
|
288
|
+
conn.proxy :uri => 'http://proxy.com', :user => 'rick'
|
|
289
|
+
assert_equal 'proxy.com', conn.proxy.host
|
|
290
|
+
assert_equal 'rick', conn.proxy.user
|
|
291
|
+
end
|
|
207
292
|
end
|
|
208
293
|
|
|
209
294
|
def test_proxy_accepts_hash
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
295
|
+
with_env 'http_proxy', "http://duncan.proxy.com:80" do
|
|
296
|
+
conn = Faraday::Connection.new
|
|
297
|
+
conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
|
|
298
|
+
assert_equal 'proxy.com', conn.proxy.host
|
|
299
|
+
assert_equal 'rick', conn.proxy.user
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def test_proxy_accepts_http_env
|
|
304
|
+
with_env 'http_proxy', "http://duncan.proxy.com:80" do
|
|
305
|
+
conn = Faraday::Connection.new
|
|
306
|
+
assert_equal 'duncan.proxy.com', conn.proxy.host
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def test_proxy_accepts_http_env_with_auth
|
|
311
|
+
with_env 'http_proxy', "http://a%40b:my%20pass@duncan.proxy.com:80" do
|
|
312
|
+
conn = Faraday::Connection.new
|
|
313
|
+
assert_equal 'a@b', conn.proxy.user
|
|
314
|
+
assert_equal 'my pass', conn.proxy.password
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def test_proxy_accepts_env_without_scheme
|
|
319
|
+
with_env 'http_proxy', "localhost:8888" do
|
|
320
|
+
uri = Faraday::Connection.new.proxy[:uri]
|
|
321
|
+
assert_equal 'localhost', uri.host
|
|
322
|
+
assert_equal 8888, uri.port
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def test_no_proxy_from_env
|
|
327
|
+
with_env 'http_proxy', nil do
|
|
328
|
+
conn = Faraday::Connection.new
|
|
329
|
+
assert_equal nil, conn.proxy
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def test_no_proxy_from_blank_env
|
|
334
|
+
with_env 'http_proxy', '' do
|
|
335
|
+
conn = Faraday::Connection.new
|
|
336
|
+
assert_equal nil, conn.proxy
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def test_proxy_doesnt_accept_uppercase_env
|
|
341
|
+
with_env 'HTTP_PROXY', "http://localhost:8888/" do
|
|
342
|
+
conn = Faraday::Connection.new
|
|
343
|
+
assert_nil conn.proxy
|
|
344
|
+
end
|
|
214
345
|
end
|
|
215
346
|
|
|
216
347
|
def test_proxy_requires_uri
|
|
@@ -220,26 +351,172 @@ class TestConnection < Faraday::TestCase
|
|
|
220
351
|
end
|
|
221
352
|
end
|
|
222
353
|
|
|
223
|
-
def
|
|
224
|
-
conn = Faraday::Connection.new
|
|
225
|
-
|
|
226
|
-
|
|
354
|
+
def test_dups_connection_object
|
|
355
|
+
conn = Faraday::Connection.new 'http://sushi.com/foo',
|
|
356
|
+
:ssl => { :verify => :none },
|
|
357
|
+
:headers => {'content-type' => 'text/plain'},
|
|
358
|
+
:params => {'a'=>'1'}
|
|
359
|
+
|
|
360
|
+
other = conn.dup
|
|
361
|
+
|
|
362
|
+
assert_equal conn.build_exclusive_url, other.build_exclusive_url
|
|
363
|
+
assert_equal 'text/plain', other.headers['content-type']
|
|
364
|
+
assert_equal '1', other.params['a']
|
|
365
|
+
|
|
366
|
+
other.basic_auth('', '')
|
|
367
|
+
other.headers['content-length'] = 12
|
|
368
|
+
other.params['b'] = '2'
|
|
369
|
+
|
|
370
|
+
assert_equal 2, other.builder.handlers.size
|
|
371
|
+
assert_equal 2, conn.builder.handlers.size
|
|
372
|
+
assert !conn.headers.key?('content-length')
|
|
373
|
+
assert !conn.params.key?('b')
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def test_initialize_with_false_option
|
|
377
|
+
conn = Faraday::Connection.new :ssl => {:verify => false}
|
|
378
|
+
assert !conn.ssl.verify?
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def test_init_with_block
|
|
382
|
+
conn = Faraday::Connection.new { }
|
|
383
|
+
assert_equal 0, conn.builder.handlers.size
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def test_init_with_block_yields_connection
|
|
387
|
+
conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
|
|
388
|
+
faraday.adapter :net_http
|
|
389
|
+
faraday.url_prefix = 'http://sushi.com/omnom'
|
|
390
|
+
assert_equal '1', faraday.params['a']
|
|
391
|
+
}
|
|
392
|
+
assert_equal 1, conn.builder.handlers.size
|
|
393
|
+
assert_equal '/omnom', conn.path_prefix
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def env_url(url, params)
|
|
397
|
+
conn = Faraday::Connection.new(url, :params => params)
|
|
398
|
+
yield(conn) if block_given?
|
|
399
|
+
req = conn.build_request(:get)
|
|
400
|
+
req.to_env(conn).url
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
class TestRequestParams < Faraday::TestCase
|
|
405
|
+
def create_connection(*args)
|
|
406
|
+
@conn = Faraday::Connection.new(*args) do |conn|
|
|
407
|
+
yield(conn) if block_given?
|
|
408
|
+
class << conn.builder
|
|
409
|
+
undef app
|
|
410
|
+
def app() lambda { |env| env } end
|
|
411
|
+
end
|
|
227
412
|
end
|
|
228
|
-
assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
|
|
229
413
|
end
|
|
230
414
|
|
|
231
|
-
def
|
|
232
|
-
|
|
233
|
-
|
|
415
|
+
def assert_query_equal(expected, query)
|
|
416
|
+
assert_equal expected, query.split('&').sort
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def with_default_params_encoder(encoder)
|
|
420
|
+
old_encoder = Faraday::Utils.default_params_encoder
|
|
421
|
+
begin
|
|
422
|
+
Faraday::Utils.default_params_encoder = encoder
|
|
423
|
+
yield
|
|
424
|
+
ensure
|
|
425
|
+
Faraday::Utils.default_params_encoder = old_encoder
|
|
234
426
|
end
|
|
235
|
-
|
|
236
|
-
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def test_merges_connection_and_request_params
|
|
430
|
+
create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
|
|
431
|
+
query = get '?page=1', :limit => 5
|
|
432
|
+
assert_query_equal %w[format=json limit=5 page=1 token=abc], query
|
|
433
|
+
end
|
|
434
|
+
|
|
435
|
+
def test_overrides_connection_params
|
|
436
|
+
create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
|
|
437
|
+
conn.params[:b] = 'B'
|
|
438
|
+
assert_equal 'c', conn.params[:c]
|
|
439
|
+
end
|
|
440
|
+
assert_query_equal %w[a=A b=B c=c], get
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
def test_all_overrides_connection_params
|
|
444
|
+
create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
|
|
445
|
+
conn.params = {'b' => 'b'}
|
|
446
|
+
end
|
|
447
|
+
assert_query_equal %w[b=b], get
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
def test_overrides_request_params
|
|
451
|
+
create_connection
|
|
452
|
+
query = get '?p=1&a=a', :p => 2
|
|
453
|
+
assert_query_equal %w[a=a p=2], query
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
def test_overrides_request_params_block
|
|
457
|
+
create_connection
|
|
458
|
+
query = get '?p=1&a=a', :p => 2 do |req|
|
|
459
|
+
req.params[:p] = 3
|
|
460
|
+
end
|
|
461
|
+
assert_query_equal %w[a=a p=3], query
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def test_overrides_request_params_block_url
|
|
465
|
+
create_connection
|
|
466
|
+
query = get nil, :p => 2 do |req|
|
|
467
|
+
req.url '?p=1&a=a', 'p' => 3
|
|
468
|
+
end
|
|
469
|
+
assert_query_equal %w[a=a p=3], query
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
def test_overrides_all_request_params
|
|
473
|
+
create_connection :params => {:c => 'c'}
|
|
474
|
+
query = get '?p=1&a=a', :p => 2 do |req|
|
|
475
|
+
assert_equal 'a', req.params[:a]
|
|
476
|
+
assert_equal 'c', req.params['c']
|
|
477
|
+
assert_equal 2, req.params['p']
|
|
478
|
+
req.params = {:b => 'b'}
|
|
479
|
+
assert_equal 'b', req.params['b']
|
|
480
|
+
end
|
|
481
|
+
assert_query_equal %w[b=b], query
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def test_array_params_in_url
|
|
485
|
+
with_default_params_encoder(nil) do
|
|
486
|
+
create_connection 'http://a.co/page1?color[]=red&color[]=blue'
|
|
487
|
+
query = get
|
|
488
|
+
assert_equal "color%5B%5D=red&color%5B%5D=blue", query
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def test_array_params_in_params
|
|
493
|
+
with_default_params_encoder(nil) do
|
|
494
|
+
create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
|
|
495
|
+
query = get
|
|
496
|
+
assert_equal "color%5B%5D=red&color%5B%5D=blue", query
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
def test_array_params_in_url_with_flat_params
|
|
501
|
+
with_default_params_encoder(Faraday::FlatParamsEncoder) do
|
|
502
|
+
create_connection 'http://a.co/page1?color=red&color=blue'
|
|
503
|
+
query = get
|
|
504
|
+
assert_equal "color=red&color=blue", query
|
|
505
|
+
end
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def test_array_params_in_params_with_flat_params
|
|
509
|
+
with_default_params_encoder(Faraday::FlatParamsEncoder) do
|
|
510
|
+
create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
|
|
511
|
+
query = get
|
|
512
|
+
assert_equal "color=red&color=blue", query
|
|
513
|
+
end
|
|
514
|
+
end
|
|
237
515
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
assert_equal conn.send(attr), duped.send(attr)
|
|
242
|
-
assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
|
|
516
|
+
def get(*args)
|
|
517
|
+
env = @conn.get(*args) do |req|
|
|
518
|
+
yield(req) if block_given?
|
|
243
519
|
end
|
|
520
|
+
env[:url].query
|
|
244
521
|
end
|
|
245
522
|
end
|