faraday 0.17.0 → 0.17.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +216 -0
- data/Rakefile +13 -0
- data/lib/faraday.rb +1 -1
- data/lib/faraday/adapter/em_http.rb +9 -9
- data/lib/faraday/adapter/em_synchrony.rb +5 -5
- data/lib/faraday/adapter/excon.rb +3 -3
- data/lib/faraday/adapter/httpclient.rb +4 -4
- data/lib/faraday/adapter/net_http.rb +2 -2
- data/lib/faraday/adapter/net_http_persistent.rb +3 -3
- data/lib/faraday/adapter/patron.rb +5 -5
- data/lib/faraday/adapter/rack.rb +1 -1
- data/lib/faraday/deprecate.rb +101 -0
- data/lib/faraday/error.rb +85 -32
- data/lib/faraday/options.rb +1 -1
- data/lib/faraday/request/retry.rb +6 -5
- data/lib/faraday/response.rb +3 -3
- data/lib/faraday/response/raise_error.rb +7 -3
- data/spec/faraday/deprecate_spec.rb +69 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +95 -0
- data/spec/spec_helper.rb +104 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +30 -0
- data/test/adapters/em_synchrony_test.rb +32 -0
- data/test/adapters/excon_test.rb +30 -0
- data/test/adapters/httpclient_test.rb +34 -0
- data/test/adapters/integration.rb +263 -0
- data/test/adapters/logger_test.rb +136 -0
- data/test/adapters/net_http_persistent_test.rb +114 -0
- data/test/adapters/net_http_test.rb +79 -0
- data/test/adapters/patron_test.rb +40 -0
- data/test/adapters/rack_test.rb +38 -0
- data/test/adapters/test_middleware_test.rb +157 -0
- data/test/adapters/typhoeus_test.rb +38 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +109 -0
- data/test/connection_test.rb +738 -0
- data/test/env_test.rb +268 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +67 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +282 -0
- data/test/middleware_stack_test.rb +260 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +333 -0
- data/test/parameters_test.rb +157 -0
- data/test/request_middleware_test.rb +126 -0
- data/test/response_middleware_test.rb +72 -0
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +98 -0
- metadata +47 -5
@@ -0,0 +1,126 @@
|
|
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
|
@@ -0,0 +1,72 @@
|
|
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
ADDED
data/test/utils_test.rb
ADDED
@@ -0,0 +1,98 @@
|
|
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
|
+
|
metadata
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.17.
|
4
|
+
version: 0.17.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- "@technoweenie"
|
8
|
+
- "@iMacTia"
|
9
|
+
- "@olleolleolle"
|
8
10
|
autorequire:
|
9
11
|
bindir: bin
|
10
12
|
cert_chain: []
|
11
|
-
date: 2019-
|
13
|
+
date: 2019-11-27 00:00:00.000000000 Z
|
12
14
|
dependencies:
|
13
15
|
- !ruby/object:Gem::Dependency
|
14
16
|
name: multipart-post
|
@@ -36,8 +38,10 @@ executables: []
|
|
36
38
|
extensions: []
|
37
39
|
extra_rdoc_files: []
|
38
40
|
files:
|
41
|
+
- CHANGELOG.md
|
39
42
|
- LICENSE.md
|
40
43
|
- README.md
|
44
|
+
- Rakefile
|
41
45
|
- lib/faraday.rb
|
42
46
|
- lib/faraday/adapter.rb
|
43
47
|
- lib/faraday/adapter/em_http.rb
|
@@ -54,6 +58,7 @@ files:
|
|
54
58
|
- lib/faraday/adapter/typhoeus.rb
|
55
59
|
- lib/faraday/autoload.rb
|
56
60
|
- lib/faraday/connection.rb
|
61
|
+
- lib/faraday/deprecate.rb
|
57
62
|
- lib/faraday/error.rb
|
58
63
|
- lib/faraday/middleware.rb
|
59
64
|
- lib/faraday/options.rb
|
@@ -72,10 +77,47 @@ files:
|
|
72
77
|
- lib/faraday/response/raise_error.rb
|
73
78
|
- lib/faraday/upload_io.rb
|
74
79
|
- lib/faraday/utils.rb
|
75
|
-
|
80
|
+
- spec/faraday/deprecate_spec.rb
|
81
|
+
- spec/faraday/error_spec.rb
|
82
|
+
- spec/faraday/response/raise_error_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- test/adapters/default_test.rb
|
85
|
+
- test/adapters/em_http_test.rb
|
86
|
+
- test/adapters/em_synchrony_test.rb
|
87
|
+
- test/adapters/excon_test.rb
|
88
|
+
- test/adapters/httpclient_test.rb
|
89
|
+
- test/adapters/integration.rb
|
90
|
+
- test/adapters/logger_test.rb
|
91
|
+
- test/adapters/net_http_persistent_test.rb
|
92
|
+
- test/adapters/net_http_test.rb
|
93
|
+
- test/adapters/patron_test.rb
|
94
|
+
- test/adapters/rack_test.rb
|
95
|
+
- test/adapters/test_middleware_test.rb
|
96
|
+
- test/adapters/typhoeus_test.rb
|
97
|
+
- test/authentication_middleware_test.rb
|
98
|
+
- test/composite_read_io_test.rb
|
99
|
+
- test/connection_test.rb
|
100
|
+
- test/env_test.rb
|
101
|
+
- test/helper.rb
|
102
|
+
- test/live_server.rb
|
103
|
+
- test/middleware/instrumentation_test.rb
|
104
|
+
- test/middleware/retry_test.rb
|
105
|
+
- test/middleware_stack_test.rb
|
106
|
+
- test/multibyte.txt
|
107
|
+
- test/options_test.rb
|
108
|
+
- test/parameters_test.rb
|
109
|
+
- test/request_middleware_test.rb
|
110
|
+
- test/response_middleware_test.rb
|
111
|
+
- test/strawberry.rb
|
112
|
+
- test/utils_test.rb
|
113
|
+
homepage: https://lostisland.github.io/faraday
|
76
114
|
licenses:
|
77
115
|
- MIT
|
78
|
-
metadata:
|
116
|
+
metadata:
|
117
|
+
homepage_uri: https://lostisland.github.io/faraday
|
118
|
+
changelog_uri: https://github.com/lostisland/faraday/blob/master/CHANGELOG.md
|
119
|
+
source_code_uri: https://github.com/lostisland/faraday/
|
120
|
+
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
79
121
|
post_install_message:
|
80
122
|
rdoc_options: []
|
81
123
|
require_paths:
|