faraday 0.17.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +69 -9
- data/LICENSE.md +1 -1
- data/README.md +17 -347
- data/Rakefile +1 -7
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday.rb +94 -176
- data/lib/faraday/adapter.rb +83 -22
- data/lib/faraday/adapter/em_http.rb +143 -100
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +98 -56
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http.rb +130 -63
- data/lib/faraday/adapter/net_http_persistent.rb +51 -28
- data/lib/faraday/adapter/patron.rb +80 -43
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +312 -182
- data/lib/faraday/dependency_loader.rb +39 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +46 -25
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +12 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options.rb +38 -193
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +77 -65
- data/lib/faraday/request.rb +86 -44
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +86 -48
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/response.rb +24 -14
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -18
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +0 -57
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +60 -16
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -35
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +80 -37
- data/lib/faraday/deprecate.rb +0 -101
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -1,126 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require File.expand_path('../helper', __FILE__)
|
3
|
-
|
4
|
-
Faraday::CompositeReadIO.class_eval { attr_reader :ios }
|
5
|
-
|
6
|
-
class RequestMiddlewareTest < Faraday::TestCase
|
7
|
-
def conn
|
8
|
-
Faraday.new do |b|
|
9
|
-
b.request :multipart
|
10
|
-
b.request :url_encoded
|
11
|
-
b.adapter :test do |stub|
|
12
|
-
stub.post('/echo') do |env|
|
13
|
-
posted_as = env[:request_headers]['Content-Type']
|
14
|
-
[200, {'Content-Type' => posted_as}, env[:body]]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_does_nothing_without_payload
|
21
|
-
response = conn.post('/echo')
|
22
|
-
assert_nil response.headers['Content-Type']
|
23
|
-
assert response.body.empty?
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_ignores_custom_content_type
|
27
|
-
response = conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
|
28
|
-
assert_equal 'application/x-foo', response.headers['Content-Type']
|
29
|
-
assert_equal({ :some => 'data' }, response.body)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_url_encoded_no_header
|
33
|
-
response = conn.post('/echo', { :fruit => %w[apples oranges] })
|
34
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
35
|
-
assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_url_encoded_with_header
|
39
|
-
response = conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
|
40
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
41
|
-
assert_equal 'a=123', response.body
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_url_encoded_nested
|
45
|
-
response = conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
|
46
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
47
|
-
expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
|
48
|
-
assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_url_encoded_non_nested
|
52
|
-
response = conn.post('/echo', { :dimensions => ['date', 'location']}) do |req|
|
53
|
-
req.options.params_encoder = Faraday::FlatParamsEncoder
|
54
|
-
end
|
55
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
56
|
-
expected = { 'dimensions' => ['date', 'location'] }
|
57
|
-
assert_equal expected, Faraday::Utils.parse_query(response.body)
|
58
|
-
assert_equal 'dimensions=date&dimensions=location', response.body
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_url_encoded_unicode
|
62
|
-
err = capture_warnings {
|
63
|
-
response = conn.post('/echo', {:str => "eé cç aã aâ"})
|
64
|
-
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
65
|
-
}
|
66
|
-
assert err.empty?, "stderr did include: #{err}"
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_url_encoded_nested_keys
|
70
|
-
response = conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
|
71
|
-
assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_multipart
|
75
|
-
# assume params are out of order
|
76
|
-
regexes = [
|
77
|
-
/name\=\"a\"/,
|
78
|
-
/name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
79
|
-
/name=\"b\[d\]\"/]
|
80
|
-
|
81
|
-
payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
|
82
|
-
response = conn.post('/echo', payload)
|
83
|
-
|
84
|
-
assert_kind_of Faraday::CompositeReadIO, response.body
|
85
|
-
assert response.headers['Content-Type'].start_with?(
|
86
|
-
"multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX,
|
87
|
-
)
|
88
|
-
|
89
|
-
response.body.send(:ios).map{|io| io.read}.each do |io|
|
90
|
-
if re = regexes.detect { |r| io =~ r }
|
91
|
-
regexes.delete re
|
92
|
-
end
|
93
|
-
end
|
94
|
-
assert_equal [], regexes
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_multipart_with_arrays
|
98
|
-
# assume params are out of order
|
99
|
-
regexes = [
|
100
|
-
/name\=\"a\"/,
|
101
|
-
/name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
102
|
-
/name=\"b\[\]\[d\]\"/]
|
103
|
-
|
104
|
-
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
105
|
-
response = conn.post('/echo', payload)
|
106
|
-
|
107
|
-
assert_kind_of Faraday::CompositeReadIO, response.body
|
108
|
-
assert response.headers['Content-Type'].start_with?(
|
109
|
-
"multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX,
|
110
|
-
)
|
111
|
-
|
112
|
-
response.body.send(:ios).map{|io| io.read}.each do |io|
|
113
|
-
if re = regexes.detect { |r| io =~ r }
|
114
|
-
regexes.delete re
|
115
|
-
end
|
116
|
-
end
|
117
|
-
assert_equal [], regexes
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_multipart_unique_boundary
|
121
|
-
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
122
|
-
response1 = conn.post('/echo', payload)
|
123
|
-
response2 = conn.post('/echo', payload)
|
124
|
-
assert response1.headers['Content-Type'] != response2.headers['Content-Type']
|
125
|
-
end
|
126
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
require File.expand_path('../helper', __FILE__)
|
2
|
-
|
3
|
-
class ResponseMiddlewareTest < Faraday::TestCase
|
4
|
-
def setup
|
5
|
-
@conn = Faraday.new do |b|
|
6
|
-
b.response :raise_error
|
7
|
-
b.adapter :test do |stub|
|
8
|
-
stub.get('ok') { [200, {'Content-Type' => 'text/html'}, '<body></body>'] }
|
9
|
-
stub.get('not-found') { [404, {'X-Reason' => 'because'}, 'keep looking'] }
|
10
|
-
stub.get('error') { [500, {'X-Error' => 'bailout'}, 'fail'] }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class ResponseUpcaser < Faraday::Response::Middleware
|
16
|
-
def parse(body)
|
17
|
-
body.upcase
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_success
|
22
|
-
assert @conn.get('ok')
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_raises_not_found
|
26
|
-
error = assert_raises Faraday::ResourceNotFound do
|
27
|
-
@conn.get('not-found')
|
28
|
-
end
|
29
|
-
assert_equal 'the server responded with status 404', error.message
|
30
|
-
assert_equal 'because', error.response[:headers]['X-Reason']
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_raises_error
|
34
|
-
error = assert_raises Faraday::ClientError do
|
35
|
-
@conn.get('error')
|
36
|
-
end
|
37
|
-
assert_equal 'the server responded with status 500', error.message
|
38
|
-
assert_equal 'bailout', error.response[:headers]['X-Error']
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_upcase
|
42
|
-
@conn.builder.insert(0, ResponseUpcaser)
|
43
|
-
assert_equal '<BODY></BODY>', @conn.get('ok').body
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
class ResponseNoBodyMiddleWareTest < Faraday::TestCase
|
48
|
-
def setup
|
49
|
-
@conn = Faraday.new do |b|
|
50
|
-
b.response :raise_error
|
51
|
-
b.adapter :test do |stub|
|
52
|
-
stub.get('not_modified') { [304, nil, nil] }
|
53
|
-
stub.get('no_content') { [204, nil, nil] }
|
54
|
-
end
|
55
|
-
end
|
56
|
-
@conn.builder.insert(0, NotCalled)
|
57
|
-
end
|
58
|
-
|
59
|
-
class NotCalled < Faraday::Response::Middleware
|
60
|
-
def parse(body)
|
61
|
-
raise "this should not be called"
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_204
|
66
|
-
assert_nil @conn.get('no_content').body
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_304
|
70
|
-
assert_nil @conn.get('not_modified').body
|
71
|
-
end
|
72
|
-
end
|
data/test/strawberry.rb
DELETED
data/test/utils_test.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require File.expand_path('../helper', __FILE__)
|
2
|
-
|
3
|
-
class TestUtils < Faraday::TestCase
|
4
|
-
|
5
|
-
# Headers parsing
|
6
|
-
|
7
|
-
def test_headers
|
8
|
-
"HTTP/1.x 500 OK\r\nContent-Type: text/html; charset=UTF-8\r\n" \
|
9
|
-
"HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_headers_parsing_for_aggregated_responses
|
13
|
-
headers = Faraday::Utils::Headers.new()
|
14
|
-
headers.parse(test_headers)
|
15
|
-
|
16
|
-
result = headers.to_hash
|
17
|
-
|
18
|
-
assert_equal result["Content-Type"], "application/json; charset=UTF-8"
|
19
|
-
end
|
20
|
-
|
21
|
-
# URI parsing
|
22
|
-
|
23
|
-
def setup
|
24
|
-
@url = "http://example.com/abc"
|
25
|
-
end
|
26
|
-
|
27
|
-
# emulates ActiveSupport::SafeBuffer#gsub
|
28
|
-
FakeSafeBuffer = Struct.new(:string) do
|
29
|
-
def to_s() self end
|
30
|
-
def gsub(regex)
|
31
|
-
string.gsub(regex) {
|
32
|
-
match, = $&, '' =~ /a/
|
33
|
-
yield(match)
|
34
|
-
}
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_escaping_safe_buffer
|
39
|
-
str = FakeSafeBuffer.new('$32,000.00')
|
40
|
-
assert_equal '%2432%2C000.00', Faraday::Utils.escape(str)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_parses_with_default
|
44
|
-
with_default_uri_parser(nil) do
|
45
|
-
uri = normalize(@url)
|
46
|
-
assert_equal 'example.com', uri.host
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_parses_with_URI
|
51
|
-
with_default_uri_parser(::URI) do
|
52
|
-
uri = normalize(@url)
|
53
|
-
assert_equal 'example.com', uri.host
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_parses_with_block
|
58
|
-
with_default_uri_parser(lambda {|u| "booya#{"!" * u.size}" }) do
|
59
|
-
assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_replace_header_hash
|
64
|
-
headers = Faraday::Utils::Headers.new('authorization' => 't0ps3cr3t!')
|
65
|
-
assert headers.include?('authorization')
|
66
|
-
|
67
|
-
headers.replace({'content-type' => 'text/plain'})
|
68
|
-
|
69
|
-
assert !headers.include?('authorization')
|
70
|
-
end
|
71
|
-
|
72
|
-
def normalize(url)
|
73
|
-
Faraday::Utils::URI(url)
|
74
|
-
end
|
75
|
-
|
76
|
-
def with_default_uri_parser(parser)
|
77
|
-
old_parser = Faraday::Utils.default_uri_parser
|
78
|
-
begin
|
79
|
-
Faraday::Utils.default_uri_parser = parser
|
80
|
-
yield
|
81
|
-
ensure
|
82
|
-
Faraday::Utils.default_uri_parser = old_parser
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
# YAML parsing
|
87
|
-
|
88
|
-
def test_headers_yaml_roundtrip
|
89
|
-
headers = Faraday::Utils::Headers.new('User-Agent' => 'safari', 'Content-type' => 'text/html')
|
90
|
-
result = YAML.load(headers.to_yaml)
|
91
|
-
|
92
|
-
assert result.include?('user-agent'), 'Unable to hydrate to a correct Headers'
|
93
|
-
assert result.include?('content-type'), 'Unable to hydrate to a correct Headers'
|
94
|
-
assert result['user-agent'] == 'safari', 'Unable to access rehydrated Headers'
|
95
|
-
assert result['content-type'] == 'text/html', 'Unable to access rehydrated Headers'
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|