faraday 0.16.0 → 0.17.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +232 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +358 -18
  5. data/Rakefile +13 -0
  6. data/lib/faraday/adapter/em_http.rb +97 -140
  7. data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
  8. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
  9. data/lib/faraday/adapter/em_synchrony.rb +60 -104
  10. data/lib/faraday/adapter/excon.rb +55 -100
  11. data/lib/faraday/adapter/httpclient.rb +39 -61
  12. data/lib/faraday/adapter/net_http.rb +51 -103
  13. data/lib/faraday/adapter/net_http_persistent.rb +28 -49
  14. data/lib/faraday/adapter/patron.rb +35 -54
  15. data/lib/faraday/adapter/rack.rb +12 -28
  16. data/lib/faraday/adapter/test.rb +53 -86
  17. data/lib/faraday/adapter/typhoeus.rb +1 -4
  18. data/lib/faraday/adapter.rb +22 -36
  19. data/lib/faraday/autoload.rb +36 -47
  20. data/lib/faraday/connection.rb +179 -321
  21. data/lib/faraday/deprecate.rb +109 -0
  22. data/lib/faraday/error.rb +79 -21
  23. data/lib/faraday/middleware.rb +28 -4
  24. data/lib/faraday/options.rb +183 -32
  25. data/lib/faraday/parameters.rb +197 -4
  26. data/lib/faraday/rack_builder.rb +55 -66
  27. data/lib/faraday/request/authorization.rb +30 -42
  28. data/lib/faraday/request/basic_authentication.rb +7 -14
  29. data/lib/faraday/request/instrumentation.rb +27 -45
  30. data/lib/faraday/request/multipart.rb +48 -79
  31. data/lib/faraday/request/retry.rb +171 -197
  32. data/lib/faraday/request/token_authentication.rb +10 -15
  33. data/lib/faraday/request/url_encoded.rb +23 -41
  34. data/lib/faraday/request.rb +36 -68
  35. data/lib/faraday/response/logger.rb +69 -22
  36. data/lib/faraday/response/raise_error.rb +18 -36
  37. data/lib/faraday/response.rb +13 -20
  38. data/lib/faraday/upload_io.rb +67 -0
  39. data/lib/faraday/utils.rb +245 -28
  40. data/lib/faraday.rb +174 -93
  41. data/spec/faraday/deprecate_spec.rb +147 -0
  42. data/spec/faraday/error_spec.rb +102 -0
  43. data/spec/faraday/response/raise_error_spec.rb +106 -0
  44. data/spec/spec_helper.rb +105 -0
  45. data/test/adapters/default_test.rb +14 -0
  46. data/test/adapters/em_http_test.rb +30 -0
  47. data/test/adapters/em_synchrony_test.rb +32 -0
  48. data/test/adapters/excon_test.rb +30 -0
  49. data/test/adapters/httpclient_test.rb +34 -0
  50. data/test/adapters/integration.rb +263 -0
  51. data/test/adapters/logger_test.rb +136 -0
  52. data/test/adapters/net_http_persistent_test.rb +114 -0
  53. data/test/adapters/net_http_test.rb +79 -0
  54. data/test/adapters/patron_test.rb +40 -0
  55. data/test/adapters/rack_test.rb +38 -0
  56. data/test/adapters/test_middleware_test.rb +157 -0
  57. data/test/adapters/typhoeus_test.rb +38 -0
  58. data/test/authentication_middleware_test.rb +65 -0
  59. data/test/composite_read_io_test.rb +109 -0
  60. data/test/connection_test.rb +738 -0
  61. data/test/env_test.rb +268 -0
  62. data/test/helper.rb +75 -0
  63. data/test/live_server.rb +67 -0
  64. data/test/middleware/instrumentation_test.rb +88 -0
  65. data/test/middleware/retry_test.rb +282 -0
  66. data/test/middleware_stack_test.rb +260 -0
  67. data/test/multibyte.txt +1 -0
  68. data/test/options_test.rb +333 -0
  69. data/test/parameters_test.rb +157 -0
  70. data/test/request_middleware_test.rb +126 -0
  71. data/test/response_middleware_test.rb +72 -0
  72. data/test/strawberry.rb +2 -0
  73. data/test/utils_test.rb +98 -0
  74. metadata +50 -25
  75. data/lib/faraday/adapter_registry.rb +0 -28
  76. data/lib/faraday/dependency_loader.rb +0 -37
  77. data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
  78. data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
  79. data/lib/faraday/file_part.rb +0 -128
  80. data/lib/faraday/logging/formatter.rb +0 -92
  81. data/lib/faraday/middleware_registry.rb +0 -129
  82. data/lib/faraday/options/connection_options.rb +0 -22
  83. data/lib/faraday/options/env.rb +0 -181
  84. data/lib/faraday/options/proxy_options.rb +0 -28
  85. data/lib/faraday/options/request_options.rb +0 -21
  86. data/lib/faraday/options/ssl_options.rb +0 -59
  87. data/lib/faraday/param_part.rb +0 -53
  88. data/lib/faraday/utils/headers.rb +0 -139
  89. data/lib/faraday/utils/params_hash.rb +0 -61
  90. data/spec/external_adapters/faraday_specs_setup.rb +0 -14
@@ -0,0 +1,157 @@
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 do |stub|
8
+ stub.get('/hello') do
9
+ [200, {'Content-Type' => 'text/html'}, 'hello']
10
+ end
11
+ stub.get('/method-echo') do |env|
12
+ [200, {'Content-Type' => 'text/html'}, env[:method].to_s]
13
+ end
14
+ stub.get(/\A\/resources\/\d+(?:\?|\z)/) do
15
+ [200, {'Content-Type' => 'text/html'}, 'show']
16
+ end
17
+ stub.get(/\A\/resources\/(specified)\z/) do |env, meta|
18
+ [200, {'Content-Type' => 'text/html'}, "show #{meta[:match_data][1]}"]
19
+ end
20
+ stub.get('http://domain.test/hello') do
21
+ [200, {'Content-Type' => 'text/html'}, 'domain: hello']
22
+ end
23
+ stub.get('http://wrong.test/hello') do
24
+ [200, {'Content-Type' => 'text/html'}, 'wrong: hello']
25
+ end
26
+ stub.get('http://wrong.test/bait') do
27
+ [404, {'Content-Type' => 'text/html'}]
28
+ end
29
+ end
30
+ @conn = Faraday.new do |builder|
31
+ builder.adapter :test, @stubs
32
+ end
33
+ @resp = @conn.get('/hello')
34
+ end
35
+
36
+ def test_middleware_with_simple_path_sets_status
37
+ assert_equal 200, @resp.status
38
+ end
39
+
40
+ def test_middleware_with_simple_path_sets_headers
41
+ assert_equal 'text/html', @resp.headers['Content-Type']
42
+ end
43
+
44
+ def test_middleware_with_simple_path_sets_body
45
+ assert_equal 'hello', @resp.body
46
+ end
47
+
48
+ def test_middleware_with_host_points_to_the_right_stub
49
+ assert_equal 'domain: hello', @conn.get('http://domain.test/hello').body
50
+ end
51
+
52
+ def test_middleware_can_be_called_several_times
53
+ assert_equal 'hello', @conn.get('/hello').body
54
+ end
55
+
56
+ def test_middleware_can_handle_regular_expression_path
57
+ assert_equal 'show', @conn.get('/resources/1').body
58
+ end
59
+
60
+ def test_middleware_can_handle_single_parameter_block
61
+ assert_equal 'get', @conn.get('/method-echo').body
62
+ end
63
+
64
+ def test_middleware_can_handle_regular_expression_path_with_captured_result
65
+ assert_equal 'show specified', @conn.get('/resources/specified').body
66
+ end
67
+
68
+ def test_middleware_with_get_params
69
+ @stubs.get('/param?a=1') { [200, {}, 'a'] }
70
+ assert_equal 'a', @conn.get('/param?a=1').body
71
+ end
72
+
73
+ def test_middleware_ignores_unspecified_get_params
74
+ @stubs.get('/optional?a=1') { [200, {}, 'a'] }
75
+ assert_equal 'a', @conn.get('/optional?a=1&b=1').body
76
+ assert_equal 'a', @conn.get('/optional?a=1').body
77
+ assert_raises Faraday::Adapter::Test::Stubs::NotFound do
78
+ @conn.get('/optional')
79
+ end
80
+ end
81
+
82
+ def test_middleware_with_http_headers
83
+ @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
84
+ @stubs.get('/yo') { [200, {}, 'b'] }
85
+ assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
86
+ assert_equal 'b', @conn.get('/yo').body
87
+ end
88
+
89
+ def test_middleware_allow_different_outcomes_for_the_same_request
90
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
91
+ @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
92
+ assert_equal 'hello', @conn.get("/hello").body
93
+ assert_equal 'world', @conn.get("/hello").body
94
+ end
95
+
96
+ def test_yields_env_to_stubs
97
+ @stubs.get '/hello' do |env|
98
+ assert_equal '/hello', env[:url].path
99
+ assert_equal 'foo.com', env[:url].host
100
+ assert_equal '1', env[:params]['a']
101
+ assert_equal 'text/plain', env[:request_headers]['Accept']
102
+ [200, {}, 'a']
103
+ end
104
+
105
+ @conn.headers['Accept'] = 'text/plain'
106
+ assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
107
+ end
108
+
109
+ def test_parses_params_with_default_encoder
110
+ @stubs.get '/hello' do |env|
111
+ assert_equal '1', env[:params]['a']['b']
112
+ [200, {}, 'a']
113
+ end
114
+
115
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
116
+ end
117
+
118
+ def test_parses_params_with_nested_encoder
119
+ @stubs.get '/hello' do |env|
120
+ assert_equal '1', env[:params]['a']['b']
121
+ [200, {}, 'a']
122
+ end
123
+
124
+ @conn.options.params_encoder = Faraday::NestedParamsEncoder
125
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
126
+ end
127
+
128
+ def test_parses_params_with_flat_encoder
129
+ @stubs.get '/hello' do |env|
130
+ assert_equal '1', env[:params]['a[b]']
131
+ [200, {}, 'a']
132
+ end
133
+
134
+ @conn.options.params_encoder = Faraday::FlatParamsEncoder
135
+ assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
136
+ end
137
+
138
+ def test_raises_an_error_if_no_stub_is_found_for_request
139
+ assert_raises Stubs::NotFound do
140
+ @conn.get('/invalid'){ [200, {}, []] }
141
+ end
142
+ end
143
+
144
+ def test_raises_an_error_if_no_stub_is_found_for_specified_host
145
+ assert_raises Stubs::NotFound do
146
+ @conn.get('http://domain.test/bait')
147
+ end
148
+ end
149
+
150
+ def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
151
+ @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
152
+ assert_raises Faraday::Adapter::Test::Stubs::NotFound do
153
+ @conn.get('/yo')
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,38 @@
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
+ # inconsistent outcomes ranging from successful response to connection error
10
+ undef :test_proxy_auth_fail if ssl_mode?
11
+
12
+ # Typhoeus adapter not supporting Faraday::SSLError
13
+ undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
14
+
15
+ def test_binds_local_socket
16
+ host = '1.2.3.4'
17
+ conn = create_connection :request => { :bind => { :host => host } }
18
+ assert_equal host, conn.options[:bind][:host]
19
+ end
20
+
21
+ # Typhoeus::Response doesn't provide an easy way to access the reason phrase,
22
+ # so override the shared test from Common.
23
+ def test_GET_reason_phrase
24
+ response = get('echo')
25
+ assert_nil response.reason_phrase
26
+ end
27
+ end
28
+
29
+ def test_custom_adapter_config
30
+ adapter = Faraday::Adapter::Typhoeus.new(nil, { :forbid_reuse => true, :maxredirs => 1 })
31
+
32
+ request = adapter.method(:typhoeus_request).call({})
33
+
34
+ assert_equal true, request.options[:forbid_reuse]
35
+ assert_equal 1, request.options[:maxredirs]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,65 @@
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
@@ -0,0 +1,109 @@
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_nil io.read(3)
44
+ assert_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_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_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_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
+ def test_read_from_multibyte
90
+ File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
91
+ io = composite_io(part("\x86"), Part.new(utf8))
92
+ assert_equal bin("\x86\xE3\x83\x95\xE3\x82\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read
93
+ end
94
+ end
95
+
96
+ def test_limited_from_multibyte
97
+ File.open(File.dirname(__FILE__) + '/multibyte.txt') do |utf8|
98
+ io = composite_io(part("\x86"), Part.new(utf8))
99
+ assert_equal bin("\x86\xE3\x83"), io.read(3)
100
+ assert_equal bin("\x95\xE3\x82"), io.read(3)
101
+ assert_equal bin("\xA1\xE3\x82\xA4\xE3\x83\xAB\n"), io.read(8)
102
+ end
103
+ end
104
+
105
+ def bin(str)
106
+ str.force_encoding("BINARY") if str.respond_to?(:force_encoding)
107
+ str
108
+ end
109
+ end