faraday 0.17.4 → 1.8.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 +156 -8
- data/LICENSE.md +1 -1
- data/README.md +16 -358
- data/Rakefile +1 -7
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +118 -69
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +72 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +39 -36
- data/lib/faraday/connection.rb +343 -185
- data/lib/faraday/dependency_loader.rb +37 -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 +29 -35
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +36 -191
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +76 -64
- data/lib/faraday/request/authorization.rb +51 -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/request.rb +86 -44
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -18
- data/lib/faraday/response.rb +24 -20
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +127 -189
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +49 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +18 -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_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 +377 -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 +736 -0
- data/spec/faraday/error_spec.rb +12 -54
- data/spec/faraday/middleware_spec.rb +52 -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 +44 -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 +96 -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 +78 -15
- 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 -36
- 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 +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +194 -48
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -82
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -153
- data/lib/faraday/adapter/net_http_persistent.rb +0 -68
- data/lib/faraday/adapter/patron.rb +0 -95
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/deprecate.rb +0 -109
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -147
- 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,282 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helper", __FILE__)
|
2
|
-
|
3
|
-
module Middleware
|
4
|
-
class RetryTest < Faraday::TestCase
|
5
|
-
def setup
|
6
|
-
@times_called = 0
|
7
|
-
@envs = []
|
8
|
-
end
|
9
|
-
|
10
|
-
def conn(*retry_args)
|
11
|
-
Faraday.new do |b|
|
12
|
-
b.request :retry, *retry_args
|
13
|
-
|
14
|
-
b.adapter :test do |stub|
|
15
|
-
['get', 'post'].each do |method|
|
16
|
-
stub.send(method, '/unstable') do |env|
|
17
|
-
@times_called += 1
|
18
|
-
@envs << env.dup
|
19
|
-
env[:body] = nil # simulate blanking out response body
|
20
|
-
@explode.call @times_called
|
21
|
-
end
|
22
|
-
|
23
|
-
stub.send(method, '/throttled') do |env|
|
24
|
-
@times_called += 1
|
25
|
-
@envs << env.dup
|
26
|
-
|
27
|
-
params = env[:params]
|
28
|
-
|
29
|
-
status = (params['status'] || 429).to_i
|
30
|
-
headers = {}
|
31
|
-
|
32
|
-
retry_after = params['retry_after']
|
33
|
-
|
34
|
-
headers['Retry-After'] = retry_after if retry_after
|
35
|
-
|
36
|
-
[status, headers, '']
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_unhandled_error
|
44
|
-
@explode = lambda {|n| raise "boom!" }
|
45
|
-
assert_raises(RuntimeError) { conn.get("/unstable") }
|
46
|
-
assert_equal 1, @times_called
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_handled_error
|
50
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
51
|
-
assert_raises(Errno::ETIMEDOUT) { conn.get("/unstable") }
|
52
|
-
assert_equal 3, @times_called
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_legacy_max_retries
|
56
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
57
|
-
assert_raises(Errno::ETIMEDOUT) { conn(1).get("/unstable") }
|
58
|
-
assert_equal 2, @times_called
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_legacy_max_negative_retries
|
62
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
63
|
-
assert_raises(Errno::ETIMEDOUT) { conn(-9).get("/unstable") }
|
64
|
-
assert_equal 1, @times_called
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_new_max_retries
|
68
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
69
|
-
assert_raises(Errno::ETIMEDOUT) { conn(:max => 3).get("/unstable") }
|
70
|
-
assert_equal 4, @times_called
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_new_max_negative_retries
|
74
|
-
@explode = lambda { |n| raise Errno::ETIMEDOUT }
|
75
|
-
assert_raises(Errno::ETIMEDOUT) { conn(:max => -9).get("/unstable") }
|
76
|
-
assert_equal 1, @times_called
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_interval
|
80
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
81
|
-
started = Time.now
|
82
|
-
assert_raises(Errno::ETIMEDOUT) {
|
83
|
-
conn(:max => 2, :interval => 0.1).get("/unstable")
|
84
|
-
}
|
85
|
-
assert_in_delta 0.2, Time.now - started, 0.04
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_calls_calculate_sleep_amount
|
89
|
-
explode_app = MiniTest::Mock.new
|
90
|
-
explode_app.expect(:call, nil, [{:body=>nil}])
|
91
|
-
def explode_app.call(env)
|
92
|
-
raise Errno::ETIMEDOUT
|
93
|
-
end
|
94
|
-
|
95
|
-
retry_middleware = Faraday::Request::Retry.new(explode_app)
|
96
|
-
class << retry_middleware
|
97
|
-
attr_accessor :sleep_amount_retries
|
98
|
-
|
99
|
-
def calculate_sleep_amount(retries, env)
|
100
|
-
self.sleep_amount_retries.delete(retries)
|
101
|
-
0
|
102
|
-
end
|
103
|
-
end
|
104
|
-
retry_middleware.sleep_amount_retries = [2, 1]
|
105
|
-
|
106
|
-
assert_raises(Errno::ETIMEDOUT) {
|
107
|
-
retry_middleware.call({:method => :get})
|
108
|
-
}
|
109
|
-
|
110
|
-
assert_empty retry_middleware.sleep_amount_retries
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_exponential_backoff
|
114
|
-
middleware = Faraday::Request::Retry.new(nil, :max => 5, :interval => 0.1, :backoff_factor => 2)
|
115
|
-
assert_equal middleware.send(:calculate_retry_interval, 5), 0.1
|
116
|
-
assert_equal middleware.send(:calculate_retry_interval, 4), 0.2
|
117
|
-
assert_equal middleware.send(:calculate_retry_interval, 3), 0.4
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_exponential_backoff_with_max_interval
|
121
|
-
middleware = Faraday::Request::Retry.new(nil, :max => 5, :interval => 1, :max_interval => 3, :backoff_factor => 2)
|
122
|
-
assert_equal middleware.send(:calculate_retry_interval, 5), 1
|
123
|
-
assert_equal middleware.send(:calculate_retry_interval, 4), 2
|
124
|
-
assert_equal middleware.send(:calculate_retry_interval, 3), 3
|
125
|
-
assert_equal middleware.send(:calculate_retry_interval, 2), 3
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_random_additional_interval_amount
|
129
|
-
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 1.0)
|
130
|
-
sleep_amount = middleware.send(:calculate_retry_interval, 2)
|
131
|
-
assert_operator sleep_amount, :>=, 0.1
|
132
|
-
assert_operator sleep_amount, :<=, 0.2
|
133
|
-
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.5)
|
134
|
-
sleep_amount = middleware.send(:calculate_retry_interval, 2)
|
135
|
-
assert_operator sleep_amount, :>=, 0.1
|
136
|
-
assert_operator sleep_amount, :<=, 0.15
|
137
|
-
middleware = Faraday::Request::Retry.new(nil, :max => 2, :interval => 0.1, :interval_randomness => 0.25)
|
138
|
-
sleep_amount = middleware.send(:calculate_retry_interval, 2)
|
139
|
-
assert_operator sleep_amount, :>=, 0.1
|
140
|
-
assert_operator sleep_amount, :<=, 0.125
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_custom_exceptions
|
144
|
-
@explode = lambda {|n| raise "boom!" }
|
145
|
-
assert_raises(RuntimeError) {
|
146
|
-
conn(:exceptions => StandardError).get("/unstable")
|
147
|
-
}
|
148
|
-
assert_equal 3, @times_called
|
149
|
-
end
|
150
|
-
|
151
|
-
def test_should_retry_with_body_if_block_returns_true_for_non_idempotent_request
|
152
|
-
body = { :foo => :bar }
|
153
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
154
|
-
check = lambda { |env,exception| true }
|
155
|
-
assert_raises(Errno::ETIMEDOUT) {
|
156
|
-
conn(:retry_if => check).post("/unstable", body)
|
157
|
-
}
|
158
|
-
assert_equal 3, @times_called
|
159
|
-
assert @envs.all? { |env| env[:body] === body }
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_should_stop_retrying_if_block_returns_false_checking_env
|
163
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
164
|
-
check = lambda { |env,exception| env[:method] != :post }
|
165
|
-
assert_raises(Errno::ETIMEDOUT) {
|
166
|
-
conn(:retry_if => check).post("/unstable")
|
167
|
-
}
|
168
|
-
assert_equal 1, @times_called
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_should_stop_retrying_if_block_returns_false_checking_exception
|
172
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
173
|
-
check = lambda { |env,exception| !exception.kind_of?(Errno::ETIMEDOUT) }
|
174
|
-
assert_raises(Errno::ETIMEDOUT) {
|
175
|
-
conn(:retry_if => check).post("/unstable")
|
176
|
-
}
|
177
|
-
assert_equal 1, @times_called
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_should_not_call_retry_if_for_idempotent_methods_if_methods_unspecified
|
181
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
182
|
-
check = lambda { |env,exception| raise "this should have never been called" }
|
183
|
-
assert_raises(Errno::ETIMEDOUT) {
|
184
|
-
conn(:retry_if => check).get("/unstable")
|
185
|
-
}
|
186
|
-
assert_equal 3, @times_called
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_should_not_retry_for_non_idempotent_method_if_methods_unspecified
|
190
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
191
|
-
assert_raises(Errno::ETIMEDOUT) {
|
192
|
-
conn.post("/unstable")
|
193
|
-
}
|
194
|
-
assert_equal 1, @times_called
|
195
|
-
end
|
196
|
-
|
197
|
-
def test_should_not_call_retry_if_for_specified_methods
|
198
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
199
|
-
check = lambda { |env,exception| raise "this should have never been called" }
|
200
|
-
assert_raises(Errno::ETIMEDOUT) {
|
201
|
-
conn(:retry_if => check, :methods => [:post]).post("/unstable")
|
202
|
-
}
|
203
|
-
assert_equal 3, @times_called
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_should_call_retry_if_for_empty_method_list
|
207
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
208
|
-
check = lambda { |env,exception| @times_called < 2 }
|
209
|
-
assert_raises(Errno::ETIMEDOUT) {
|
210
|
-
conn(:retry_if => check, :methods => []).get("/unstable")
|
211
|
-
}
|
212
|
-
assert_equal 2, @times_called
|
213
|
-
end
|
214
|
-
|
215
|
-
def test_should_rewind_files_on_retry
|
216
|
-
io = StringIO.new("Test data")
|
217
|
-
upload_io = Faraday::UploadIO.new(io, "application/octet/stream")
|
218
|
-
|
219
|
-
rewound = 0
|
220
|
-
rewind = lambda { rewound += 1 }
|
221
|
-
|
222
|
-
upload_io.stub :rewind, rewind do
|
223
|
-
@explode = lambda {|n| raise Errno::ETIMEDOUT }
|
224
|
-
check = lambda { |env,exception| true }
|
225
|
-
assert_raises(Errno::ETIMEDOUT) {
|
226
|
-
conn(:retry_if => check).post("/unstable", { :file => upload_io })
|
227
|
-
}
|
228
|
-
end
|
229
|
-
assert_equal 3, @times_called
|
230
|
-
assert_equal 2, rewound
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_should_retry_retriable_response
|
234
|
-
params = { status: 429 }
|
235
|
-
response = conn(:max => 1, :retry_statuses => 429).get("/throttled", params)
|
236
|
-
|
237
|
-
assert_equal 2, @times_called
|
238
|
-
assert_equal 429, response.status
|
239
|
-
end
|
240
|
-
|
241
|
-
def test_should_not_retry_non_retriable_response
|
242
|
-
params = { status: 503 }
|
243
|
-
conn(:max => 1, :retry_statuses => 429).get("/throttled", params)
|
244
|
-
|
245
|
-
assert_equal 1, @times_called
|
246
|
-
end
|
247
|
-
|
248
|
-
def test_interval_if_retry_after_present
|
249
|
-
started = Time.now
|
250
|
-
|
251
|
-
params = { :retry_after => 0.5 }
|
252
|
-
conn(:max => 1, :interval => 0.1, :retry_statuses => [429]).get("/throttled", params)
|
253
|
-
|
254
|
-
assert Time.now - started > 0.5
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_should_ignore_retry_after_if_less_then_calculated
|
258
|
-
started = Time.now
|
259
|
-
|
260
|
-
params = { :retry_after => 0.1 }
|
261
|
-
conn(:max => 1, :interval => 0.2, :retry_statuses => [429]).get("/throttled", params)
|
262
|
-
|
263
|
-
assert Time.now - started > 0.2
|
264
|
-
end
|
265
|
-
|
266
|
-
def test_interval_when_retry_after_is_timestamp
|
267
|
-
started = Time.now
|
268
|
-
|
269
|
-
params = { :retry_after => (Time.now.utc + 2).strftime('%a, %d %b %Y %H:%M:%S GMT') }
|
270
|
-
conn(:max => 1, :interval => 0.1, :retry_statuses => [429]).get("/throttled", params)
|
271
|
-
|
272
|
-
assert Time.now - started > 1
|
273
|
-
end
|
274
|
-
|
275
|
-
def test_should_not_retry_when_retry_after_greater_then_max_interval
|
276
|
-
params = { :retry_after => (Time.now.utc + 20).strftime('%a, %d %b %Y %H:%M:%S GMT') }
|
277
|
-
conn(:max => 2, :interval => 0.1, :retry_statuses => [429], max_interval: 5).get("/throttled", params)
|
278
|
-
|
279
|
-
assert_equal 1, @times_called
|
280
|
-
end
|
281
|
-
end
|
282
|
-
end
|
@@ -1,260 +0,0 @@
|
|
1
|
-
require File.expand_path('../helper', __FILE__)
|
2
|
-
|
3
|
-
class MiddlewareStackTest < Faraday::TestCase
|
4
|
-
# mock handler classes
|
5
|
-
class Handler < Struct.new(:app)
|
6
|
-
def call(env)
|
7
|
-
(env[:request_headers]['X-Middleware'] ||= '') << ":#{self.class.name.split('::').last}"
|
8
|
-
app.call(env)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
class Apple < Handler; end
|
12
|
-
class Orange < Handler; end
|
13
|
-
class Banana < Handler; end
|
14
|
-
|
15
|
-
class Broken < Faraday::Middleware
|
16
|
-
dependency 'zomg/i_dont/exist'
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup
|
20
|
-
@conn = Faraday::Connection.new
|
21
|
-
@builder = @conn.builder
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_sets_default_adapter_if_none_set
|
25
|
-
default_middleware = Faraday::Request.lookup_middleware :url_encoded
|
26
|
-
default_adapter_klass = Faraday::Adapter.lookup_middleware Faraday.default_adapter
|
27
|
-
assert @builder[0] == default_middleware
|
28
|
-
assert @builder[1] == default_adapter_klass
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_allows_rebuilding
|
32
|
-
build_stack Apple
|
33
|
-
build_stack Orange
|
34
|
-
assert_handlers %w[Orange]
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_allows_extending
|
38
|
-
build_handlers_stack Apple
|
39
|
-
@builder.use Orange
|
40
|
-
@builder.adapter :test, &test_adapter
|
41
|
-
assert_handlers %w[Apple Orange]
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_builder_is_passed_to_new_faraday_connection
|
45
|
-
new_conn = Faraday::Connection.new :builder => @builder
|
46
|
-
assert_equal @builder, new_conn.builder
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_insert_before
|
50
|
-
build_handlers_stack Apple, Orange
|
51
|
-
@builder.insert_before Apple, Banana
|
52
|
-
@builder.adapter :test, &test_adapter
|
53
|
-
assert_handlers %w[Banana Apple Orange]
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_insert_after
|
57
|
-
build_handlers_stack Apple, Orange
|
58
|
-
@builder.insert_after Apple, Banana
|
59
|
-
@builder.adapter :test, &test_adapter
|
60
|
-
assert_handlers %w[Apple Banana Orange]
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_swap_handlers
|
64
|
-
build_handlers_stack Apple, Orange
|
65
|
-
@builder.swap Apple, Banana
|
66
|
-
@builder.adapter :test, &test_adapter
|
67
|
-
assert_handlers %w[Banana Orange]
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_delete_handler
|
71
|
-
build_stack Apple, Orange
|
72
|
-
@builder.delete Apple
|
73
|
-
assert_handlers %w[Orange]
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_stack_is_locked_after_making_requests
|
77
|
-
build_stack Apple
|
78
|
-
assert !@builder.locked?
|
79
|
-
@conn.get('/')
|
80
|
-
assert @builder.locked?
|
81
|
-
|
82
|
-
assert_raises Faraday::RackBuilder::StackLocked do
|
83
|
-
@conn.use Orange
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_duped_stack_is_unlocked
|
88
|
-
build_stack Apple
|
89
|
-
assert !@builder.locked?
|
90
|
-
@builder.lock!
|
91
|
-
assert @builder.locked?
|
92
|
-
|
93
|
-
duped_connection = @conn.dup
|
94
|
-
assert_equal @builder, duped_connection.builder
|
95
|
-
assert !duped_connection.builder.locked?
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_handler_comparison
|
99
|
-
build_stack Apple
|
100
|
-
assert_equal @builder.handlers.first, Apple
|
101
|
-
assert_equal @builder.handlers[0,1], [Apple]
|
102
|
-
assert_equal @builder.handlers.first, Faraday::RackBuilder::Handler.new(Apple)
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_unregistered_symbol
|
106
|
-
err = assert_raises(Faraday::Error){ build_stack :apple }
|
107
|
-
assert_equal ":apple is not registered on Faraday::Middleware", err.message
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_registered_symbol
|
111
|
-
Faraday::Middleware.register_middleware :apple => Apple
|
112
|
-
begin
|
113
|
-
build_stack :apple
|
114
|
-
assert_handlers %w[Apple]
|
115
|
-
ensure
|
116
|
-
unregister_middleware Faraday::Middleware, :apple
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_registered_symbol_with_proc
|
121
|
-
Faraday::Middleware.register_middleware :apple => lambda { Apple }
|
122
|
-
begin
|
123
|
-
build_stack :apple
|
124
|
-
assert_handlers %w[Apple]
|
125
|
-
ensure
|
126
|
-
unregister_middleware Faraday::Middleware, :apple
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_registered_symbol_with_array
|
131
|
-
Faraday::Middleware.register_middleware File.expand_path("..", __FILE__),
|
132
|
-
:strawberry => [lambda { Strawberry }, 'strawberry']
|
133
|
-
begin
|
134
|
-
build_stack :strawberry
|
135
|
-
assert_handlers %w[Strawberry]
|
136
|
-
ensure
|
137
|
-
unregister_middleware Faraday::Middleware, :strawberry
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_missing_dependencies
|
142
|
-
build_stack Broken
|
143
|
-
err = assert_raises RuntimeError do
|
144
|
-
@conn.get('/')
|
145
|
-
end
|
146
|
-
assert_match "missing dependency for MiddlewareStackTest::Broken: ", err.message
|
147
|
-
assert_match "zomg/i_dont/exist", err.message
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_env_stored_on_middleware_response_has_reference_to_the_response
|
151
|
-
env = response = nil
|
152
|
-
build_stack Struct.new(:app) {
|
153
|
-
define_method(:call) { |e| env, response = e, app.call(e) }
|
154
|
-
}
|
155
|
-
@conn.get("/")
|
156
|
-
assert_same env.response, response.env.response
|
157
|
-
end
|
158
|
-
|
159
|
-
private
|
160
|
-
|
161
|
-
# make a stack with test adapter that reflects the order of middleware
|
162
|
-
def build_stack(*handlers)
|
163
|
-
@builder.build do |b|
|
164
|
-
handlers.each { |handler| b.use(*handler) }
|
165
|
-
yield(b) if block_given?
|
166
|
-
|
167
|
-
@builder.adapter :test, &test_adapter
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def build_handlers_stack(*handlers)
|
172
|
-
@builder.build do |b|
|
173
|
-
handlers.each { |handler| b.use(*handler) }
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_adapter
|
178
|
-
Proc.new do |stub|
|
179
|
-
stub.get '/' do |env|
|
180
|
-
# echo the "X-Middleware" request header in the body
|
181
|
-
[200, {}, env[:request_headers]['X-Middleware'].to_s]
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def assert_handlers(list)
|
187
|
-
echoed_list = @conn.get('/').body.to_s.split(':')
|
188
|
-
echoed_list.shift if echoed_list.first == ''
|
189
|
-
assert_equal list, echoed_list
|
190
|
-
end
|
191
|
-
|
192
|
-
def unregister_middleware(component, key)
|
193
|
-
# TODO: unregister API?
|
194
|
-
component.instance_variable_get('@registered_middleware').delete(key)
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
class MiddlewareStackOrderTest < Faraday::TestCase
|
199
|
-
def test_adding_response_middleware_after_adapter
|
200
|
-
response_after_adapter = lambda do
|
201
|
-
Faraday::RackBuilder.new do |b|
|
202
|
-
b.adapter :test
|
203
|
-
b.response :raise_error
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
assert_output("", expected_middleware_warning, &response_after_adapter)
|
208
|
-
end
|
209
|
-
|
210
|
-
def test_adding_request_middleware_after_adapter
|
211
|
-
request_after_adapter = lambda do
|
212
|
-
Faraday::RackBuilder.new do |b|
|
213
|
-
b.adapter :test
|
214
|
-
b.request :multipart
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
assert_output("", expected_middleware_warning, &request_after_adapter)
|
219
|
-
end
|
220
|
-
|
221
|
-
def test_adding_request_middleware_after_adapter_via_use
|
222
|
-
use_after_adapter = lambda do
|
223
|
-
Faraday::RackBuilder.new do |b|
|
224
|
-
b.adapter :test
|
225
|
-
b.use Faraday::Request::Multipart
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
assert_output("", expected_middleware_warning, &use_after_adapter)
|
230
|
-
end
|
231
|
-
|
232
|
-
def test_adding_request_middleware_after_adapter_via_insert
|
233
|
-
insert_after_adapter = lambda do
|
234
|
-
Faraday::RackBuilder.new do |b|
|
235
|
-
b.adapter :test
|
236
|
-
b.insert(1, Faraday::Request::Multipart)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
assert_output("", expected_middleware_warning, &insert_after_adapter)
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_adding_request_middleware_before_adapter_via_insert_no_warning
|
244
|
-
builder = Faraday::RackBuilder.new do |b|
|
245
|
-
b.adapter :test
|
246
|
-
end
|
247
|
-
|
248
|
-
insert_before_adapter = lambda do
|
249
|
-
builder.insert(0, Faraday::Request::Multipart)
|
250
|
-
end
|
251
|
-
|
252
|
-
assert_output("", "", &insert_before_adapter)
|
253
|
-
end
|
254
|
-
|
255
|
-
private
|
256
|
-
|
257
|
-
def expected_middleware_warning
|
258
|
-
/Unexpected middleware set after the adapter/
|
259
|
-
end
|
260
|
-
end
|
data/test/multibyte.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ファイル
|