faraday 0.9.0.rc5 → 0.9.0.rc6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -5
- data/README.md +0 -22
- data/faraday.gemspec +2 -2
- data/lib/faraday.rb +6 -2
- data/lib/faraday/adapter/em_http.rb +34 -14
- data/lib/faraday/adapter/em_http_ssl_patch.rb +56 -0
- data/lib/faraday/adapter/em_synchrony.rb +21 -20
- data/lib/faraday/adapter/excon.rb +16 -0
- data/lib/faraday/adapter/httpclient.rb +14 -0
- data/lib/faraday/adapter/net_http.rb +6 -2
- data/lib/faraday/adapter/net_http_persistent.rb +15 -3
- data/lib/faraday/adapter/patron.rb +13 -11
- data/lib/faraday/adapter/typhoeus.rb +11 -0
- data/lib/faraday/autoload.rb +2 -4
- data/lib/faraday/connection.rb +30 -3
- data/lib/faraday/error.rb +4 -1
- data/lib/faraday/options.rb +117 -23
- data/lib/faraday/request/instrumentation.rb +1 -3
- data/lib/faraday/request/multipart.rb +1 -1
- data/lib/faraday/request/retry.rb +38 -9
- data/lib/faraday/response.rb +1 -2
- data/lib/faraday/response/raise_error.rb +3 -0
- data/lib/faraday/utils.rb +10 -4
- data/script/proxy-server +42 -0
- data/script/server +1 -3
- data/script/test +35 -7
- data/test/adapters/excon_test.rb +4 -0
- data/test/adapters/httpclient_test.rb +5 -0
- data/test/adapters/integration.rb +48 -2
- data/test/adapters/net_http_persistent_test.rb +10 -1
- data/test/adapters/patron_test.rb +3 -0
- data/test/adapters/rack_test.rb +5 -0
- data/test/adapters/typhoeus_test.rb +3 -13
- data/test/authentication_middleware_test.rb +6 -6
- data/test/connection_test.rb +123 -49
- data/test/env_test.rb +19 -1
- data/test/helper.rb +2 -4
- data/test/live_server.rb +4 -2
- data/test/middleware/instrumentation_test.rb +13 -0
- data/test/middleware/retry_test.rb +47 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +93 -17
- data/test/request_middleware_test.rb +6 -6
- data/test/utils_test.rb +34 -13
- metadata +69 -44
data/test/env_test.rb
CHANGED
@@ -98,6 +98,25 @@ class HeadersTest < Faraday::TestCase
|
|
98
98
|
assert_equal 'application/xml', @headers['content-type']
|
99
99
|
end
|
100
100
|
|
101
|
+
def test_fetch_key
|
102
|
+
@headers['Content-Type'] = 'application/json'
|
103
|
+
block_called = false
|
104
|
+
assert_equal 'application/json', @headers.fetch('content-type') { block_called = true }
|
105
|
+
assert_equal 'application/json', @headers.fetch('Content-Type')
|
106
|
+
assert_equal 'application/json', @headers.fetch('CONTENT-TYPE')
|
107
|
+
assert_equal 'application/json', @headers.fetch(:content_type)
|
108
|
+
assert_equal false, block_called
|
109
|
+
|
110
|
+
assert_equal 'default', @headers.fetch('invalid', 'default')
|
111
|
+
assert_equal false, @headers.fetch('invalid', false)
|
112
|
+
assert_nil @headers.fetch('invalid', nil)
|
113
|
+
|
114
|
+
assert_equal 'Invalid key', @headers.fetch('Invalid') { |key| "#{key} key" }
|
115
|
+
|
116
|
+
expected_error = defined?(KeyError) ? KeyError : IndexError
|
117
|
+
assert_raises(expected_error) { @headers.fetch('invalid') }
|
118
|
+
end
|
119
|
+
|
101
120
|
def test_delete_key
|
102
121
|
@headers['Content-Type'] = 'application/json'
|
103
122
|
assert_equal 1, @headers.size
|
@@ -189,4 +208,3 @@ class ResponseTest < Faraday::TestCase
|
|
189
208
|
assert_equal hash[:body], @response.body
|
190
209
|
end
|
191
210
|
end
|
192
|
-
|
data/test/helper.rb
CHANGED
@@ -11,6 +11,7 @@ SimpleCov.start do
|
|
11
11
|
add_filter '/bundle/'
|
12
12
|
end
|
13
13
|
|
14
|
+
gem 'minitest' if defined? Bundler
|
14
15
|
require 'minitest/autorun'
|
15
16
|
|
16
17
|
if ENV['LEFTRIGHT']
|
@@ -22,9 +23,6 @@ if ENV['LEFTRIGHT']
|
|
22
23
|
end
|
23
24
|
|
24
25
|
require File.expand_path('../../lib/faraday', __FILE__)
|
25
|
-
Dir[File.expand_path('../../lib/faraday/r*/*', __FILE__)].each do |file|
|
26
|
-
require file
|
27
|
-
end
|
28
26
|
|
29
27
|
require 'stringio'
|
30
28
|
require 'uri'
|
@@ -50,7 +48,7 @@ module Faraday
|
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
|
-
class TestCase < MiniTest::
|
51
|
+
class TestCase < MiniTest::Test
|
54
52
|
extend LiveServerConfig
|
55
53
|
self.live_server = ENV['LIVE']
|
56
54
|
|
data/test/live_server.rb
CHANGED
@@ -25,9 +25,11 @@ class LiveServer < Sinatra::Base
|
|
25
25
|
|
26
26
|
post '/file' do
|
27
27
|
if params[:uploaded_file].respond_to? :each_key
|
28
|
-
"file %s %s" % [
|
28
|
+
"file %s %s %d" % [
|
29
29
|
params[:uploaded_file][:filename],
|
30
|
-
params[:uploaded_file][:type]
|
30
|
+
params[:uploaded_file][:type],
|
31
|
+
params[:uploaded_file][:tempfile].size
|
32
|
+
]
|
31
33
|
else
|
32
34
|
status 400
|
33
35
|
end
|
@@ -28,6 +28,19 @@ module Middleware
|
|
28
28
|
assert_equal :boom, options(:instrumenter => :boom).instrumenter
|
29
29
|
end
|
30
30
|
|
31
|
+
def test_instrumentation_with_default_name
|
32
|
+
assert_equal 0, @instrumenter.instrumentations.size
|
33
|
+
|
34
|
+
faraday = conn
|
35
|
+
res = faraday.get '/'
|
36
|
+
assert_equal 'ok', res.body
|
37
|
+
|
38
|
+
assert_equal 1, @instrumenter.instrumentations.size
|
39
|
+
name, env = @instrumenter.instrumentations.first
|
40
|
+
assert_equal 'request.faraday', name
|
41
|
+
assert_equal '/', env[:url].path
|
42
|
+
end
|
43
|
+
|
31
44
|
def test_instrumentation
|
32
45
|
assert_equal 0, @instrumenter.instrumentations.size
|
33
46
|
|
@@ -51,6 +51,53 @@ module Middleware
|
|
51
51
|
assert_in_delta 0.2, Time.now - started, 0.03
|
52
52
|
end
|
53
53
|
|
54
|
+
def test_calls_sleep_amount
|
55
|
+
explode_app = MiniTest::Mock.new
|
56
|
+
explode_app.expect(:call, nil, [{:body=>nil}])
|
57
|
+
def explode_app.call(env)
|
58
|
+
raise Errno::ETIMEDOUT
|
59
|
+
end
|
60
|
+
|
61
|
+
retry_middleware = Faraday::Request::Retry.new(explode_app)
|
62
|
+
class << retry_middleware
|
63
|
+
attr_accessor :sleep_amount_retries
|
64
|
+
|
65
|
+
def sleep_amount(retries)
|
66
|
+
self.sleep_amount_retries.delete(retries)
|
67
|
+
0
|
68
|
+
end
|
69
|
+
end
|
70
|
+
retry_middleware.sleep_amount_retries = [2, 1]
|
71
|
+
|
72
|
+
assert_raises(Errno::ETIMEDOUT) {
|
73
|
+
retry_middleware.call({})
|
74
|
+
}
|
75
|
+
|
76
|
+
assert_empty retry_middleware.sleep_amount_retries
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_exponential_backoff
|
80
|
+
middleware = Faraday::Request::Retry.new(nil, :max => 5, :interval => 0.1, :backoff_factor => 2)
|
81
|
+
assert_equal middleware.sleep_amount(5), 0.1
|
82
|
+
assert_equal middleware.sleep_amount(4), 0.2
|
83
|
+
assert_equal middleware.sleep_amount(3), 0.4
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_random_additional_interval_amount
|
87
|
+
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 1.0)
|
88
|
+
sleep_amount = middleware.sleep_amount(2)
|
89
|
+
assert_operator sleep_amount, :>=, 0.1
|
90
|
+
assert_operator sleep_amount, :<=, 0.2
|
91
|
+
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.5)
|
92
|
+
sleep_amount = middleware.sleep_amount(2)
|
93
|
+
assert_operator sleep_amount, :>=, 0.1
|
94
|
+
assert_operator sleep_amount, :<=, 0.15
|
95
|
+
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.25)
|
96
|
+
sleep_amount = middleware.sleep_amount(2)
|
97
|
+
assert_operator sleep_amount, :>=, 0.1
|
98
|
+
assert_operator sleep_amount, :<=, 0.125
|
99
|
+
end
|
100
|
+
|
54
101
|
def test_custom_exceptions
|
55
102
|
@explode = lambda {|n| raise "boom!" }
|
56
103
|
assert_raises(RuntimeError) {
|
data/test/multibyte.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ファイル
|
data/test/options_test.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path('../helper', __FILE__)
|
|
2
2
|
|
3
3
|
class OptionsTest < Faraday::TestCase
|
4
4
|
class SubOptions < Faraday::Options.new(:sub); end
|
5
|
-
class
|
5
|
+
class ParentOptions < Faraday::Options.new(:a, :b, :c)
|
6
6
|
options :c => SubOptions
|
7
7
|
end
|
8
8
|
|
@@ -33,18 +33,32 @@ class OptionsTest < Faraday::TestCase
|
|
33
33
|
assert_equal 'http', options.scheme
|
34
34
|
end
|
35
35
|
|
36
|
+
def test_proxy_options_hash_access
|
37
|
+
proxy = Faraday::ProxyOptions.from 'http://a%40b:pw%20d@example.org'
|
38
|
+
assert_equal 'a@b', proxy[:user]
|
39
|
+
assert_equal 'a@b', proxy.user
|
40
|
+
assert_equal 'pw d', proxy[:password]
|
41
|
+
assert_equal 'pw d', proxy.password
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_proxy_options_no_auth
|
45
|
+
proxy = Faraday::ProxyOptions.from 'http://example.org'
|
46
|
+
assert_nil proxy.user
|
47
|
+
assert_nil proxy.password
|
48
|
+
end
|
49
|
+
|
36
50
|
def test_from_options
|
37
|
-
options =
|
51
|
+
options = ParentOptions.new 1
|
38
52
|
|
39
|
-
value =
|
53
|
+
value = ParentOptions.from(options)
|
40
54
|
assert_equal 1, value.a
|
41
55
|
assert_nil value.b
|
42
56
|
end
|
43
57
|
|
44
58
|
def test_from_options_with_sub_object
|
45
59
|
sub = SubOptions.new 1
|
46
|
-
options =
|
47
|
-
assert_kind_of
|
60
|
+
options = ParentOptions.from :a => 1, :c => sub
|
61
|
+
assert_kind_of ParentOptions, options
|
48
62
|
assert_equal 1, options.a
|
49
63
|
assert_nil options.b
|
50
64
|
assert_kind_of SubOptions, options.c
|
@@ -52,24 +66,31 @@ class OptionsTest < Faraday::TestCase
|
|
52
66
|
end
|
53
67
|
|
54
68
|
def test_from_hash
|
55
|
-
options =
|
56
|
-
assert_kind_of
|
69
|
+
options = ParentOptions.from :a => 1
|
70
|
+
assert_kind_of ParentOptions, options
|
57
71
|
assert_equal 1, options.a
|
58
72
|
assert_nil options.b
|
59
73
|
end
|
60
74
|
|
61
75
|
def test_from_hash_with_sub_object
|
62
|
-
options =
|
63
|
-
assert_kind_of
|
76
|
+
options = ParentOptions.from :a => 1, :c => {:sub => 1}
|
77
|
+
assert_kind_of ParentOptions, options
|
64
78
|
assert_equal 1, options.a
|
65
79
|
assert_nil options.b
|
66
80
|
assert_kind_of SubOptions, options.c
|
67
81
|
assert_equal 1, options.c.sub
|
68
82
|
end
|
69
83
|
|
84
|
+
def test_inheritance
|
85
|
+
subclass = Class.new(ParentOptions)
|
86
|
+
options = subclass.from(:c => {:sub => 'hello'})
|
87
|
+
assert_kind_of SubOptions, options.c
|
88
|
+
assert_equal 'hello', options.c.sub
|
89
|
+
end
|
90
|
+
|
70
91
|
def test_from_deep_hash
|
71
92
|
hash = {:b => 1}
|
72
|
-
options =
|
93
|
+
options = ParentOptions.from :a => hash
|
73
94
|
assert_equal 1, options.a[:b]
|
74
95
|
|
75
96
|
hash[:b] = 2
|
@@ -81,20 +102,20 @@ class OptionsTest < Faraday::TestCase
|
|
81
102
|
end
|
82
103
|
|
83
104
|
def test_from_nil
|
84
|
-
options =
|
85
|
-
assert_kind_of
|
105
|
+
options = ParentOptions.from(nil)
|
106
|
+
assert_kind_of ParentOptions, options
|
86
107
|
assert_nil options.a
|
87
108
|
assert_nil options.b
|
88
109
|
end
|
89
110
|
|
90
111
|
def test_invalid_key
|
91
112
|
assert_raises NoMethodError do
|
92
|
-
|
113
|
+
ParentOptions.from :invalid => 1
|
93
114
|
end
|
94
115
|
end
|
95
116
|
|
96
117
|
def test_update
|
97
|
-
options =
|
118
|
+
options = ParentOptions.new 1
|
98
119
|
assert_equal 1, options.a
|
99
120
|
assert_nil options.b
|
100
121
|
|
@@ -105,14 +126,14 @@ class OptionsTest < Faraday::TestCase
|
|
105
126
|
end
|
106
127
|
|
107
128
|
def test_delete
|
108
|
-
options =
|
129
|
+
options = ParentOptions.new 1
|
109
130
|
assert_equal 1, options.a
|
110
131
|
assert_equal 1, options.delete(:a)
|
111
132
|
assert_nil options.a
|
112
133
|
end
|
113
134
|
|
114
135
|
def test_merge
|
115
|
-
options =
|
136
|
+
options = ParentOptions.new 1
|
116
137
|
assert_equal 1, options.a
|
117
138
|
assert_nil options.b
|
118
139
|
|
@@ -122,5 +143,60 @@ class OptionsTest < Faraday::TestCase
|
|
122
143
|
assert_equal 1, options.a
|
123
144
|
assert_nil options.b
|
124
145
|
end
|
125
|
-
end
|
126
146
|
|
147
|
+
def test_env_access_member
|
148
|
+
e = Faraday::Env.new
|
149
|
+
assert_nil e.method
|
150
|
+
e.method = :get
|
151
|
+
assert_equal :get, e.method
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_env_access_symbol_non_member
|
155
|
+
e = Faraday::Env.new
|
156
|
+
assert_nil e[:custom]
|
157
|
+
e[:custom] = :boom
|
158
|
+
assert_equal :boom, e[:custom]
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_env_access_string_non_member
|
162
|
+
e = Faraday::Env.new
|
163
|
+
assert_nil e["custom"]
|
164
|
+
e["custom"] = :boom
|
165
|
+
assert_equal :boom, e["custom"]
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_env_fetch_ignores_false
|
169
|
+
ssl = Faraday::SSLOptions.new
|
170
|
+
ssl.verify = false
|
171
|
+
assert !ssl.fetch(:verify, true)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_fetch_grabs_value
|
175
|
+
opt = Faraday::SSLOptions.new
|
176
|
+
opt.verify = 1
|
177
|
+
assert_equal 1, opt.fetch(:verify, false) { |k| :blah }
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_fetch_uses_falsey_default
|
181
|
+
opt = Faraday::SSLOptions.new
|
182
|
+
assert_equal false, opt.fetch(:verify, false) { |k| :blah }
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_fetch_accepts_block
|
186
|
+
opt = Faraday::SSLOptions.new
|
187
|
+
assert_equal "yo :verify", opt.fetch(:verify) { |k| "yo #{k.inspect}"}
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_fetch_needs_a_default_if_key_is_missing
|
191
|
+
opt = Faraday::SSLOptions.new
|
192
|
+
assert_raises Faraday::Options.fetch_error_class do
|
193
|
+
opt.fetch :verify
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_fetch_works_with_key
|
198
|
+
opt = Faraday::SSLOptions.new
|
199
|
+
opt.verify = 1
|
200
|
+
assert_equal 1, opt.fetch(:verify)
|
201
|
+
end
|
202
|
+
end
|
@@ -77,7 +77,7 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
77
77
|
response = @conn.post('/echo', {:str => "eé cç aã aâ"})
|
78
78
|
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
79
79
|
}
|
80
|
-
assert err.empty
|
80
|
+
assert err.empty?, "stderr did include: #{err}"
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_url_encoded_unicode_with_kcode_set
|
@@ -86,7 +86,7 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
86
86
|
response = @conn.post('/echo', {:str => "eé cç aã aâ"})
|
87
87
|
assert_equal "str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2", response.body
|
88
88
|
}
|
89
|
-
assert err.empty
|
89
|
+
assert err.empty?, "stderr did include: #{err}"
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -106,7 +106,7 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
106
106
|
response = @conn.post('/echo', payload)
|
107
107
|
|
108
108
|
assert_kind_of Faraday::CompositeReadIO, response.body
|
109
|
-
assert_equal "multipart/form-data;boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
109
|
+
assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
110
110
|
response.headers['Content-Type']
|
111
111
|
|
112
112
|
response.body.send(:ios).map{|io| io.read}.each do |io|
|
@@ -116,7 +116,7 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
116
116
|
end
|
117
117
|
assert_equal [], regexes
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
def test_multipart_with_arrays
|
121
121
|
# assume params are out of order
|
122
122
|
regexes = [
|
@@ -128,7 +128,7 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
128
128
|
response = @conn.post('/echo', payload)
|
129
129
|
|
130
130
|
assert_kind_of Faraday::CompositeReadIO, response.body
|
131
|
-
assert_equal "multipart/form-data;boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
131
|
+
assert_equal "multipart/form-data; boundary=%s" % Faraday::Request::Multipart::DEFAULT_BOUNDARY,
|
132
132
|
response.headers['Content-Type']
|
133
133
|
|
134
134
|
response.body.send(:ios).map{|io| io.read}.each do |io|
|
@@ -138,5 +138,5 @@ class RequestMiddlewareTest < Faraday::TestCase
|
|
138
138
|
end
|
139
139
|
assert_equal [], regexes
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
end
|
data/test/utils_test.rb
CHANGED
@@ -5,33 +5,54 @@ class TestUtils < Faraday::TestCase
|
|
5
5
|
@url = "http://example.com/abc"
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
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)
|
10
22
|
end
|
11
23
|
|
12
24
|
def test_parses_with_default
|
13
|
-
|
14
|
-
|
15
|
-
|
25
|
+
with_default_uri_parser(nil) do
|
26
|
+
uri = normalize(@url)
|
27
|
+
assert_equal 'example.com', uri.host
|
28
|
+
end
|
16
29
|
end
|
17
30
|
|
18
31
|
def test_parses_with_URI
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
32
|
+
with_default_uri_parser(::URI) do
|
33
|
+
uri = normalize(@url)
|
34
|
+
assert_equal 'example.com', uri.host
|
35
|
+
end
|
23
36
|
end
|
24
37
|
|
25
38
|
def test_parses_with_block
|
26
|
-
|
27
|
-
|
39
|
+
with_default_uri_parser(lambda {|u| "booya#{"!" * u.size}" }) do
|
40
|
+
assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
|
28
41
|
end
|
29
|
-
|
30
|
-
assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
|
31
42
|
end
|
32
43
|
|
33
44
|
def normalize(url)
|
34
45
|
Faraday::Utils::URI(url)
|
35
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
|
36
57
|
end
|
37
58
|
|