faraday 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +1 -1
- data/README.md +41 -16
- data/lib/faraday.rb +1 -1
- data/lib/faraday/adapter/em_synchrony.rb +8 -0
- data/lib/faraday/adapter/excon.rb +1 -0
- data/lib/faraday/adapter/httpclient.rb +14 -3
- data/lib/faraday/adapter/net_http.rb +2 -2
- data/lib/faraday/adapter/net_http_persistent.rb +2 -1
- data/lib/faraday/adapter/patron.rb +8 -2
- data/lib/faraday/connection.rb +8 -3
- data/lib/faraday/options.rb +9 -0
- data/lib/faraday/parameters.rb +54 -38
- data/lib/faraday/rack_builder.rb +3 -2
- data/lib/faraday/request/retry.rb +9 -3
- data/lib/faraday/response.rb +2 -2
- data/lib/faraday/utils.rb +13 -1
- metadata +61 -124
- checksums.yaml +0 -7
- data/.document +0 -6
- data/CHANGELOG.md +0 -20
- data/CONTRIBUTING.md +0 -36
- data/Gemfile +0 -25
- data/Rakefile +0 -71
- data/faraday.gemspec +0 -34
- data/script/cached-bundle +0 -46
- data/script/console +0 -7
- data/script/generate_certs +0 -42
- data/script/package +0 -7
- data/script/proxy-server +0 -42
- data/script/release +0 -17
- data/script/s3-put +0 -71
- data/script/server +0 -36
- data/script/test +0 -172
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -20
- data/test/adapters/em_synchrony_test.rb +0 -20
- data/test/adapters/excon_test.rb +0 -20
- data/test/adapters/httpclient_test.rb +0 -21
- data/test/adapters/integration.rb +0 -254
- data/test/adapters/logger_test.rb +0 -82
- data/test/adapters/net_http_persistent_test.rb +0 -20
- data/test/adapters/net_http_test.rb +0 -14
- data/test/adapters/patron_test.rb +0 -20
- data/test/adapters/rack_test.rb +0 -31
- data/test/adapters/test_middleware_test.rb +0 -114
- data/test/adapters/typhoeus_test.rb +0 -28
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -111
- data/test/connection_test.rb +0 -522
- data/test/env_test.rb +0 -218
- data/test/helper.rb +0 -81
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -177
- data/test/middleware_stack_test.rb +0 -173
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -252
- data/test/parameters_test.rb +0 -64
- data/test/request_middleware_test.rb +0 -142
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -58
data/test/parameters_test.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require File.expand_path("../helper", __FILE__)
|
2
|
-
|
3
|
-
class TestParameters < Faraday::TestCase
|
4
|
-
# emulates ActiveSupport::SafeBuffer#gsub
|
5
|
-
FakeSafeBuffer = Struct.new(:string) do
|
6
|
-
def to_s() self end
|
7
|
-
def gsub(regex)
|
8
|
-
string.gsub(regex) {
|
9
|
-
match, = $&, '' =~ /a/
|
10
|
-
yield(match)
|
11
|
-
}
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_escaping_safe_buffer_nested
|
16
|
-
monies = FakeSafeBuffer.new("$32,000.00")
|
17
|
-
assert_equal "a=%2432%2C000.00", Faraday::NestedParamsEncoder.encode("a" => monies)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_escaping_safe_buffer_flat
|
21
|
-
monies = FakeSafeBuffer.new("$32,000.00")
|
22
|
-
assert_equal "a=%2432%2C000.00", Faraday::FlatParamsEncoder.encode("a" => monies)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_raises_typeerror_nested
|
26
|
-
error = assert_raises TypeError do
|
27
|
-
Faraday::NestedParamsEncoder.encode("")
|
28
|
-
end
|
29
|
-
assert_equal "Can't convert String into Hash.", error.message
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_raises_typeerror_flat
|
33
|
-
error = assert_raises TypeError do
|
34
|
-
Faraday::FlatParamsEncoder.encode("")
|
35
|
-
end
|
36
|
-
assert_equal "Can't convert String into Hash.", error.message
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_decode_array_nested
|
40
|
-
query = "a[1]=one&a[2]=two&a[3]=three"
|
41
|
-
expected = {"a" => ["one", "two", "three"]}
|
42
|
-
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_decode_array_flat
|
46
|
-
query = "a=one&a=two&a=three"
|
47
|
-
expected = {"a" => ["one", "two", "three"]}
|
48
|
-
assert_equal expected, Faraday::FlatParamsEncoder.decode(query)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_nested_decode_hash
|
52
|
-
query = "a[b1]=one&a[b2]=two&a[b][c]=foo"
|
53
|
-
expected = {"a" => {"b1" => "one", "b2" => "two", "b" => {"c" => "foo"}}}
|
54
|
-
assert_equal expected, Faraday::NestedParamsEncoder.decode(query)
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_encode_nil_nested
|
58
|
-
assert_equal "a=", Faraday::NestedParamsEncoder.encode("a" => nil)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_encode_nil_flat
|
62
|
-
assert_equal "a", Faraday::FlatParamsEncoder.encode("a" => nil)
|
63
|
-
end
|
64
|
-
end
|
@@ -1,142 +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 setup
|
8
|
-
@conn = 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 with_utf8
|
21
|
-
if defined?(RUBY_VERSION) && RUBY_VERSION.match(/1.8.\d/)
|
22
|
-
begin
|
23
|
-
previous_kcode = $KCODE
|
24
|
-
$KCODE = "UTF8"
|
25
|
-
yield
|
26
|
-
ensure
|
27
|
-
$KCODE = previous_kcode
|
28
|
-
end
|
29
|
-
else
|
30
|
-
yield
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_does_nothing_without_payload
|
35
|
-
response = @conn.post('/echo')
|
36
|
-
assert_nil response.headers['Content-Type']
|
37
|
-
assert response.body.empty?
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_ignores_custom_content_type
|
41
|
-
response = @conn.post('/echo', { :some => 'data' }, 'content-type' => 'application/x-foo')
|
42
|
-
assert_equal 'application/x-foo', response.headers['Content-Type']
|
43
|
-
assert_equal({ :some => 'data' }, response.body)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_url_encoded_no_header
|
47
|
-
response = @conn.post('/echo', { :fruit => %w[apples oranges] })
|
48
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
49
|
-
assert_equal 'fruit%5B%5D=apples&fruit%5B%5D=oranges', response.body
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_url_encoded_with_header
|
53
|
-
response = @conn.post('/echo', {'a'=>123}, 'content-type' => 'application/x-www-form-urlencoded')
|
54
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
55
|
-
assert_equal 'a=123', response.body
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_url_encoded_nested
|
59
|
-
response = @conn.post('/echo', { :user => {:name => 'Mislav', :web => 'mislav.net'} })
|
60
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
61
|
-
expected = { 'user' => {'name' => 'Mislav', 'web' => 'mislav.net'} }
|
62
|
-
assert_equal expected, Faraday::Utils.parse_nested_query(response.body)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_url_encoded_non_nested
|
66
|
-
response = @conn.post('/echo', { :dimensions => ['date', 'location']}) do |req|
|
67
|
-
req.options.params_encoder = Faraday::FlatParamsEncoder
|
68
|
-
end
|
69
|
-
assert_equal 'application/x-www-form-urlencoded', response.headers['Content-Type']
|
70
|
-
expected = { 'dimensions' => ['date', 'location'] }
|
71
|
-
assert_equal expected, Faraday::Utils.parse_query(response.body)
|
72
|
-
assert_equal 'dimensions=date&dimensions=location', response.body
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_url_encoded_unicode
|
76
|
-
err = capture_warnings {
|
77
|
-
response = @conn.post('/echo', {:str => "eé cç aã aâ"})
|
78
|
-
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
79
|
-
}
|
80
|
-
assert err.empty?, "stderr did include: #{err}"
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_url_encoded_unicode_with_kcode_set
|
84
|
-
with_utf8 do
|
85
|
-
err = capture_warnings {
|
86
|
-
response = @conn.post('/echo', {:str => "eé cç aã aâ"})
|
87
|
-
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
88
|
-
}
|
89
|
-
assert err.empty?, "stderr did include: #{err}"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_url_encoded_nested_keys
|
94
|
-
response = @conn.post('/echo', {'a'=>{'b'=>{'c'=>['d']}}})
|
95
|
-
assert_equal "a%5Bb%5D%5Bc%5D%5B%5D=d", response.body
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_multipart
|
99
|
-
# assume params are out of order
|
100
|
-
regexes = [
|
101
|
-
/name\=\"a\"/,
|
102
|
-
/name=\"b\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
103
|
-
/name=\"b\[d\]\"/]
|
104
|
-
|
105
|
-
payload = {:a => 1, :b => {:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}}
|
106
|
-
response = @conn.post('/echo', payload)
|
107
|
-
|
108
|
-
assert_kind_of Faraday::CompositeReadIO, response.body
|
109
|
-
assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
110
|
-
response.headers['Content-Type']
|
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_with_arrays
|
121
|
-
# assume params are out of order
|
122
|
-
regexes = [
|
123
|
-
/name\=\"a\"/,
|
124
|
-
/name=\"b\[\]\[c\]\"\; filename\=\"request_middleware_test\.rb\"/,
|
125
|
-
/name=\"b\[\]\[d\]\"/]
|
126
|
-
|
127
|
-
payload = {:a => 1, :b =>[{:c => Faraday::UploadIO.new(__FILE__, 'text/x-ruby'), :d => 2}]}
|
128
|
-
response = @conn.post('/echo', payload)
|
129
|
-
|
130
|
-
assert_kind_of Faraday::CompositeReadIO, response.body
|
131
|
-
assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
132
|
-
response.headers['Content-Type']
|
133
|
-
|
134
|
-
response.body.send(:ios).map{|io| io.read}.each do |io|
|
135
|
-
if re = regexes.detect { |r| io =~ r }
|
136
|
-
regexes.delete re
|
137
|
-
end
|
138
|
-
end
|
139
|
-
assert_equal [], regexes
|
140
|
-
end
|
141
|
-
|
142
|
-
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::Error::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::Error::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_equal nil, @conn.get('no_content').body
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_304
|
70
|
-
assert_equal nil, @conn.get('not_modified').body
|
71
|
-
end
|
72
|
-
end
|
data/test/strawberry.rb
DELETED
data/test/utils_test.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require File.expand_path('../helper', __FILE__)
|
2
|
-
|
3
|
-
class TestUtils < Faraday::TestCase
|
4
|
-
def setup
|
5
|
-
@url = "http://example.com/abc"
|
6
|
-
end
|
7
|
-
|
8
|
-
# emulates ActiveSupport::SafeBuffer#gsub
|
9
|
-
FakeSafeBuffer = Struct.new(:string) do
|
10
|
-
def to_s() self end
|
11
|
-
def gsub(regex)
|
12
|
-
string.gsub(regex) {
|
13
|
-
match, = $&, '' =~ /a/
|
14
|
-
yield(match)
|
15
|
-
}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_escaping_safe_buffer
|
20
|
-
str = FakeSafeBuffer.new('$32,000.00')
|
21
|
-
assert_equal '%2432%2C000.00', Faraday::Utils.escape(str)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_parses_with_default
|
25
|
-
with_default_uri_parser(nil) do
|
26
|
-
uri = normalize(@url)
|
27
|
-
assert_equal 'example.com', uri.host
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_parses_with_URI
|
32
|
-
with_default_uri_parser(::URI) do
|
33
|
-
uri = normalize(@url)
|
34
|
-
assert_equal 'example.com', uri.host
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_parses_with_block
|
39
|
-
with_default_uri_parser(lambda {|u| "booya#{"!" * u.size}" }) do
|
40
|
-
assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def normalize(url)
|
45
|
-
Faraday::Utils::URI(url)
|
46
|
-
end
|
47
|
-
|
48
|
-
def with_default_uri_parser(parser)
|
49
|
-
old_parser = Faraday::Utils.default_uri_parser
|
50
|
-
begin
|
51
|
-
Faraday::Utils.default_uri_parser = parser
|
52
|
-
yield
|
53
|
-
ensure
|
54
|
-
Faraday::Utils.default_uri_parser = old_parser
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|