faraday 0.7.6 → 0.8.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,7 +15,7 @@ module Adapters
15
15
  stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
16
16
  end
17
17
  end
18
- @resp = @conn.get '/hello', :accept => 'text/html'
18
+ @resp = @conn.get '/hello', nil, :accept => 'text/html'
19
19
  end
20
20
 
21
21
  def test_still_returns_output
@@ -23,7 +23,7 @@ module Adapters
23
23
  end
24
24
 
25
25
  def test_logs_method_and_url
26
- assert_match "get /hello", @io.string
26
+ assert_match "get http:/hello", @io.string
27
27
  end
28
28
 
29
29
  def test_logs_request_headers
@@ -13,7 +13,7 @@ module Adapters
13
13
  stub_request(:get, 'disney.com/world').with(:headers => {'User-Agent'=>'Faraday Agent'}){ |request|
14
14
  request.headers["User-Agent"] == 'Faraday Agent'
15
15
  }
16
- @connection.get('/world', :user_agent => 'Faraday Agent')
16
+ @connection.get('/world', nil, :user_agent => 'Faraday Agent')
17
17
  end
18
18
 
19
19
  end if defined? ::Typhoeus
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ class AuthenticationMiddlewareTest < Faraday::TestCase
4
+ def conn
5
+ Faraday::Connection.new('http://example.net/') do |builder|
6
+ yield builder
7
+ builder.adapter :test do |stub|
8
+ stub.get('/auth-echo') do |env|
9
+ [200, {}, env[:request_headers]['Authorization']]
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def test_basic_middleware_adds_basic_header
16
+ response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.get('/auth-echo')
17
+ assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', response.body
18
+ end
19
+
20
+ def test_basic_middleware_adds_basic_header_correctly_with_long_values
21
+ response = conn { |b| b.request :basic_auth, 'A' * 255, '' }.get('/auth-echo')
22
+ assert_equal "Basic #{'QUFB' * 85}Og==", response.body
23
+ end
24
+
25
+ def test_basic_middleware_does_not_interfere_with_existing_authorization
26
+ response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.
27
+ get('/auth-echo', nil, :authorization => 'Token token="bar"')
28
+ assert_equal 'Token token="bar"', response.body
29
+ end
30
+
31
+ def test_token_middleware_adds_token_header
32
+ response = conn { |b| b.request :token_auth, 'quux' }.get('/auth-echo')
33
+ assert_equal 'Token token="quux"', response.body
34
+ end
35
+
36
+ def test_token_middleware_includes_other_values_if_provided
37
+ response = conn { |b|
38
+ b.request :token_auth, 'baz', :foo => 42
39
+ }.get('/auth-echo')
40
+ assert_equal "Token token=\"baz\",\n foo=\"42\"", response.body
41
+ end
42
+
43
+ def test_token_middleware_does_not_interfere_with_existing_authorization
44
+ response = conn { |b| b.request :token_auth, 'quux' }.
45
+ get('/auth-echo', nil, :authorization => 'Token token="bar"')
46
+ assert_equal 'Token token="bar"', response.body
47
+ end
48
+ end
@@ -2,14 +2,25 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
2
  require 'uri'
3
3
 
4
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
+
5
16
  def test_initialize_parses_host_out_of_given_url
6
17
  conn = Faraday::Connection.new "http://sushi.com"
7
18
  assert_equal 'sushi.com', conn.host
8
19
  end
9
20
 
10
- def test_initialize_parses_nil_port_out_of_given_url
21
+ def test_initialize_inherits_default_port_out_of_given_url
11
22
  conn = Faraday::Connection.new "http://sushi.com"
12
- assert_nil conn.port
23
+ assert_equal 80, conn.port
13
24
  end
14
25
 
15
26
  def test_initialize_parses_scheme_out_of_given_url
@@ -57,39 +68,24 @@ class TestConnection < Faraday::TestCase
57
68
  assert_equal 'Faraday', conn.headers['User-agent']
58
69
  end
59
70
 
60
- def test_basic_auth_sets_authorization_header
71
+ def test_basic_auth_prepends_basic_auth_middleware
61
72
  conn = Faraday::Connection.new
62
73
  conn.basic_auth 'Aladdin', 'open sesame'
63
- assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
64
- end
65
-
66
- def test_long_basic_auth_sets_authorization_header_without_new_lines
67
- conn = Faraday::Connection.new
68
- conn.basic_auth "A" * 255, ""
69
- assert_equal "Basic #{'QUFB' * 85}Og==", conn.headers['Authorization']
70
- end
71
-
72
- def test_auto_parses_basic_auth_from_url
73
- conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
74
- assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
74
+ assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
75
+ assert_equal ['Aladdin', 'open sesame'], conn.builder[0].instance_eval { @args }
75
76
  end
76
77
 
77
78
  def test_auto_parses_basic_auth_from_url_and_unescapes
78
79
  conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
79
- assert_equal 'Basic Zm9vQGJhci5jb206cGFzcyB3b3Jk', conn.headers['Authorization']
80
- end
81
-
82
- def test_token_auth_sets_authorization_header
83
- conn = Faraday::Connection.new
84
- conn.token_auth 'abcdef'
85
- assert_equal 'Token token="abcdef"', conn.headers['Authorization']
80
+ assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
81
+ assert_equal ['foo@bar.com', 'pass word'], conn.builder[0].instance_eval { @args }
86
82
  end
87
83
 
88
- def test_token_auth_with_options_sets_authorization_header
84
+ def test_token_auth_prepends_token_auth_middleware
89
85
  conn = Faraday::Connection.new
90
86
  conn.token_auth 'abcdef', :nonce => 'abc'
91
- assert_equal 'Token token="abcdef",
92
- nonce="abc"', conn.headers['Authorization']
87
+ assert_equal Faraday::Request::TokenAuthentication, conn.builder[0].klass
88
+ assert_equal ['abcdef', { :nonce => 'abc' }], conn.builder[0].instance_eval { @args }
93
89
  end
94
90
 
95
91
  def test_build_url_uses_connection_host_as_default_uri_host
@@ -99,11 +95,11 @@ class TestConnection < Faraday::TestCase
99
95
  assert_equal 'sushi.com', uri.host
100
96
  end
101
97
 
102
- def test_build_url_uses_connection_port_as_default_uri_port
98
+ def test_build_url_overrides_connection_port_for_absolute_urls
103
99
  conn = Faraday::Connection.new
104
100
  conn.port = 23
105
101
  uri = conn.build_url("http://sushi.com")
106
- assert_equal 23, uri.port
102
+ assert_equal 80, uri.port
107
103
  end
108
104
 
109
105
  def test_build_url_uses_connection_scheme_as_default_uri_scheme
@@ -170,36 +166,21 @@ class TestConnection < Faraday::TestCase
170
166
  def test_build_url_parses_url_params_into_query
171
167
  conn = Faraday::Connection.new
172
168
  uri = conn.build_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
173
- assert_equal "a%5Bb%5D=1%20%2B%202", uri.query
174
- end
175
-
176
- def test_build_url_mashes_default_and_given_params_together
177
- conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
178
- url = conn.build_url("nigiri?page=1", :limit => 5)
179
- assert_equal %w[format=json limit=5 page=1 token=abc], url.query.split('&').sort
180
- end
181
-
182
- def test_build_url_overrides_default_params_with_given_params
183
- conn = Faraday::Connection.new 'http://sushi.com/api?token=abc', :params => {'format' => 'json'}
184
- url = conn.build_url("nigiri?page=1", :limit => 5, :token => 'def', :format => 'xml')
185
- assert_equal %w[format=xml limit=5 page=1 token=def], url.query.split('&').sort
169
+ assert_equal "a%5Bb%5D=1+%2B+2", uri.query
186
170
  end
187
171
 
188
- def test_default_params_hash_has_indifferent_access
189
- conn = Faraday::Connection.new :params => {'format' => 'json'}
190
- assert conn.params.has_key?(:format)
191
- conn.params[:format] = 'xml'
192
- url = conn.build_url("")
193
- assert_equal %w[format=xml], url.query.split('&').sort
172
+ def test_build_url_bracketizes_nested_params_in_query
173
+ conn = Faraday::Connection.new
174
+ uri = conn.build_url("http://sushi.com/sake.html", 'a' => {'b' => 'c'})
175
+ assert_equal "a%5Bb%5D=c", uri.query
194
176
  end
195
177
 
196
178
  def test_build_url_parses_url
197
179
  conn = Faraday::Connection.new
198
180
  uri = conn.build_url("http://sushi.com/sake.html")
199
- assert_equal "http", uri.scheme
200
- assert_equal "sushi.com", uri.host
181
+ assert_equal "http", uri.scheme
182
+ assert_equal "sushi.com", uri.host
201
183
  assert_equal '/sake.html', uri.path
202
- assert_nil uri.port
203
184
  end
204
185
 
205
186
  def test_build_url_parses_url_and_changes_scheme
@@ -216,38 +197,46 @@ class TestConnection < Faraday::TestCase
216
197
  end
217
198
 
218
199
  def test_proxy_accepts_string
219
- conn = Faraday::Connection.new
220
- conn.proxy 'http://proxy.com'
221
- assert_equal 'proxy.com', conn.proxy[:uri].host
222
- assert_equal [:uri], conn.proxy.keys
223
- end
224
-
225
- def test_proxy_accepts_addressable_uri
226
- conn = Faraday::Connection.new
227
- conn.proxy Addressable::URI.parse('http://proxy.com')
228
- assert_equal 'proxy.com', conn.proxy[:uri].host
229
- assert_equal [:uri], conn.proxy.keys
200
+ with_proxy_env "http://duncan.proxy.com:80" do
201
+ conn = Faraday::Connection.new
202
+ conn.proxy 'http://proxy.com'
203
+ assert_equal 'proxy.com', conn.proxy[:uri].host
204
+ assert_equal [:uri], conn.proxy.keys
205
+ end
230
206
  end
231
207
 
232
- def test_proxy_accepts_stdlib_uri
233
- conn = Faraday::Connection.new
234
- conn.proxy URI('http://proxy.com')
235
- assert_equal 'proxy.com', conn.proxy[:uri].host
236
- assert_equal [:uri], conn.proxy.keys
208
+ def test_proxy_accepts_uri
209
+ with_proxy_env "http://duncan.proxy.com:80" do
210
+ conn = Faraday::Connection.new
211
+ conn.proxy URI.parse('http://proxy.com')
212
+ assert_equal 'proxy.com', conn.proxy[:uri].host
213
+ assert_equal [:uri], conn.proxy.keys
214
+ end
237
215
  end
238
216
 
239
217
  def test_proxy_accepts_hash_with_string_uri
240
- conn = Faraday::Connection.new
241
- conn.proxy :uri => 'http://proxy.com', :user => 'rick'
242
- assert_equal 'proxy.com', conn.proxy[:uri].host
243
- assert_equal 'rick', conn.proxy[:user]
218
+ with_proxy_env "http://duncan.proxy.com:80" do
219
+ conn = Faraday::Connection.new
220
+ conn.proxy :uri => 'http://proxy.com', :user => 'rick'
221
+ assert_equal 'proxy.com', conn.proxy[:uri].host
222
+ assert_equal 'rick', conn.proxy[:user]
223
+ end
244
224
  end
245
225
 
246
226
  def test_proxy_accepts_hash
247
- conn = Faraday::Connection.new
248
- conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
249
- assert_equal 'proxy.com', conn.proxy[:uri].host
250
- assert_equal 'rick', conn.proxy[:user]
227
+ with_proxy_env "http://duncan.proxy.com:80" do
228
+ conn = Faraday::Connection.new
229
+ conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
230
+ assert_equal 'proxy.com', conn.proxy[:uri].host
231
+ assert_equal 'rick', conn.proxy[:user]
232
+ end
233
+ end
234
+
235
+ def test_proxy_accepts_http_env
236
+ with_proxy_env "http://duncan.proxy.com:80" do
237
+ conn = Faraday::Connection.new
238
+ assert_equal 'duncan.proxy.com', conn.proxy[:uri].host
239
+ end
251
240
  end
252
241
 
253
242
  def test_proxy_requires_uri
@@ -260,22 +249,121 @@ class TestConnection < Faraday::TestCase
260
249
  def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
261
250
  conn = Faraday::Connection.new
262
251
  url = conn.build_url('', 'a[b]' => '1 + 2')
263
- assert_equal "a%5Bb%5D=1%20%2B%202", url.query
252
+ assert_equal "a%5Bb%5D=1+%2B+2", url.query
264
253
  end
265
254
 
266
255
  def test_dups_connection_object
267
- conn = Faraday::Connection.new 'http://sushi.com/foo', :ssl => { :verify => :none } do |b|
268
- b.adapter :net_http
256
+ conn = Faraday::Connection.new 'http://sushi.com/foo',
257
+ :ssl => { :verify => :none },
258
+ :headers => {'content-type' => 'text/plain'},
259
+ :params => {'a'=>'1'}
260
+
261
+ other = conn.dup
262
+
263
+ assert_equal conn.build_url(''), other.build_url('')
264
+ assert_equal 'text/plain', other.headers['content-type']
265
+ assert_equal '1', other.params['a']
266
+
267
+ other.basic_auth('', '')
268
+ other.headers['content-length'] = 12
269
+ other.params['b'] = '2'
270
+
271
+ assert_equal 3, other.builder.handlers.size
272
+ assert_equal 2, conn.builder.handlers.size
273
+ assert !conn.headers.key?('content-length')
274
+ assert !conn.params.key?('b')
275
+ end
276
+
277
+ def test_init_with_block
278
+ conn = Faraday::Connection.new { }
279
+ assert_equal 0, conn.builder.handlers.size
280
+ end
281
+
282
+ def test_init_with_block_yields_connection
283
+ conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
284
+ faraday.adapter :net_http
285
+ faraday.url_prefix = 'http://sushi.com/omnom'
286
+ assert_equal '1', faraday.params['a']
287
+ }
288
+ assert_equal 1, conn.builder.handlers.size
289
+ assert_equal '/omnom', conn.path_prefix
290
+ end
291
+ end
292
+
293
+ class TestRequestParams < Faraday::TestCase
294
+ def create_connection(*args)
295
+ @conn = Faraday::Connection.new(*args) do |conn|
296
+ yield conn if block_given?
297
+ class << conn
298
+ undef app
299
+ def app() lambda { |env| env } end
300
+ end
301
+ end
302
+ end
303
+
304
+ def get(*args)
305
+ env = @conn.get(*args) do |req|
306
+ yield req if block_given?
269
307
  end
270
- conn.headers['content-type'] = 'text/plain'
271
- conn.params['a'] = '1'
308
+ env[:url].query
309
+ end
310
+
311
+ def assert_query_equal(expected, query)
312
+ assert_equal expected, query.split('&').sort
313
+ end
272
314
 
273
- duped = conn.dup
315
+ def test_merges_connection_and_request_params
316
+ create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
317
+ query = get '?page=1', :limit => 5
318
+ assert_query_equal %w[format=json limit=5 page=1 token=abc], query
319
+ end
320
+
321
+ def test_overrides_connection_params
322
+ create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
323
+ conn.params[:b] = 'B'
324
+ assert_equal 'c', conn.params[:c]
325
+ end
326
+ assert_query_equal %w[a=A b=B c=c], get
327
+ end
328
+
329
+ def test_all_overrides_connection_params
330
+ create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
331
+ conn.params = {'b' => 'b'}
332
+ end
333
+ assert_query_equal %w[b=b], get
334
+ end
335
+
336
+ def test_overrides_request_params
337
+ create_connection
338
+ query = get '?p=1&a=a', :p => 2
339
+ assert_query_equal %w[a=a p=2], query
340
+ end
341
+
342
+ def test_overrides_request_params_block
343
+ create_connection
344
+ query = get '?p=1&a=a', :p => 2 do |req|
345
+ req.params[:p] = 3
346
+ end
347
+ assert_query_equal %w[a=a p=3], query
348
+ end
349
+
350
+ def test_overrides_request_params_block_url
351
+ create_connection
352
+ query = get nil, :p => 2 do |req|
353
+ req.url '?p=1&a=a', 'p' => 3
354
+ end
355
+ assert_query_equal %w[a=a p=3], query
356
+ end
274
357
 
275
- assert_equal conn.build_url(''), duped.build_url('')
276
- [:headers, :params, :builder, :ssl].each do |attr|
277
- assert_equal conn.send(attr), duped.send(attr)
278
- assert_not_equal conn.send(attr).object_id, duped.send(attr).object_id
358
+ def test_overrides_all_request_params
359
+ create_connection :params => {:c => 'c'}
360
+ query = get '?p=1&a=a', :p => 2 do |req|
361
+ assert_equal 'a', req.params[:a]
362
+ assert_equal 'c', req.params['c']
363
+ assert_equal 2, req.params['p']
364
+ req.params = {:b => 'b'}
365
+ assert_equal 'b', req.params['b']
279
366
  end
367
+ assert_query_equal %w[b=b], query
280
368
  end
281
369
  end
@@ -17,7 +17,7 @@ class EnvTest < Faraday::TestCase
17
17
  assert_equal :get, env[:method]
18
18
  end
19
19
 
20
- def test_request_create_stores_addressable_uri
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
@@ -50,7 +50,7 @@ class EnvTest < Faraday::TestCase
50
50
  env = make_env do |req|
51
51
  req.options[:timeout] = 10
52
52
  req.options[:custom] = true
53
- req.options[:oauth] = {:consumer_secret => 'xyz'}
53
+ req.options[:oauth][:consumer_secret] = 'xyz'
54
54
  end
55
55
  assert_equal 10, env[:request][:timeout]
56
56
  assert_equal 5, env[:request][:open_timeout]
@@ -73,7 +73,7 @@ class EnvTest < Faraday::TestCase
73
73
  private
74
74
 
75
75
  def make_env(method = :get, connection = @conn, &block)
76
- request = Faraday::Request.create(method, &block)
76
+ request = connection.build_request(method, &block)
77
77
  request.to_env(connection)
78
78
  end
79
79
  end
@@ -83,6 +83,31 @@ class HeadersTest < Faraday::TestCase
83
83
  @headers = Faraday::Utils::Headers.new
84
84
  end
85
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
+
86
111
  def test_parse_response_headers_leaves_http_status_line_out
87
112
  @headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
88
113
  assert_equal %w(Content-Type), @headers.keys