faraday 0.6.0 → 0.8.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.
Files changed (58) hide show
  1. data/Gemfile +22 -15
  2. data/{LICENSE → LICENSE.md} +1 -1
  3. data/README.md +216 -146
  4. data/Rakefile +14 -58
  5. data/config.ru +6 -0
  6. data/faraday.gemspec +31 -38
  7. data/lib/faraday/adapter/em_http.rb +204 -0
  8. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +66 -0
  9. data/lib/faraday/adapter/em_synchrony.rb +67 -16
  10. data/lib/faraday/adapter/excon.rb +1 -2
  11. data/lib/faraday/adapter/net_http.rb +82 -39
  12. data/lib/faraday/adapter/net_http_persistent.rb +37 -0
  13. data/lib/faraday/adapter/patron.rb +42 -8
  14. data/lib/faraday/adapter/rack.rb +59 -0
  15. data/lib/faraday/adapter/test.rb +49 -9
  16. data/lib/faraday/adapter/typhoeus.rb +70 -15
  17. data/lib/faraday/adapter.rb +41 -18
  18. data/lib/faraday/builder.rb +34 -16
  19. data/lib/faraday/connection.rb +177 -115
  20. data/lib/faraday/error.rb +4 -2
  21. data/lib/faraday/middleware.rb +18 -10
  22. data/lib/faraday/request/authorization.rb +40 -0
  23. data/lib/faraday/request/basic_authentication.rb +13 -0
  24. data/lib/faraday/request/multipart.rb +5 -6
  25. data/lib/faraday/request/retry.rb +21 -0
  26. data/lib/faraday/request/token_authentication.rb +15 -0
  27. data/lib/faraday/request/url_encoded.rb +1 -3
  28. data/lib/faraday/request.rb +48 -38
  29. data/lib/faraday/response/raise_error.rb +1 -1
  30. data/lib/faraday/response.rb +6 -5
  31. data/lib/faraday/upload_io.rb +1 -1
  32. data/lib/faraday/utils.rb +196 -29
  33. data/lib/faraday.rb +69 -13
  34. data/test/adapters/default_test.rb +14 -0
  35. data/test/adapters/em_http_test.rb +14 -0
  36. data/test/adapters/em_synchrony_test.rb +14 -0
  37. data/test/adapters/excon_test.rb +23 -0
  38. data/test/adapters/integration.rb +190 -0
  39. data/test/adapters/logger_test.rb +2 -2
  40. data/test/adapters/net_http_persistent_test.rb +11 -0
  41. data/test/adapters/net_http_test.rb +37 -21
  42. data/test/adapters/patron_test.rb +17 -0
  43. data/test/adapters/rack_test.rb +26 -0
  44. data/test/adapters/test_middleware_test.rb +28 -1
  45. data/test/adapters/typhoeus_test.rb +14 -0
  46. data/test/authentication_middleware_test.rb +65 -0
  47. data/test/connection_test.rb +211 -81
  48. data/test/env_test.rb +80 -31
  49. data/test/helper.rb +23 -13
  50. data/test/live_server.rb +38 -29
  51. data/test/middleware/retry_test.rb +25 -0
  52. data/test/middleware_stack_test.rb +85 -4
  53. data/test/request_middleware_test.rb +42 -19
  54. data/test/response_middleware_test.rb +30 -3
  55. metadata +104 -110
  56. data/lib/faraday/adapter/action_dispatch.rb +0 -30
  57. data/lib/faraday/request/json.rb +0 -31
  58. data/test/adapters/live_test.rb +0 -186
@@ -1,14 +1,26 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+ require 'uri'
2
3
 
3
4
  class TestConnection < Faraday::TestCase
5
+
6
+ def with_proxy_env(proxy)
7
+ old_proxy = ENV['http_proxy']
8
+ ENV['http_proxy'] = proxy
9
+ begin
10
+ yield
11
+ ensure
12
+ ENV['http_proxy'] = old_proxy
13
+ end
14
+ end
15
+
4
16
  def test_initialize_parses_host_out_of_given_url
5
17
  conn = Faraday::Connection.new "http://sushi.com"
6
18
  assert_equal 'sushi.com', conn.host
7
19
  end
8
20
 
9
- def test_initialize_parses_nil_port_out_of_given_url
21
+ def test_initialize_inherits_default_port_out_of_given_url
10
22
  conn = Faraday::Connection.new "http://sushi.com"
11
- assert_nil conn.port
23
+ assert_equal 80, conn.port
12
24
  end
13
25
 
14
26
  def test_initialize_parses_scheme_out_of_given_url
@@ -38,48 +50,48 @@ class TestConnection < Faraday::TestCase
38
50
 
39
51
  def test_initialize_stores_default_params_from_options
40
52
  conn = Faraday::Connection.new :params => {:a => 1}
41
- assert_equal 1, conn.params['a']
53
+ assert_equal({'a' => 1}, conn.params)
42
54
  end
43
55
 
44
56
  def test_initialize_stores_default_params_from_uri
45
- conn = Faraday::Connection.new "http://sushi.com/fish?a=1", :params => {'b' => '2'}
46
- assert_equal '1', conn.params['a']
47
- assert_equal '2', conn.params['b']
57
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
58
+ assert_equal({'a' => '1'}, conn.params)
48
59
  end
49
60
 
50
- def test_initialize_stores_default_headers_from_options
51
- conn = Faraday::Connection.new :headers => {:a => 1}
52
- assert_equal '1', conn.headers['A']
61
+ def test_initialize_stores_default_params_from_uri_and_options
62
+ conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}
63
+ assert_equal({'a' => 3, 'b' => '2'}, conn.params)
53
64
  end
54
65
 
55
- def test_basic_auth_sets_authorization_header
56
- conn = Faraday::Connection.new
57
- conn.basic_auth 'Aladdin', 'open sesame'
58
- assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
66
+ def test_initialize_stores_default_headers_from_options
67
+ conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
68
+ assert_equal 'Faraday', conn.headers['User-agent']
59
69
  end
60
70
 
61
- def test_long_basic_auth_sets_authorization_header_without_new_lines
71
+ def test_basic_auth_sets_header
62
72
  conn = Faraday::Connection.new
63
- conn.basic_auth "A" * 255, ""
64
- assert_equal "Basic #{'QUFB' * 85}Og==", conn.headers['Authorization']
65
- end
73
+ assert_nil conn.headers['Authorization']
66
74
 
67
- def test_auto_parses_basic_auth_from_url
68
- conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
69
- assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
75
+ conn.basic_auth 'Aladdin', 'open sesame'
76
+ assert auth = conn.headers['Authorization']
77
+ assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
70
78
  end
71
79
 
72
- def test_token_auth_sets_authorization_header
73
- conn = Faraday::Connection.new
74
- conn.token_auth 'abcdef'
75
- assert_equal 'Token token="abcdef"', conn.headers['Authorization']
80
+ def test_auto_parses_basic_auth_from_url_and_unescapes
81
+ conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
82
+ assert auth = conn.headers['Authorization']
83
+ assert_equal Faraday::Request::BasicAuthentication.header("foo@bar.com", "pass word"), auth
76
84
  end
77
85
 
78
- def test_token_auth_with_options_sets_authorization_header
86
+ def test_token_auth_sets_header
79
87
  conn = Faraday::Connection.new
88
+ assert_nil conn.headers['Authorization']
89
+
80
90
  conn.token_auth 'abcdef', :nonce => 'abc'
81
- assert_equal 'Token token="abcdef",
82
- nonce="abc"', conn.headers['Authorization']
91
+ assert auth = conn.headers['Authorization']
92
+ assert_match /^Token /, auth
93
+ assert_match /token="abcdef"/, auth
94
+ assert_match /nonce="abc"/, auth
83
95
  end
84
96
 
85
97
  def test_build_url_uses_connection_host_as_default_uri_host
@@ -89,11 +101,11 @@ class TestConnection < Faraday::TestCase
89
101
  assert_equal 'sushi.com', uri.host
90
102
  end
91
103
 
92
- def test_build_url_uses_connection_port_as_default_uri_port
104
+ def test_build_url_overrides_connection_port_for_absolute_urls
93
105
  conn = Faraday::Connection.new
94
106
  conn.port = 23
95
107
  uri = conn.build_url("http://sushi.com")
96
- assert_equal 23, uri.port
108
+ assert_equal 80, uri.port
97
109
  end
98
110
 
99
111
  def test_build_url_uses_connection_scheme_as_default_uri_scheme
@@ -143,39 +155,38 @@ class TestConnection < Faraday::TestCase
143
155
  assert_equal '/sake.html', uri.path
144
156
  end
145
157
 
146
- def test_build_url_parses_url_params_into_query
158
+ def test_build_url_doesnt_add_ending_slash_given_nil_url
147
159
  conn = Faraday::Connection.new
148
- uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
149
- assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
160
+ conn.url_prefix = "http://sushi.com/nigiri"
161
+ uri = conn.build_url(nil)
162
+ assert_equal "/nigiri", uri.path
150
163
  end
151
164
 
152
- def test_build_url_mashes_default_and_given_params_together
153
- conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
154
- url = conn.build_url("nigiri?page=1", :limit => 5)
155
- assert_match /limit=5/, url.query
156
- assert_match /page=1/, url.query
157
- assert_match /format=json/, url.query
158
- assert_match /token=abc/, url.query
165
+ def test_build_url_doesnt_add_ending_slash_given_empty_url
166
+ conn = Faraday::Connection.new
167
+ conn.url_prefix = "http://sushi.com/nigiri"
168
+ uri = conn.build_url('')
169
+ assert_equal "/nigiri", uri.path
170
+ end
171
+
172
+ def test_build_url_parses_url_params_into_query
173
+ conn = Faraday::Connection.new
174
+ uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
175
+ assert_equal "a%5Bb%5D=1+%2B+2", uri.query
159
176
  end
160
177
 
161
- def test_build_url_overrides_default_params_with_given_params
162
- conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
163
- url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
164
- assert_match /limit=5/, url.query
165
- assert_match /page=1/, url.query
166
- assert_match /format=xml/, url.query
167
- assert_match /token=def/, url.query
168
- assert_no_match /format=json/, url.query
169
- assert_no_match /token=abc/, url.query
178
+ def test_build_url_bracketizes_nested_params_in_query
179
+ conn = Faraday::Connection.new
180
+ uri = conn.build_url("http://sushi.com/sake.html", 'a' => {'b' => 'c'})
181
+ assert_equal "a%5Bb%5D=c", uri.query
170
182
  end
171
183
 
172
184
  def test_build_url_parses_url
173
185
  conn = Faraday::Connection.new
174
186
  uri = conn.build_url("http://sushi.com/sake.html")
175
- assert_equal "http", uri.scheme
176
- assert_equal "sushi.com", uri.host
187
+ assert_equal "http", uri.scheme
188
+ assert_equal "sushi.com", uri.host
177
189
  assert_equal '/sake.html', uri.path
178
- assert_nil uri.port
179
190
  end
180
191
 
181
192
  def test_build_url_parses_url_and_changes_scheme
@@ -185,32 +196,53 @@ class TestConnection < Faraday::TestCase
185
196
  assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
186
197
  end
187
198
 
188
- def test_proxy_accepts_string
199
+ def test_build_url_handles_uri_instances
189
200
  conn = Faraday::Connection.new
190
- conn.proxy 'http://proxy.com'
191
- assert_equal 'proxy.com', conn.proxy[:uri].host
192
- assert_equal [:uri], conn.proxy.keys
201
+ uri = conn.build_url(URI('/sake.html'))
202
+ assert_equal '/sake.html', uri.path
203
+ end
204
+
205
+ def test_proxy_accepts_string
206
+ with_proxy_env "http://duncan.proxy.com:80" do
207
+ conn = Faraday::Connection.new
208
+ conn.proxy 'http://proxy.com'
209
+ assert_equal 'proxy.com', conn.proxy[:uri].host
210
+ assert_equal [:uri], conn.proxy.keys
211
+ end
193
212
  end
194
213
 
195
214
  def test_proxy_accepts_uri
196
- conn = Faraday::Connection.new
197
- conn.proxy Addressable::URI.parse('http://proxy.com')
198
- assert_equal 'proxy.com', conn.proxy[:uri].host
199
- assert_equal [:uri], conn.proxy.keys
215
+ with_proxy_env "http://duncan.proxy.com:80" do
216
+ conn = Faraday::Connection.new
217
+ conn.proxy URI.parse('http://proxy.com')
218
+ assert_equal 'proxy.com', conn.proxy[:uri].host
219
+ assert_equal [:uri], conn.proxy.keys
220
+ end
200
221
  end
201
222
 
202
223
  def test_proxy_accepts_hash_with_string_uri
203
- conn = Faraday::Connection.new
204
- conn.proxy :uri => 'http://proxy.com', :user => 'rick'
205
- assert_equal 'proxy.com', conn.proxy[:uri].host
206
- assert_equal 'rick', conn.proxy[:user]
224
+ with_proxy_env "http://duncan.proxy.com:80" do
225
+ conn = Faraday::Connection.new
226
+ conn.proxy :uri => 'http://proxy.com', :user => 'rick'
227
+ assert_equal 'proxy.com', conn.proxy[:uri].host
228
+ assert_equal 'rick', conn.proxy[:user]
229
+ end
207
230
  end
208
231
 
209
232
  def test_proxy_accepts_hash
210
- conn = Faraday::Connection.new
211
- conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
212
- assert_equal 'proxy.com', conn.proxy[:uri].host
213
- assert_equal 'rick', conn.proxy[:user]
233
+ with_proxy_env "http://duncan.proxy.com:80" do
234
+ conn = Faraday::Connection.new
235
+ conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
236
+ assert_equal 'proxy.com', conn.proxy[:uri].host
237
+ assert_equal 'rick', conn.proxy[:user]
238
+ end
239
+ end
240
+
241
+ def test_proxy_accepts_http_env
242
+ with_proxy_env "http://duncan.proxy.com:80" do
243
+ conn = Faraday::Connection.new
244
+ assert_equal 'duncan.proxy.com', conn.proxy[:uri].host
245
+ end
214
246
  end
215
247
 
216
248
  def test_proxy_requires_uri
@@ -222,24 +254,122 @@ class TestConnection < Faraday::TestCase
222
254
 
223
255
  def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
224
256
  conn = Faraday::Connection.new
225
- class << conn
226
- public :build_query
227
- end
228
- assert_equal "a%5Bb%5D=1%20%2B%202", conn.build_query('a[b]' => '1 + 2')
257
+ url = conn.build_url('', 'a[b]' => '1 + 2')
258
+ assert_equal "a%5Bb%5D=1+%2B+2", url.query
229
259
  end
230
260
 
231
261
  def test_dups_connection_object
232
- conn = Faraday::Connection.new 'http://sushi.com/foo' do |b|
233
- b.adapter :net_http
262
+ conn = Faraday::Connection.new 'http://sushi.com/foo',
263
+ :ssl => { :verify => :none },
264
+ :headers => {'content-type' => 'text/plain'},
265
+ :params => {'a'=>'1'}
266
+
267
+ other = conn.dup
268
+
269
+ assert_equal conn.build_url(''), other.build_url('')
270
+ assert_equal 'text/plain', other.headers['content-type']
271
+ assert_equal '1', other.params['a']
272
+
273
+ other.basic_auth('', '')
274
+ other.headers['content-length'] = 12
275
+ other.params['b'] = '2'
276
+
277
+ assert_equal 2, other.builder.handlers.size
278
+ assert_equal 2, conn.builder.handlers.size
279
+ assert !conn.headers.key?('content-length')
280
+ assert !conn.params.key?('b')
281
+ end
282
+
283
+ def test_init_with_block
284
+ conn = Faraday::Connection.new { }
285
+ assert_equal 0, conn.builder.handlers.size
286
+ end
287
+
288
+ def test_init_with_block_yields_connection
289
+ conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
290
+ faraday.adapter :net_http
291
+ faraday.url_prefix = 'http://sushi.com/omnom'
292
+ assert_equal '1', faraday.params['a']
293
+ }
294
+ assert_equal 1, conn.builder.handlers.size
295
+ assert_equal '/omnom', conn.path_prefix
296
+ end
297
+ end
298
+
299
+ class TestRequestParams < Faraday::TestCase
300
+ def create_connection(*args)
301
+ @conn = Faraday::Connection.new(*args) do |conn|
302
+ yield conn if block_given?
303
+ class << conn
304
+ undef app
305
+ def app() lambda { |env| env } end
306
+ end
234
307
  end
235
- conn.headers['content-type'] = 'text/plain'
236
- conn.params['a'] = '1'
237
-
238
- duped = conn.dup
239
- assert_equal conn.build_url(''), duped.build_url('')
240
- [:headers, :params, :builder].each do |attr|
241
- assert_equal conn.send(attr), duped.send(attr)
242
- assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
308
+ end
309
+
310
+ def get(*args)
311
+ env = @conn.get(*args) do |req|
312
+ yield req if block_given?
313
+ end
314
+ env[:url].query
315
+ end
316
+
317
+ def assert_query_equal(expected, query)
318
+ assert_equal expected, query.split('&').sort
319
+ end
320
+
321
+ def test_merges_connection_and_request_params
322
+ create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
323
+ query = get '?page=1', :limit => 5
324
+ assert_query_equal %w[format=json limit=5 page=1 token=abc], query
325
+ end
326
+
327
+ def test_overrides_connection_params
328
+ create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
329
+ conn.params[:b] = 'B'
330
+ assert_equal 'c', conn.params[:c]
331
+ end
332
+ assert_query_equal %w[a=A b=B c=c], get
333
+ end
334
+
335
+ def test_all_overrides_connection_params
336
+ create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
337
+ conn.params = {'b' => 'b'}
338
+ end
339
+ assert_query_equal %w[b=b], get
340
+ end
341
+
342
+ def test_overrides_request_params
343
+ create_connection
344
+ query = get '?p=1&a=a', :p => 2
345
+ assert_query_equal %w[a=a p=2], query
346
+ end
347
+
348
+ def test_overrides_request_params_block
349
+ create_connection
350
+ query = get '?p=1&a=a', :p => 2 do |req|
351
+ req.params[:p] = 3
352
+ end
353
+ assert_query_equal %w[a=a p=3], query
354
+ end
355
+
356
+ def test_overrides_request_params_block_url
357
+ create_connection
358
+ query = get nil, :p => 2 do |req|
359
+ req.url '?p=1&a=a', 'p' => 3
360
+ end
361
+ assert_query_equal %w[a=a p=3], query
362
+ end
363
+
364
+ def test_overrides_all_request_params
365
+ create_connection :params => {:c => 'c'}
366
+ query = get '?p=1&a=a', :p => 2 do |req|
367
+ assert_equal 'a', req.params[:a]
368
+ assert_equal 'c', req.params['c']
369
+ assert_equal 2, req.params['p']
370
+ req.params = {:b => 'b'}
371
+ assert_equal 'b', req.params['b']
243
372
  end
373
+ assert_query_equal %w[b=b], query
244
374
  end
245
375
  end
data/test/env_test.rb CHANGED
@@ -2,55 +2,79 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
 
3
3
  class EnvTest < Faraday::TestCase
4
4
  def setup
5
- @conn = Faraday.new :url => 'http://sushi.com/api', :headers => {'Mime-Version' => '1.0'}
5
+ @conn = Faraday.new :url => 'http://sushi.com/api',
6
+ :headers => {'Mime-Version' => '1.0'},
7
+ :request => {:oauth => {:consumer_key => 'anonymous'}}
8
+
6
9
  @conn.options[:timeout] = 3
7
10
  @conn.options[:open_timeout] = 5
8
11
  @conn.ssl[:verify] = false
9
12
  @conn.proxy 'http://proxy.com'
10
- @input = { :body => 'abc' }
11
- @env = env_for @conn do |req|
12
- req.url 'foo.json', 'a' => 1
13
- req['Server'] = 'Faraday'
14
- req.body = @input[:body]
15
- end
16
13
  end
17
14
 
18
15
  def test_request_create_stores_method
19
- assert_equal :get, @env[:method]
16
+ env = make_env(:get)
17
+ assert_equal :get, env[:method]
20
18
  end
21
19
 
22
- def test_request_create_stores_addressable_uri
23
- assert_equal 'http://sushi.com/api/foo.json?a=1', @env[:url].to_s
20
+ def test_request_create_stores_uri
21
+ env = make_env do |req|
22
+ req.url 'foo.json', 'a' => 1
23
+ end
24
+ assert_equal 'http://sushi.com/api/foo.json?a=1', env[:url].to_s
24
25
  end
25
26
 
26
27
  def test_request_create_stores_headers
27
- headers = @env[:request_headers]
28
+ env = make_env do |req|
29
+ req['Server'] = 'Faraday'
30
+ end
31
+ headers = env[:request_headers]
28
32
  assert_equal '1.0', headers['mime-version']
29
33
  assert_equal 'Faraday', headers['server']
30
34
  end
31
35
 
32
36
  def test_request_create_stores_body
33
- assert_equal @input[:body], @env[:body]
37
+ env = make_env do |req|
38
+ req.body = 'hi'
39
+ end
40
+ assert_equal 'hi', env[:body]
34
41
  end
35
42
 
36
- def test_request_create_stores_timeout_options
37
- assert_equal 3, @env[:request][:timeout]
38
- assert_equal 5, @env[:request][:open_timeout]
43
+ def test_global_request_options
44
+ env = make_env
45
+ assert_equal 3, env[:request][:timeout]
46
+ assert_equal 5, env[:request][:open_timeout]
47
+ end
48
+
49
+ def test_per_request_options
50
+ env = make_env do |req|
51
+ req.options[:timeout] = 10
52
+ req.options[:custom] = true
53
+ req.options[:oauth][:consumer_secret] = 'xyz'
54
+ end
55
+ assert_equal 10, env[:request][:timeout]
56
+ assert_equal 5, env[:request][:open_timeout]
57
+ assert_equal true, env[:request][:custom]
58
+
59
+ oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'}
60
+ assert_equal oauth_expected, env[:request][:oauth]
39
61
  end
40
62
 
41
63
  def test_request_create_stores_ssl_options
42
- assert_equal false, @env[:ssl][:verify]
64
+ env = make_env
65
+ assert_equal false, env[:ssl][:verify]
43
66
  end
44
67
 
45
68
  def test_request_create_stores_proxy_options
46
- assert_equal 'proxy.com', @env[:request][:proxy][:uri].host
69
+ env = make_env
70
+ assert_equal 'proxy.com', env[:request][:proxy][:uri].host
47
71
  end
48
72
 
49
- def env_for(connection)
50
- env_setup = Faraday::Request.create do |req|
51
- yield req
52
- end
53
- env_setup.to_env_hash(connection, :get)
73
+ private
74
+
75
+ def make_env(method = :get, connection = @conn, &block)
76
+ request = connection.build_request(method, &block)
77
+ request.to_env(connection)
54
78
  end
55
79
  end
56
80
 
@@ -59,6 +83,31 @@ class HeadersTest < Faraday::TestCase
59
83
  @headers = Faraday::Utils::Headers.new
60
84
  end
61
85
 
86
+ def test_normalizes_different_capitalizations
87
+ @headers['Content-Type'] = 'application/json'
88
+ assert_equal ['Content-Type'], @headers.keys
89
+ assert_equal 'application/json', @headers['Content-Type']
90
+ assert_equal 'application/json', @headers['CONTENT-TYPE']
91
+ assert_equal 'application/json', @headers['content-type']
92
+ assert @headers.include?('content-type')
93
+
94
+ @headers['content-type'] = 'application/xml'
95
+ assert_equal ['Content-Type'], @headers.keys
96
+ assert_equal 'application/xml', @headers['Content-Type']
97
+ assert_equal 'application/xml', @headers['CONTENT-TYPE']
98
+ assert_equal 'application/xml', @headers['content-type']
99
+ end
100
+
101
+ def test_delete_key
102
+ @headers['Content-Type'] = 'application/json'
103
+ assert_equal 1, @headers.size
104
+ assert @headers.include?('content-type')
105
+ assert_equal 'application/json', @headers.delete('content-type')
106
+ assert_equal 0, @headers.size
107
+ assert !@headers.include?('content-type')
108
+ assert_equal nil, @headers.delete('content-type')
109
+ end
110
+
62
111
  def test_parse_response_headers_leaves_http_status_line_out
63
112
  @headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
64
113
  assert_equal %w(Content-Type), @headers.keys
@@ -88,45 +137,45 @@ class ResponseTest < Faraday::TestCase
88
137
  }
89
138
  @response = Faraday::Response.new @env
90
139
  end
91
-
140
+
92
141
  def test_finished
93
142
  assert @response.finished?
94
143
  end
95
-
144
+
96
145
  def test_error_on_finish
97
146
  assert_raises RuntimeError do
98
147
  @response.finish({})
99
148
  end
100
149
  end
101
-
150
+
102
151
  def test_not_success
103
152
  assert !@response.success?
104
153
  end
105
-
154
+
106
155
  def test_status
107
156
  assert_equal 404, @response.status
108
157
  end
109
-
158
+
110
159
  def test_body
111
160
  assert_equal 'yikes', @response.body
112
161
  end
113
-
162
+
114
163
  def test_headers
115
164
  assert_equal 'text/plain', @response.headers['Content-Type']
116
165
  assert_equal 'text/plain', @response['content-type']
117
166
  end
118
-
167
+
119
168
  def test_apply_request
120
169
  @response.apply_request :body => 'a=b', :method => :post
121
170
  assert_equal 'yikes', @response.body
122
171
  assert_equal :post, @response.env[:method]
123
172
  end
124
-
173
+
125
174
  def test_marshal
126
175
  @response = Faraday::Response.new
127
176
  @response.on_complete { }
128
177
  @response.finish @env.merge(:custom => 'moo')
129
-
178
+
130
179
  loaded = Marshal.load Marshal.dump(@response)
131
180
  assert_nil loaded.env[:custom]
132
181
  assert_equal %w[body response_headers status], loaded.env.keys.map { |k| k.to_s }.sort
data/test/helper.rb CHANGED
@@ -1,12 +1,13 @@
1
- require 'test/unit'
2
- unless ENV['BUNDLE_GEMFILE']
3
- require 'rubygems'
4
- require 'bundler'
5
- Bundler.setup
1
+ unless ENV['CI']
2
+ begin
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ rescue LoadError
6
+ end
6
7
  end
7
8
 
8
- require 'webmock/test_unit'
9
- WebMock.disable_net_connect!(:allow_localhost => true)
9
+ require 'test/unit'
10
+ require 'stringio'
10
11
 
11
12
  if ENV['LEFTRIGHT']
12
13
  begin
@@ -16,11 +17,7 @@ if ENV['LEFTRIGHT']
16
17
  end
17
18
  end
18
19
 
19
- unless $LOAD_PATH.include? 'lib'
20
- $LOAD_PATH.unshift(File.dirname(__FILE__))
21
- $LOAD_PATH.unshift(File.join($LOAD_PATH.first, '..', 'lib'))
22
- end
23
- require 'faraday'
20
+ require File.expand_path('../../lib/faraday', __FILE__)
24
21
 
25
22
  begin
26
23
  require 'ruby-debug'
@@ -35,11 +32,24 @@ module Faraday
35
32
  LIVE_SERVER = case ENV['LIVE']
36
33
  when /^http/ then ENV['LIVE']
37
34
  when nil then nil
38
- else 'http://localhost:4567'
35
+ else 'http://127.0.0.1:4567'
39
36
  end
40
37
 
41
38
  def test_default
42
39
  assert true
43
40
  end unless defined? ::MiniTest
41
+
42
+ def capture_warnings
43
+ old, $stderr = $stderr, StringIO.new
44
+ begin
45
+ yield
46
+ $stderr.string
47
+ ensure
48
+ $stderr = old
49
+ end
50
+ end
44
51
  end
45
52
  end
53
+
54
+ require 'webmock/test_unit'
55
+ WebMock.disable_net_connect!(:allow => Faraday::TestCase::LIVE_SERVER)