faraday 0.9.1 → 0.9.2

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 (62) hide show
  1. data/LICENSE.md +1 -1
  2. data/README.md +41 -16
  3. data/lib/faraday.rb +1 -1
  4. data/lib/faraday/adapter/em_synchrony.rb +8 -0
  5. data/lib/faraday/adapter/excon.rb +1 -0
  6. data/lib/faraday/adapter/httpclient.rb +14 -3
  7. data/lib/faraday/adapter/net_http.rb +2 -2
  8. data/lib/faraday/adapter/net_http_persistent.rb +2 -1
  9. data/lib/faraday/adapter/patron.rb +8 -2
  10. data/lib/faraday/connection.rb +8 -3
  11. data/lib/faraday/options.rb +9 -0
  12. data/lib/faraday/parameters.rb +54 -38
  13. data/lib/faraday/rack_builder.rb +3 -2
  14. data/lib/faraday/request/retry.rb +9 -3
  15. data/lib/faraday/response.rb +2 -2
  16. data/lib/faraday/utils.rb +13 -1
  17. metadata +61 -124
  18. checksums.yaml +0 -7
  19. data/.document +0 -6
  20. data/CHANGELOG.md +0 -20
  21. data/CONTRIBUTING.md +0 -36
  22. data/Gemfile +0 -25
  23. data/Rakefile +0 -71
  24. data/faraday.gemspec +0 -34
  25. data/script/cached-bundle +0 -46
  26. data/script/console +0 -7
  27. data/script/generate_certs +0 -42
  28. data/script/package +0 -7
  29. data/script/proxy-server +0 -42
  30. data/script/release +0 -17
  31. data/script/s3-put +0 -71
  32. data/script/server +0 -36
  33. data/script/test +0 -172
  34. data/test/adapters/default_test.rb +0 -14
  35. data/test/adapters/em_http_test.rb +0 -20
  36. data/test/adapters/em_synchrony_test.rb +0 -20
  37. data/test/adapters/excon_test.rb +0 -20
  38. data/test/adapters/httpclient_test.rb +0 -21
  39. data/test/adapters/integration.rb +0 -254
  40. data/test/adapters/logger_test.rb +0 -82
  41. data/test/adapters/net_http_persistent_test.rb +0 -20
  42. data/test/adapters/net_http_test.rb +0 -14
  43. data/test/adapters/patron_test.rb +0 -20
  44. data/test/adapters/rack_test.rb +0 -31
  45. data/test/adapters/test_middleware_test.rb +0 -114
  46. data/test/adapters/typhoeus_test.rb +0 -28
  47. data/test/authentication_middleware_test.rb +0 -65
  48. data/test/composite_read_io_test.rb +0 -111
  49. data/test/connection_test.rb +0 -522
  50. data/test/env_test.rb +0 -218
  51. data/test/helper.rb +0 -81
  52. data/test/live_server.rb +0 -67
  53. data/test/middleware/instrumentation_test.rb +0 -88
  54. data/test/middleware/retry_test.rb +0 -177
  55. data/test/middleware_stack_test.rb +0 -173
  56. data/test/multibyte.txt +0 -1
  57. data/test/options_test.rb +0 -252
  58. data/test/parameters_test.rb +0 -64
  59. data/test/request_middleware_test.rb +0 -142
  60. data/test/response_middleware_test.rb +0 -72
  61. data/test/strawberry.rb +0 -2
  62. data/test/utils_test.rb +0 -58
@@ -1,14 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class NetHttpTest < Faraday::TestCase
5
-
6
- def adapter() :net_http end
7
-
8
- behaviors = [:NonParallel]
9
- behaviors << :Compression if RUBY_VERSION >= '1.9'
10
-
11
- Integration.apply(self, *behaviors)
12
-
13
- end
14
- end
@@ -1,20 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class Patron < Faraday::TestCase
5
-
6
- def adapter() :patron end
7
-
8
- Integration.apply(self, :NonParallel) do
9
- # https://github.com/toland/patron/issues/34
10
- undef :test_PATCH_send_url_encoded_params
11
-
12
- # https://github.com/toland/patron/issues/52
13
- undef :test_GET_with_body
14
-
15
- # no support for SSL peer verification
16
- undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
17
- end unless jruby?
18
-
19
- end
20
- end
@@ -1,31 +0,0 @@
1
- require File.expand_path("../integration", __FILE__)
2
- require File.expand_path('../../live_server', __FILE__)
3
-
4
- module Adapters
5
- class RackTest < Faraday::TestCase
6
-
7
- def adapter() :rack end
8
-
9
- def adapter_options
10
- [Faraday::LiveServer]
11
- end
12
-
13
- # no Integration.apply because this doesn't require a server as a separate process
14
- include Integration::Common
15
- include Integration::NonParallel
16
-
17
- # not using shared test because error is swallowed by Sinatra
18
- def test_timeout
19
- conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
20
- begin
21
- conn.get '/slow'
22
- rescue Faraday::Error::ClientError
23
- end
24
- end
25
-
26
- # test not applicable
27
- undef test_connection_error
28
- undef test_proxy
29
- undef test_proxy_auth_fail
30
- end
31
- end
@@ -1,114 +0,0 @@
1
- require File.expand_path('../../helper', __FILE__)
2
-
3
- module Adapters
4
- class TestMiddleware < Faraday::TestCase
5
- Stubs = Faraday::Adapter.lookup_middleware(:test)::Stubs
6
- def setup
7
- @stubs = Stubs.new
8
- @conn = Faraday.new do |builder|
9
- builder.adapter :test, @stubs
10
- end
11
- @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
12
- @resp = @conn.get('/hello')
13
- end
14
-
15
- def test_middleware_with_simple_path_sets_status
16
- assert_equal 200, @resp.status
17
- end
18
-
19
- def test_middleware_with_simple_path_sets_headers
20
- assert_equal 'text/html', @resp.headers['Content-Type']
21
- end
22
-
23
- def test_middleware_with_simple_path_sets_body
24
- assert_equal 'hello', @resp.body
25
- end
26
-
27
- def test_middleware_can_be_called_several_times
28
- assert_equal 'hello', @conn.get("/hello").body
29
- end
30
-
31
- def test_middleware_with_get_params
32
- @stubs.get('/param?a=1') { [200, {}, 'a'] }
33
- assert_equal 'a', @conn.get('/param?a=1').body
34
- end
35
-
36
- def test_middleware_ignores_unspecified_get_params
37
- @stubs.get('/optional?a=1') { [200, {}, 'a'] }
38
- assert_equal 'a', @conn.get('/optional?a=1&b=1').body
39
- assert_equal 'a', @conn.get('/optional?a=1').body
40
- assert_raises Faraday::Adapter::Test::Stubs::NotFound do
41
- @conn.get('/optional')
42
- end
43
- end
44
-
45
- def test_middleware_with_http_headers
46
- @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
47
- @stubs.get('/yo') { [200, {}, 'b'] }
48
- assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
49
- assert_equal 'b', @conn.get('/yo').body
50
- end
51
-
52
- def test_middleware_allow_different_outcomes_for_the_same_request
53
- @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
54
- @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
55
- assert_equal 'hello', @conn.get("/hello").body
56
- assert_equal 'world', @conn.get("/hello").body
57
- end
58
-
59
- def test_yields_env_to_stubs
60
- @stubs.get '/hello' do |env|
61
- assert_equal '/hello', env[:url].path
62
- assert_equal 'foo.com', env[:url].host
63
- assert_equal '1', env[:params]['a']
64
- assert_equal 'text/plain', env[:request_headers]['Accept']
65
- [200, {}, 'a']
66
- end
67
-
68
- @conn.headers['Accept'] = 'text/plain'
69
- assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
70
- end
71
-
72
- def test_parses_params_with_default_encoder
73
- @stubs.get '/hello' do |env|
74
- assert_equal '1', env[:params]['a']['b']
75
- [200, {}, 'a']
76
- end
77
-
78
- assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
79
- end
80
-
81
- def test_parses_params_with_nested_encoder
82
- @stubs.get '/hello' do |env|
83
- assert_equal '1', env[:params]['a']['b']
84
- [200, {}, 'a']
85
- end
86
-
87
- @conn.options.params_encoder = Faraday::NestedParamsEncoder
88
- assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
89
- end
90
-
91
- def test_parses_params_with_flat_encoder
92
- @stubs.get '/hello' do |env|
93
- assert_equal '1', env[:params]['a[b]']
94
- [200, {}, 'a']
95
- end
96
-
97
- @conn.options.params_encoder = Faraday::FlatParamsEncoder
98
- assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
99
- end
100
-
101
- def test_raises_an_error_if_no_stub_is_found_for_request
102
- assert_raises Stubs::NotFound do
103
- @conn.get('/invalid'){ [200, {}, []] }
104
- end
105
- end
106
-
107
- def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
108
- @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
109
- assert_raises Faraday::Adapter::Test::Stubs::NotFound do
110
- @conn.get('/yo')
111
- end
112
- end
113
- end
114
- end
@@ -1,28 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class TyphoeusTest < Faraday::TestCase
5
-
6
- def adapter() :typhoeus end
7
-
8
- Integration.apply(self, :Parallel) do
9
- # https://github.com/dbalatero/typhoeus/issues/75
10
- undef :test_GET_with_body
11
-
12
- # Not a Typhoeus bug, but WEBrick inability to handle "100-continue"
13
- # which libcurl seems to generate for this particular request:
14
- undef :test_POST_sends_files
15
-
16
- # inconsistent outcomes ranging from successful response to connection error
17
- undef :test_proxy_auth_fail if ssl_mode?
18
-
19
- def test_binds_local_socket
20
- host = '1.2.3.4'
21
- conn = create_connection :request => { :bind => { :host => host } }
22
- assert_equal host, conn.options[:bind][:host]
23
- end
24
-
25
- end unless jruby?
26
- end
27
- end
28
-
@@ -1,65 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
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_match(/^Token /, response.body)
41
- assert_match(/token="baz"/, response.body)
42
- assert_match(/foo="42"/, response.body)
43
- end
44
-
45
- def test_token_middleware_does_not_interfere_with_existing_authorization
46
- response = conn { |b| b.request :token_auth, 'quux' }.
47
- get('/auth-echo', nil, :authorization => 'Token token="bar"')
48
- assert_equal 'Token token="bar"', response.body
49
- end
50
-
51
- def test_authorization_middleware_with_string
52
- response = conn { |b|
53
- b.request :authorization, 'custom', 'abc def'
54
- }.get('/auth-echo')
55
- assert_match(/^custom abc def$/, response.body)
56
- end
57
-
58
- def test_authorization_middleware_with_hash
59
- response = conn { |b|
60
- b.request :authorization, 'baz', :foo => 42
61
- }.get('/auth-echo')
62
- assert_match(/^baz /, response.body)
63
- assert_match(/foo="42"/, response.body)
64
- end
65
- end
@@ -1,111 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
- require 'stringio'
3
-
4
- class CompositeReadIOTest < Faraday::TestCase
5
- Part = Struct.new(:to_io) do
6
- def length() to_io.string.length end
7
- end
8
-
9
- def part(str)
10
- Part.new StringIO.new(str)
11
- end
12
-
13
- def composite_io(*parts)
14
- Faraday::CompositeReadIO.new(*parts)
15
- end
16
-
17
- def test_empty
18
- io = composite_io
19
- assert_equal 0, io.length
20
- assert_equal "", io.read
21
- end
22
-
23
- def test_empty_returns_nil_for_limited_read
24
- assert_nil composite_io.read(1)
25
- end
26
-
27
- def test_empty_parts_returns_nil_for_limited_read
28
- io = composite_io(part(""), part(""))
29
- assert_nil io.read(1)
30
- end
31
-
32
- def test_multipart_read_all
33
- io = composite_io(part("abcd"), part("1234"))
34
- assert_equal 8, io.length
35
- assert_equal "abcd1234", io.read
36
- end
37
-
38
- def test_multipart_read_limited
39
- io = composite_io(part("abcd"), part("1234"))
40
- assert_equal "abc", io.read(3)
41
- assert_equal "d12", io.read(3)
42
- assert_equal "34", io.read(3)
43
- assert_equal nil, io.read(3)
44
- assert_equal nil, io.read(3)
45
- end
46
-
47
- def test_multipart_read_limited_size_larger_than_part
48
- io = composite_io(part("abcd"), part("1234"))
49
- assert_equal "abcd12", io.read(6)
50
- assert_equal "34", io.read(6)
51
- assert_equal nil, io.read(6)
52
- end
53
-
54
- def test_multipart_read_with_blank_parts
55
- io = composite_io(part(""), part("abcd"), part(""), part("1234"), part(""))
56
- assert_equal "abcd12", io.read(6)
57
- assert_equal "34", io.read(6)
58
- assert_equal nil, io.read(6)
59
- end
60
-
61
- def test_multipart_rewind
62
- io = composite_io(part("abcd"), part("1234"))
63
- assert_equal "abc", io.read(3)
64
- assert_equal "d12", io.read(3)
65
- io.rewind
66
- assert_equal "abc", io.read(3)
67
- assert_equal "d1234", io.read(5)
68
- assert_equal nil, io.read(3)
69
- io.rewind
70
- assert_equal "ab", io.read(2)
71
- end
72
-
73
- # JRuby enforces types to copy_stream to be String or IO
74
- if IO.respond_to?(:copy_stream) && !jruby?
75
- def test_compatible_with_copy_stream
76
- target_io = StringIO.new
77
- def target_io.ensure_open_and_writable
78
- # Rubinius compatibility
79
- end
80
- io = composite_io(part("abcd"), part("1234"))
81
-
82
- Faraday::Timer.timeout(1) do
83
- IO.copy_stream(io, target_io)
84
- end
85
- assert_equal "abcd1234", target_io.string
86
- end
87
- end
88
-
89
- unless RUBY_VERSION < '1.9'
90
- def test_read_from_multibyte
91
- File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
92
- io = composite_io(part("\x86"), Part.new(utf8))
93
- assert_equal bin("\x86\xE3\x83\x95\xE3\x82\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read
94
- end
95
- end
96
-
97
- def test_limited_from_multibyte
98
- File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
99
- io = composite_io(part("\x86"), Part.new(utf8))
100
- assert_equal bin("\x86\xE3\x83"), io.read(3)
101
- assert_equal bin("\x95\xE3\x82"), io.read(3)
102
- assert_equal bin("\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read(8)
103
- end
104
- end
105
- end
106
-
107
- def bin(str)
108
- str.force_encoding("BINARY") if str.respond_to?(:force_encoding)
109
- str
110
- end
111
- end
@@ -1,522 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
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
-
19
- def test_initialize_parses_host_out_of_given_url
20
- conn = Faraday::Connection.new "http://sushi.com"
21
- assert_equal 'sushi.com', conn.host
22
- end
23
-
24
- def test_initialize_inherits_default_port_out_of_given_url
25
- conn = Faraday::Connection.new "http://sushi.com"
26
- assert_equal 80, conn.port
27
- end
28
-
29
- def test_initialize_parses_scheme_out_of_given_url
30
- conn = Faraday::Connection.new "http://sushi.com"
31
- assert_equal 'http', conn.scheme
32
- end
33
-
34
- def test_initialize_parses_port_out_of_given_url
35
- conn = Faraday::Connection.new "http://sushi.com:815"
36
- assert_equal 815, conn.port
37
- end
38
-
39
- def test_initialize_parses_nil_path_prefix_out_of_given_url
40
- conn = Faraday::Connection.new "http://sushi.com"
41
- assert_equal '/', conn.path_prefix
42
- end
43
-
44
- def test_initialize_parses_path_prefix_out_of_given_url
45
- conn = Faraday::Connection.new "http://sushi.com/fish"
46
- assert_equal '/fish', conn.path_prefix
47
- end
48
-
49
- def test_initialize_parses_path_prefix_out_of_given_url_option
50
- conn = Faraday::Connection.new :url => "http://sushi.com/fish"
51
- assert_equal '/fish', conn.path_prefix
52
- end
53
-
54
- def test_initialize_stores_default_params_from_options
55
- conn = Faraday::Connection.new :params => {:a => 1}
56
- assert_equal({'a' => 1}, conn.params)
57
- end
58
-
59
- def test_initialize_stores_default_params_from_uri
60
- conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
61
- assert_equal({'a' => '1'}, conn.params)
62
- end
63
-
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)
67
- end
68
-
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']
72
- end
73
-
74
- def test_basic_auth_sets_header
75
- conn = Faraday::Connection.new
76
- assert_nil conn.headers['Authorization']
77
-
78
- conn.basic_auth 'Aladdin', 'open sesame'
79
- assert auth = conn.headers['Authorization']
80
- assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
81
- end
82
-
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
87
- end
88
-
89
- def test_token_auth_sets_header
90
- conn = Faraday::Connection.new
91
- assert_nil conn.headers['Authorization']
92
-
93
- conn.token_auth 'abcdef', :nonce => 'abc'
94
- assert auth = conn.headers['Authorization']
95
- assert_match(/^Token /, auth)
96
- assert_match(/token="abcdef"/, auth)
97
- assert_match(/nonce="abc"/, auth)
98
- end
99
-
100
- def test_build_exclusive_url_uses_connection_host_as_default_uri_host
101
- conn = Faraday::Connection.new
102
- conn.host = 'sushi.com'
103
- uri = conn.build_exclusive_url("/sake.html")
104
- assert_equal 'sushi.com', uri.host
105
- end
106
-
107
- def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
108
- conn = Faraday::Connection.new
109
- conn.port = 23
110
- uri = conn.build_exclusive_url("http://sushi.com")
111
- assert_equal 80, uri.port
112
- end
113
-
114
- def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
115
- conn = Faraday::Connection.new 'http://sushi.com'
116
- uri = conn.build_exclusive_url("/sake.html")
117
- assert_equal 'http', uri.scheme
118
- end
119
-
120
- def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
121
- conn = Faraday::Connection.new
122
- conn.path_prefix = '/fish'
123
- uri = conn.build_exclusive_url("sake.html")
124
- assert_equal '/fish/sake.html', uri.path
125
- end
126
-
127
- def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
128
- conn = Faraday::Connection.new
129
- conn.path_prefix = '/'
130
- uri = conn.build_exclusive_url("sake.html")
131
- assert_equal '/sake.html', uri.path
132
- end
133
-
134
- def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
135
- conn = Faraday::Connection.new
136
- conn.path_prefix = 'fish'
137
- uri = conn.build_exclusive_url("sake.html")
138
- assert_equal '/fish/sake.html', uri.path
139
- end
140
-
141
- def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
142
- conn = Faraday::Connection.new
143
- conn.path_prefix = '/fish/'
144
- uri = conn.build_exclusive_url("sake.html")
145
- assert_equal '/fish/sake.html', uri.path
146
- end
147
-
148
- def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
149
- conn = Faraday::Connection.new
150
- conn.path_prefix = '/fish'
151
- uri = conn.build_exclusive_url("/sake.html")
152
- assert_equal '/sake.html', uri.path
153
- end
154
-
155
- def test_build_exclusive_url_parses_url_params_into_path
156
- conn = Faraday::Connection.new
157
- uri = conn.build_exclusive_url("http://sushi.com/sake.html")
158
- assert_equal '/sake.html', uri.path
159
- end
160
-
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
169
- conn = Faraday::Connection.new
170
- conn.url_prefix = "http://sushi.com/nigiri"
171
- uri = conn.build_exclusive_url('')
172
- assert_equal "/nigiri", uri.path
173
- end
174
-
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')
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
228
- end
229
-
230
- def test_build_exclusive_url_parses_url
231
- conn = Faraday::Connection.new
232
- uri = conn.build_exclusive_url("http://sushi.com/sake.html")
233
- assert_equal "http", uri.scheme
234
- assert_equal "sushi.com", uri.host
235
- assert_equal '/sake.html', uri.path
236
- end
237
-
238
- def test_build_exclusive_url_parses_url_and_changes_scheme
239
- conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
240
- conn.scheme = 'https'
241
- uri = conn.build_exclusive_url("sake.html")
242
- assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
243
- end
244
-
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
264
- conn = Faraday::Connection.new
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
275
- end
276
-
277
- def test_proxy_accepts_uri
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
283
- end
284
-
285
- def test_proxy_accepts_hash_with_string_uri
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
292
- end
293
-
294
- def test_proxy_accepts_hash
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
345
- end
346
-
347
- def test_proxy_requires_uri
348
- conn = Faraday::Connection.new
349
- assert_raises ArgumentError do
350
- conn.proxy :uri => :bad_uri, :user => 'rick'
351
- end
352
- end
353
-
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
412
- end
413
- end
414
-
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
426
- end
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
515
-
516
- def get(*args)
517
- env = @conn.get(*args) do |req|
518
- yield(req) if block_given?
519
- end
520
- env[:url].query
521
- end
522
- end