faraday 0.8.11 → 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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.document +6 -0
  3. data/CHANGELOG.md +15 -0
  4. data/CONTRIBUTING.md +36 -0
  5. data/Gemfile +10 -3
  6. data/LICENSE.md +1 -1
  7. data/README.md +17 -51
  8. data/Rakefile +2 -18
  9. data/faraday.gemspec +34 -0
  10. data/lib/faraday/adapter/em_http.rb +34 -0
  11. data/lib/faraday/adapter/em_http_ssl_patch.rb +56 -0
  12. data/lib/faraday/adapter/em_synchrony.rb +11 -24
  13. data/lib/faraday/adapter/excon.rb +12 -2
  14. data/lib/faraday/adapter/httpclient.rb +106 -0
  15. data/lib/faraday/adapter/net_http.rb +10 -6
  16. data/lib/faraday/adapter/patron.rb +2 -8
  17. data/lib/faraday/adapter/rack.rb +0 -2
  18. data/lib/faraday/adapter/test.rb +39 -39
  19. data/lib/faraday/adapter/typhoeus.rb +12 -3
  20. data/lib/faraday/adapter.rb +20 -35
  21. data/lib/faraday/autoload.rb +85 -0
  22. data/lib/faraday/connection.rb +232 -125
  23. data/lib/faraday/error.rb +42 -34
  24. data/lib/faraday/options.rb +350 -0
  25. data/lib/faraday/parameters.rb +193 -0
  26. data/lib/faraday/{builder.rb → rack_builder.rb} +79 -22
  27. data/lib/faraday/request/authorization.rb +7 -5
  28. data/lib/faraday/request/basic_authentication.rb +1 -1
  29. data/lib/faraday/request/instrumentation.rb +36 -0
  30. data/lib/faraday/request/multipart.rb +10 -9
  31. data/lib/faraday/request/retry.rb +99 -4
  32. data/lib/faraday/request/token_authentication.rb +2 -2
  33. data/lib/faraday/request/url_encoded.rb +7 -6
  34. data/lib/faraday/request.rb +21 -30
  35. data/lib/faraday/response/logger.rb +4 -4
  36. data/lib/faraday/response/raise_error.rb +4 -2
  37. data/lib/faraday/response.rb +17 -23
  38. data/lib/faraday/utils.rb +81 -71
  39. data/lib/faraday.rb +187 -68
  40. data/script/console +7 -0
  41. data/script/proxy-server +1 -0
  42. data/script/release +6 -3
  43. data/script/test +4 -2
  44. data/test/adapters/em_http_test.rb +6 -1
  45. data/test/adapters/em_synchrony_test.rb +7 -1
  46. data/test/adapters/httpclient_test.rb +21 -0
  47. data/test/adapters/integration.rb +23 -8
  48. data/test/adapters/logger_test.rb +1 -1
  49. data/test/adapters/net_http_persistent_test.rb +10 -1
  50. data/test/adapters/net_http_test.rb +0 -31
  51. data/test/adapters/patron_test.rb +4 -1
  52. data/test/adapters/test_middleware_test.rb +48 -4
  53. data/test/adapters/typhoeus_test.rb +8 -1
  54. data/test/authentication_middleware_test.rb +2 -2
  55. data/test/connection_test.rb +160 -84
  56. data/test/env_test.rb +51 -24
  57. data/test/helper.rb +13 -13
  58. data/test/live_server.rb +8 -0
  59. data/test/middleware/instrumentation_test.rb +88 -0
  60. data/test/middleware/retry_test.rb +88 -35
  61. data/test/middleware_stack_test.rb +13 -12
  62. data/test/options_test.rb +252 -0
  63. data/test/request_middleware_test.rb +11 -1
  64. data/test/response_middleware_test.rb +2 -4
  65. data/test/strawberry.rb +2 -0
  66. data/test/utils_test.rb +34 -6
  67. metadata +71 -11
  68. data/test/parameters_test.rb +0 -24
@@ -1,5 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
- require 'uri'
1
+ require File.expand_path('../helper', __FILE__)
3
2
 
4
3
  class TestConnection < Faraday::TestCase
5
4
 
@@ -98,140 +97,180 @@ class TestConnection < Faraday::TestCase
98
97
  assert_match(/nonce="abc"/, auth)
99
98
  end
100
99
 
101
- def test_build_url_uses_connection_host_as_default_uri_host
100
+ def test_build_exclusive_url_uses_connection_host_as_default_uri_host
102
101
  conn = Faraday::Connection.new
103
102
  conn.host = 'sushi.com'
104
- uri = conn.build_url("/sake.html")
103
+ uri = conn.build_exclusive_url("/sake.html")
105
104
  assert_equal 'sushi.com', uri.host
106
105
  end
107
106
 
108
- def test_build_url_overrides_connection_port_for_absolute_urls
107
+ def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
109
108
  conn = Faraday::Connection.new
110
109
  conn.port = 23
111
- uri = conn.build_url("http://sushi.com")
110
+ uri = conn.build_exclusive_url("http://sushi.com")
112
111
  assert_equal 80, uri.port
113
112
  end
114
113
 
115
- def test_build_url_uses_connection_scheme_as_default_uri_scheme
114
+ def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
116
115
  conn = Faraday::Connection.new 'http://sushi.com'
117
- uri = conn.build_url("/sake.html")
116
+ uri = conn.build_exclusive_url("/sake.html")
118
117
  assert_equal 'http', uri.scheme
119
118
  end
120
119
 
121
- def test_build_url_uses_connection_path_prefix_to_customize_path
120
+ def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
122
121
  conn = Faraday::Connection.new
123
122
  conn.path_prefix = '/fish'
124
- uri = conn.build_url("sake.html")
123
+ uri = conn.build_exclusive_url("sake.html")
125
124
  assert_equal '/fish/sake.html', uri.path
126
125
  end
127
126
 
128
- def test_build_url_uses_root_connection_path_prefix_to_customize_path
127
+ def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
129
128
  conn = Faraday::Connection.new
130
129
  conn.path_prefix = '/'
131
- uri = conn.build_url("sake.html")
130
+ uri = conn.build_exclusive_url("sake.html")
132
131
  assert_equal '/sake.html', uri.path
133
132
  end
134
133
 
135
- def test_build_url_forces_connection_path_prefix_to_be_absolute
134
+ def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
136
135
  conn = Faraday::Connection.new
137
136
  conn.path_prefix = 'fish'
138
- uri = conn.build_url("sake.html")
137
+ uri = conn.build_exclusive_url("sake.html")
139
138
  assert_equal '/fish/sake.html', uri.path
140
139
  end
141
140
 
142
- def test_build_url_ignores_connection_path_prefix_trailing_slash
141
+ def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
143
142
  conn = Faraday::Connection.new
144
143
  conn.path_prefix = '/fish/'
145
- uri = conn.build_url("sake.html")
144
+ uri = conn.build_exclusive_url("sake.html")
146
145
  assert_equal '/fish/sake.html', uri.path
147
146
  end
148
147
 
149
- def test_build_url_allows_absolute_uri_to_ignore_connection_path_prefix
148
+ def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
150
149
  conn = Faraday::Connection.new
151
150
  conn.path_prefix = '/fish'
152
- uri = conn.build_url("/sake.html")
151
+ uri = conn.build_exclusive_url("/sake.html")
153
152
  assert_equal '/sake.html', uri.path
154
153
  end
155
154
 
156
- def test_build_url_parses_url_params_into_path
155
+ def test_build_exclusive_url_parses_url_params_into_path
157
156
  conn = Faraday::Connection.new
158
- uri = conn.build_url("http://sushi.com/sake.html")
157
+ uri = conn.build_exclusive_url("http://sushi.com/sake.html")
159
158
  assert_equal '/sake.html', uri.path
160
159
  end
161
160
 
162
- def test_build_url_doesnt_add_ending_slash_given_nil_url
161
+ def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url
163
162
  conn = Faraday::Connection.new
164
163
  conn.url_prefix = "http://sushi.com/nigiri"
165
- uri = conn.build_url(nil)
164
+ uri = conn.build_exclusive_url
166
165
  assert_equal "/nigiri", uri.path
167
166
  end
168
167
 
169
- def test_build_url_doesnt_add_ending_slash_given_empty_url
168
+ def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url
170
169
  conn = Faraday::Connection.new
171
170
  conn.url_prefix = "http://sushi.com/nigiri"
172
- uri = conn.build_url('')
171
+ uri = conn.build_exclusive_url('')
173
172
  assert_equal "/nigiri", uri.path
174
173
  end
175
174
 
176
- def test_build_url_parses_url_params_into_query
177
- conn = Faraday::Connection.new
178
- uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
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
179
+ end
180
+
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')
179
205
  assert_equal "a%5Bb%5D=1+%2B+2", uri.query
180
206
  end
181
207
 
182
- def test_build_url_escapes_per_spec
183
- conn = Faraday::Connection.new
184
- uri = conn.build_url('http:/', 'a' => '1+2 foo~bar.-baz')
208
+ def test_env_url_escapes_per_spec
209
+ uri = env_url('http:/', 'a' => '1+2 foo~bar.-baz')
185
210
  assert_equal "a=1%2B2+foo~bar.-baz", uri.query
186
211
  end
187
212
 
188
- def test_build_url_bracketizes_nested_params_in_query
189
- conn = Faraday::Connection.new
190
- uri = conn.build_url("http://sushi.com/sake.html", 'a' => {'b' => 'c'})
191
- assert_equal "a%5Bb%5D=c", uri.query
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
192
216
  end
193
217
 
194
- def test_build_url_parses_url
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
228
+ end
229
+
230
+ def test_build_exclusive_url_parses_url
195
231
  conn = Faraday::Connection.new
196
- uri = conn.build_url("http://sushi.com/sake.html")
232
+ uri = conn.build_exclusive_url("http://sushi.com/sake.html")
197
233
  assert_equal "http", uri.scheme
198
234
  assert_equal "sushi.com", uri.host
199
235
  assert_equal '/sake.html', uri.path
200
236
  end
201
237
 
202
- def test_build_url_parses_url_and_changes_scheme
238
+ def test_build_exclusive_url_parses_url_and_changes_scheme
203
239
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
204
240
  conn.scheme = 'https'
205
- uri = conn.build_url("sake.html")
241
+ uri = conn.build_exclusive_url("sake.html")
206
242
  assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
207
243
  end
208
244
 
209
- def test_build_url_handles_uri_instances
210
- conn = Faraday::Connection.new
211
- uri = conn.build_url(URI('/sake.html'))
212
- assert_equal '/sake.html', uri.path
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
213
249
  end
214
250
 
215
- def test_request_header_change_does_not_modify_connection_header
216
- connection = Faraday.new(:url => "https://asushi.com/sake.html")
217
- connection.headers = { "Authorization"=>"token abc123" }
218
-
219
- request = connection.build_request(:get)
220
- request.headers.delete("Authorization")
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
221
256
 
222
- assert_equal connection.headers.keys.sort, ["Authorization"]
223
- assert connection.headers.include?("Authorization")
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
224
262
 
225
- assert_equal request.headers.keys.sort, []
226
- assert !request.headers.include?("Authorization")
263
+ def test_build_exclusive_url_handles_uri_instances
264
+ conn = Faraday::Connection.new
265
+ uri = conn.build_exclusive_url(URI('/sake.html'))
266
+ assert_equal '/sake.html', uri.path
227
267
  end
228
268
 
229
269
  def test_proxy_accepts_string
230
270
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
231
271
  conn = Faraday::Connection.new
232
272
  conn.proxy 'http://proxy.com'
233
- assert_equal 'proxy.com', conn.proxy[:uri].host
234
- assert_equal [:uri], conn.proxy.keys
273
+ assert_equal 'proxy.com', conn.proxy.host
235
274
  end
236
275
  end
237
276
 
@@ -239,8 +278,7 @@ class TestConnection < Faraday::TestCase
239
278
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
240
279
  conn = Faraday::Connection.new
241
280
  conn.proxy URI.parse('http://proxy.com')
242
- assert_equal 'proxy.com', conn.proxy[:uri].host
243
- assert_equal [:uri], conn.proxy.keys
281
+ assert_equal 'proxy.com', conn.proxy.host
244
282
  end
245
283
  end
246
284
 
@@ -248,8 +286,8 @@ class TestConnection < Faraday::TestCase
248
286
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
249
287
  conn = Faraday::Connection.new
250
288
  conn.proxy :uri => 'http://proxy.com', :user => 'rick'
251
- assert_equal 'proxy.com', conn.proxy[:uri].host
252
- assert_equal 'rick', conn.proxy[:user]
289
+ assert_equal 'proxy.com', conn.proxy.host
290
+ assert_equal 'rick', conn.proxy.user
253
291
  end
254
292
  end
255
293
 
@@ -257,23 +295,23 @@ class TestConnection < Faraday::TestCase
257
295
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
258
296
  conn = Faraday::Connection.new
259
297
  conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
260
- assert_equal 'proxy.com', conn.proxy[:uri].host
261
- assert_equal 'rick', conn.proxy[:user]
298
+ assert_equal 'proxy.com', conn.proxy.host
299
+ assert_equal 'rick', conn.proxy.user
262
300
  end
263
301
  end
264
302
 
265
303
  def test_proxy_accepts_http_env
266
304
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
267
305
  conn = Faraday::Connection.new
268
- assert_equal 'duncan.proxy.com', conn.proxy[:uri].host
306
+ assert_equal 'duncan.proxy.com', conn.proxy.host
269
307
  end
270
308
  end
271
309
 
272
310
  def test_proxy_accepts_http_env_with_auth
273
311
  with_env 'http_proxy', "http://a%40b:my%20pass@duncan.proxy.com:80" do
274
312
  conn = Faraday::Connection.new
275
- assert_equal 'a@b', conn.proxy[:user]
276
- assert_equal 'my pass', conn.proxy[:password]
313
+ assert_equal 'a@b', conn.proxy.user
314
+ assert_equal 'my pass', conn.proxy.password
277
315
  end
278
316
  end
279
317
 
@@ -317,26 +355,27 @@ class TestConnection < Faraday::TestCase
317
355
  conn = Faraday::Connection.new 'http://sushi.com/foo',
318
356
  :ssl => { :verify => :none },
319
357
  :headers => {'content-type' => 'text/plain'},
320
- :params => {'a'=>'1'},
321
- :request => {:timeout => 5}
358
+ :params => {'a'=>'1'}
322
359
 
323
360
  other = conn.dup
324
361
 
325
- assert_equal conn.build_url(''), other.build_url('')
362
+ assert_equal conn.build_exclusive_url, other.build_exclusive_url
326
363
  assert_equal 'text/plain', other.headers['content-type']
327
364
  assert_equal '1', other.params['a']
328
365
 
329
366
  other.basic_auth('', '')
330
367
  other.headers['content-length'] = 12
331
368
  other.params['b'] = '2'
332
- other.options[:open_timeout] = 10
333
369
 
334
370
  assert_equal 2, other.builder.handlers.size
335
371
  assert_equal 2, conn.builder.handlers.size
336
372
  assert !conn.headers.key?('content-length')
337
373
  assert !conn.params.key?('b')
338
- assert_equal 5, other.options[:timeout]
339
- assert_nil conn.options[:open_timeout]
374
+ end
375
+
376
+ def test_initialize_with_false_option
377
+ conn = Faraday::Connection.new :ssl => {:verify => false}
378
+ assert !conn.ssl.verify?
340
379
  end
341
380
 
342
381
  def test_init_with_block
@@ -353,30 +392,40 @@ class TestConnection < Faraday::TestCase
353
392
  assert_equal 1, conn.builder.handlers.size
354
393
  assert_equal '/omnom', conn.path_prefix
355
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
356
402
  end
357
403
 
358
404
  class TestRequestParams < Faraday::TestCase
359
405
  def create_connection(*args)
360
406
  @conn = Faraday::Connection.new(*args) do |conn|
361
- yield conn if block_given?
362
- class << conn
407
+ yield(conn) if block_given?
408
+ class << conn.builder
363
409
  undef app
364
410
  def app() lambda { |env| env } end
365
411
  end
366
412
  end
367
413
  end
368
414
 
369
- def get(*args)
370
- env = @conn.get(*args) do |req|
371
- yield req if block_given?
372
- end
373
- env[:url].query
374
- end
375
-
376
415
  def assert_query_equal(expected, query)
377
416
  assert_equal expected, query.split('&').sort
378
417
  end
379
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
426
+ end
427
+ end
428
+
380
429
  def test_merges_connection_and_request_params
381
430
  create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
382
431
  query = get '?page=1', :limit => 5
@@ -433,14 +482,41 @@ class TestRequestParams < Faraday::TestCase
433
482
  end
434
483
 
435
484
  def test_array_params_in_url
436
- create_connection 'http://a.co/page1?color[]=red&color[]=blue'
437
- query = get
438
- assert_equal "color%5B%5D=red&color%5B%5D=blue", query
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
439
490
  end
440
491
 
441
492
  def test_array_params_in_params
442
- create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
443
- query = get
444
- assert_equal "color%5B%5D=red&color%5B%5D=blue", query
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
515
+
516
+ def get(*args)
517
+ env = @conn.get(*args) do |req|
518
+ yield(req) if block_given?
519
+ end
520
+ env[:url].query
445
521
  end
446
522
  end
data/test/env_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
1
+ require File.expand_path('../helper', __FILE__)
2
2
 
3
3
  class EnvTest < Faraday::TestCase
4
4
  def setup
@@ -6,29 +6,29 @@ class EnvTest < Faraday::TestCase
6
6
  :headers => {'Mime-Version' => '1.0'},
7
7
  :request => {:oauth => {:consumer_key => 'anonymous'}}
8
8
 
9
- @conn.options[:timeout] = 3
10
- @conn.options[:open_timeout] = 5
11
- @conn.ssl[:verify] = false
9
+ @conn.options.timeout = 3
10
+ @conn.options.open_timeout = 5
11
+ @conn.ssl.verify = false
12
12
  @conn.proxy 'http://proxy.com'
13
13
  end
14
14
 
15
15
  def test_request_create_stores_method
16
16
  env = make_env(:get)
17
- assert_equal :get, env[:method]
17
+ assert_equal :get, env.method
18
18
  end
19
19
 
20
20
  def test_request_create_stores_uri
21
21
  env = make_env do |req|
22
22
  req.url 'foo.json', 'a' => 1
23
23
  end
24
- assert_equal 'http://sushi.com/api/foo.json?a=1', env[:url].to_s
24
+ assert_equal 'http://sushi.com/api/foo.json?a=1', env.url.to_s
25
25
  end
26
26
 
27
27
  def test_request_create_stores_headers
28
28
  env = make_env do |req|
29
29
  req['Server'] = 'Faraday'
30
30
  end
31
- headers = env[:request_headers]
31
+ headers = env.request_headers
32
32
  assert_equal '1.0', headers['mime-version']
33
33
  assert_equal 'Faraday', headers['server']
34
34
  end
@@ -37,37 +37,37 @@ class EnvTest < Faraday::TestCase
37
37
  env = make_env do |req|
38
38
  req.body = 'hi'
39
39
  end
40
- assert_equal 'hi', env[:body]
40
+ assert_equal 'hi', env.body
41
41
  end
42
42
 
43
43
  def test_global_request_options
44
44
  env = make_env
45
- assert_equal 3, env[:request][:timeout]
46
- assert_equal 5, env[:request][:open_timeout]
45
+ assert_equal 3, env.request.timeout
46
+ assert_equal 5, env.request.open_timeout
47
47
  end
48
48
 
49
49
  def test_per_request_options
50
50
  env = make_env do |req|
51
- req.options[:timeout] = 10
52
- req.options[:custom] = true
53
- req.options[:oauth][:consumer_secret] = 'xyz'
51
+ req.options.timeout = 10
52
+ req.options.boundary = 'boo'
53
+ req.options.oauth[:consumer_secret] = 'xyz'
54
54
  end
55
- assert_equal 10, env[:request][:timeout]
56
- assert_equal 5, env[:request][:open_timeout]
57
- assert_equal true, env[:request][:custom]
55
+ assert_equal 10, env.request.timeout
56
+ assert_equal 5, env.request.open_timeout
57
+ assert_equal 'boo', env.request.boundary
58
58
 
59
59
  oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'}
60
- assert_equal oauth_expected, env[:request][:oauth]
60
+ assert_equal oauth_expected, env.request.oauth
61
61
  end
62
62
 
63
63
  def test_request_create_stores_ssl_options
64
64
  env = make_env
65
- assert_equal false, env[:ssl][:verify]
65
+ assert_equal false, env.ssl.verify
66
66
  end
67
67
 
68
68
  def test_request_create_stores_proxy_options
69
69
  env = make_env
70
- assert_equal 'proxy.com', env[:request][:proxy][:uri].host
70
+ assert_equal 'proxy.com', env.request.proxy.host
71
71
  end
72
72
 
73
73
  private
@@ -98,6 +98,25 @@ class HeadersTest < Faraday::TestCase
98
98
  assert_equal 'application/xml', @headers['content-type']
99
99
  end
100
100
 
101
+ def test_fetch_key
102
+ @headers['Content-Type'] = 'application/json'
103
+ block_called = false
104
+ assert_equal 'application/json', @headers.fetch('content-type') { block_called = true }
105
+ assert_equal 'application/json', @headers.fetch('Content-Type')
106
+ assert_equal 'application/json', @headers.fetch('CONTENT-TYPE')
107
+ assert_equal 'application/json', @headers.fetch(:content_type)
108
+ assert_equal false, block_called
109
+
110
+ assert_equal 'default', @headers.fetch('invalid', 'default')
111
+ assert_equal false, @headers.fetch('invalid', false)
112
+ assert_nil @headers.fetch('invalid', nil)
113
+
114
+ assert_equal 'Invalid key', @headers.fetch('Invalid') { |key| "#{key} key" }
115
+
116
+ expected_error = defined?(KeyError) ? KeyError : IndexError
117
+ assert_raises(expected_error) { @headers.fetch('invalid') }
118
+ end
119
+
101
120
  def test_delete_key
102
121
  @headers['Content-Type'] = 'application/json'
103
122
  assert_equal 1, @headers.size
@@ -131,10 +150,9 @@ end
131
150
 
132
151
  class ResponseTest < Faraday::TestCase
133
152
  def setup
134
- @env = {
153
+ @env = Faraday::Env.from \
135
154
  :status => 404, :body => 'yikes',
136
- :response_headers => Faraday::Utils::Headers.new('Content-Type' => 'text/plain')
137
- }
155
+ :response_headers => {'Content-Type' => 'text/plain'}
138
156
  @response = Faraday::Response.new @env
139
157
  end
140
158
 
@@ -174,10 +192,19 @@ class ResponseTest < Faraday::TestCase
174
192
  def test_marshal
175
193
  @response = Faraday::Response.new
176
194
  @response.on_complete { }
177
- @response.finish @env.merge(:custom => 'moo')
195
+ @response.finish @env.merge(:params => 'moo')
178
196
 
179
197
  loaded = Marshal.load Marshal.dump(@response)
180
- assert_nil loaded.env[:custom]
198
+ assert_nil loaded.env[:params]
181
199
  assert_equal %w[body response_headers status], loaded.env.keys.map { |k| k.to_s }.sort
182
200
  end
201
+
202
+ def test_hash
203
+ hash = @response.to_hash
204
+ assert_kind_of Hash, hash
205
+ assert_equal @env.to_hash, hash
206
+ assert_equal hash[:status], @response.status
207
+ assert_equal hash[:response_headers], @response.headers
208
+ assert_equal hash[:body], @response.body
209
+ end
183
210
  end
data/test/helper.rb CHANGED
@@ -1,14 +1,18 @@
1
- if RUBY_VERSION >= '1.9'
2
- require 'simplecov'
1
+ require 'rubygems' # rubygems/version doesn't work by itself
2
+ require 'rubygems/version' # for simplecov-html
3
+ require 'simplecov'
4
+ require 'coveralls'
3
5
 
4
- SimpleCov.start do
5
- add_filter '/bundle/'
6
- add_filter '/test/'
7
- minimum_coverage(88.74)
8
- end
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+ SimpleCov.start do
11
+ add_filter '/bundle/'
9
12
  end
10
13
 
11
- require 'test/unit'
14
+ gem 'minitest' if defined? Bundler
15
+ require 'minitest/autorun'
12
16
 
13
17
  if ENV['LEFTRIGHT']
14
18
  begin
@@ -44,7 +48,7 @@ module Faraday
44
48
  end
45
49
  end
46
50
 
47
- class TestCase < Test::Unit::TestCase
51
+ class TestCase < MiniTest::Test
48
52
  extend LiveServerConfig
49
53
  self.live_server = ENV['LIVE']
50
54
 
@@ -70,10 +74,6 @@ module Faraday
70
74
  defined? RUBY_ENGINE and 'rbx' == RUBY_ENGINE
71
75
  end
72
76
 
73
- def self.ruby_22_plus?
74
- RUBY_VERSION > '2.2'
75
- end
76
-
77
77
  def self.ssl_mode?
78
78
  ENV['SSL'] == 'yes'
79
79
  end
data/test/live_server.rb CHANGED
@@ -39,11 +39,19 @@ class LiveServer < Sinatra::Base
39
39
  [200, { 'Set-Cookie' => 'one, two' }, '']
40
40
  end
41
41
 
42
+ get '/who-am-i' do
43
+ request.env['REMOTE_ADDR']
44
+ end
45
+
42
46
  get '/slow' do
43
47
  sleep 10
44
48
  [200, {}, 'ok']
45
49
  end
46
50
 
51
+ get '/204' do
52
+ status 204 # no content
53
+ end
54
+
47
55
  get '/ssl' do
48
56
  request.secure?.to_s
49
57
  end