stripe 1.58.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +4 -0
- data/.travis.yml +1 -2
- data/Gemfile +11 -12
- data/History.txt +8 -0
- data/README.md +44 -31
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/stripe.rb +4 -344
- data/lib/stripe/account.rb +4 -4
- data/lib/stripe/api_operations/create.rb +2 -2
- data/lib/stripe/api_operations/delete.rb +2 -2
- data/lib/stripe/api_operations/list.rb +2 -2
- data/lib/stripe/api_operations/request.rb +9 -3
- data/lib/stripe/api_operations/save.rb +4 -6
- data/lib/stripe/api_resource.rb +2 -2
- data/lib/stripe/bank_account.rb +2 -2
- data/lib/stripe/bitcoin_receiver.rb +1 -1
- data/lib/stripe/charge.rb +12 -12
- data/lib/stripe/customer.rb +6 -6
- data/lib/stripe/dispute.rb +2 -3
- data/lib/stripe/errors.rb +20 -10
- data/lib/stripe/invoice.rb +4 -4
- data/lib/stripe/list_object.rb +2 -2
- data/lib/stripe/order.rb +4 -4
- data/lib/stripe/reversal.rb +1 -1
- data/lib/stripe/source.rb +2 -2
- data/lib/stripe/stripe_client.rb +396 -0
- data/lib/stripe/stripe_response.rb +48 -0
- data/lib/stripe/transfer.rb +2 -2
- data/lib/stripe/util.rb +6 -6
- data/lib/stripe/version.rb +1 -1
- data/spec/fixtures.json +0 -0
- data/spec/fixtures.yaml +0 -0
- data/spec/spec.json +0 -0
- data/spec/spec.yaml +0 -0
- data/stripe.gemspec +1 -1
- data/test/api_fixtures.rb +29 -0
- data/test/api_stub_helpers.rb +125 -0
- data/test/stripe/account_test.rb +153 -247
- data/test/stripe/alipay_account_test.rb +10 -2
- data/test/stripe/api_operations_test.rb +3 -3
- data/test/stripe/api_resource_test.rb +139 -499
- data/test/stripe/apple_pay_domain_test.rb +22 -23
- data/test/stripe/application_fee_refund_test.rb +22 -31
- data/test/stripe/application_fee_test.rb +6 -17
- data/test/stripe/balance_test.rb +3 -3
- data/test/stripe/bank_account_test.rb +31 -11
- data/test/stripe/bitcoin_receiver_test.rb +51 -42
- data/test/stripe/bitcoin_transaction_test.rb +11 -19
- data/test/stripe/charge_test.rb +39 -126
- data/test/stripe/country_spec_test.rb +7 -30
- data/test/stripe/coupon_test.rb +33 -17
- data/test/stripe/customer_card_test.rb +25 -46
- data/test/stripe/customer_test.rb +86 -81
- data/test/stripe/dispute_test.rb +27 -38
- data/test/stripe/errors_test.rb +2 -2
- data/test/stripe/file_upload_test.rb +32 -24
- data/test/stripe/invoice_item_test.rb +46 -10
- data/test/stripe/invoice_test.rb +48 -48
- data/test/stripe/list_object_test.rb +22 -31
- data/test/stripe/order_return_test.rb +11 -15
- data/test/stripe/order_test.rb +38 -51
- data/test/stripe/plan_test.rb +39 -18
- data/test/stripe/product_test.rb +29 -33
- data/test/stripe/recipient_card_test.rb +23 -40
- data/test/stripe/recipient_test.rb +39 -10
- data/test/stripe/refund_test.rb +20 -45
- data/test/stripe/reversal_test.rb +27 -31
- data/test/stripe/sku_test.rb +36 -19
- data/test/stripe/source_test.rb +26 -66
- data/test/stripe/stripe_client_test.rb +428 -0
- data/test/stripe/stripe_object_test.rb +6 -2
- data/test/stripe/stripe_response_test.rb +46 -0
- data/test/stripe/subscription_item_test.rb +37 -59
- data/test/stripe/subscription_test.rb +40 -176
- data/test/stripe/three_d_secure_test.rb +13 -12
- data/test/stripe/transfer_test.rb +36 -19
- data/test/stripe_test.rb +3 -36
- data/test/test_data.rb +5 -931
- data/test/test_helper.rb +21 -25
- metadata +22 -17
- data/test/stripe/charge_refund_test.rb +0 -67
- data/test/stripe/metadata_test.rb +0 -129
@@ -2,9 +2,17 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
|
3
3
|
module Stripe
|
4
4
|
class AlipayAccountTest < Test::Unit::TestCase
|
5
|
-
|
5
|
+
FIXTURE = API_FIXTURES.fetch(:alipay_account)
|
6
|
+
|
7
|
+
should "raise on #retrieve" do
|
8
|
+
assert_raises NotImplementedError do
|
9
|
+
Stripe::AlipayAccount.retrieve FIXTURE[:id]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should "raise on #update" do
|
6
14
|
assert_raises NotImplementedError do
|
7
|
-
Stripe::AlipayAccount.
|
15
|
+
Stripe::AlipayAccount.update FIXTURE[:id], {}
|
8
16
|
end
|
9
17
|
end
|
10
18
|
end
|
@@ -13,9 +13,9 @@ module Stripe
|
|
13
13
|
|
14
14
|
context ".update" do
|
15
15
|
should "post the correct parameters to the resource URL" do
|
16
|
-
|
17
|
-
with(
|
18
|
-
|
16
|
+
stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id").
|
17
|
+
with(body: { foo: "bar" }).
|
18
|
+
to_return(body: JSON.generate({ foo: "bar" }))
|
19
19
|
resource = UpdateableResource::update("id", { foo: "bar" })
|
20
20
|
assert_equal('bar', resource.foo)
|
21
21
|
end
|
@@ -24,30 +24,30 @@ module Stripe
|
|
24
24
|
end
|
25
25
|
|
26
26
|
should "creating a new APIResource should not fetch over the network" do
|
27
|
-
@mock.expects(:get).never
|
28
27
|
Stripe::Customer.new("someid")
|
28
|
+
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
29
29
|
end
|
30
30
|
|
31
31
|
should "creating a new APIResource from a hash should not fetch over the network" do
|
32
|
-
@mock.expects(:get).never
|
33
32
|
Stripe::Customer.construct_from({
|
34
33
|
:id => "somecustomer",
|
35
34
|
:card => {:id => "somecard", :object => "card"},
|
36
35
|
:object => "customer"
|
37
36
|
})
|
37
|
+
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
38
38
|
end
|
39
39
|
|
40
40
|
should "setting an attribute should not cause a network request" do
|
41
|
-
@mock.expects(:get).never
|
42
|
-
@mock.expects(:post).never
|
43
41
|
c = Stripe::Customer.new("test_customer");
|
44
42
|
c.card = {:id => "somecard", :object => "card"}
|
43
|
+
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
44
|
+
assert_not_requested :post, %r{#{Stripe.api_base}/.*}
|
45
45
|
end
|
46
46
|
|
47
47
|
should "accessing id should not issue a fetch" do
|
48
|
-
@mock.expects(:get).never
|
49
48
|
c = Stripe::Customer.new("test_customer")
|
50
49
|
c.id
|
50
|
+
assert_not_requested :get, %r{#{Stripe.api_base}/.*}
|
51
51
|
end
|
52
52
|
|
53
53
|
should "not specifying api credentials should raise an exception" do
|
@@ -73,129 +73,41 @@ module Stripe
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
should "specifying invalid api credentials should raise an exception" do
|
77
|
-
Stripe.api_key = "invalid"
|
78
|
-
response = make_response(make_invalid_api_key_error, 401)
|
79
|
-
assert_raises Stripe::AuthenticationError do
|
80
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
81
|
-
Stripe::Customer.retrieve("failing_customer")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
should "AuthenticationErrors should have an http status, http body, and JSON body" do
|
86
|
-
Stripe.api_key = "invalid"
|
87
|
-
response = make_response(make_invalid_api_key_error, 401)
|
88
|
-
begin
|
89
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
90
|
-
Stripe::Customer.retrieve("failing_customer")
|
91
|
-
rescue Stripe::AuthenticationError => e
|
92
|
-
assert_equal(401, e.http_status)
|
93
|
-
assert_equal(true, !!e.http_body)
|
94
|
-
assert_equal(true, !!e.json_body[:error][:message])
|
95
|
-
assert_equal(make_invalid_api_key_error[:error][:message], e.json_body[:error][:message])
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
76
|
should "send expand on fetch properly" do
|
100
|
-
|
101
|
-
with("
|
102
|
-
|
77
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_test_charge").
|
78
|
+
with(query: { "expand" => ["customer"] }).
|
79
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
103
80
|
|
104
81
|
Stripe::Charge.retrieve({:id => 'ch_test_charge', :expand => [:customer]})
|
105
82
|
end
|
106
83
|
|
107
84
|
should "preserve expand across refreshes" do
|
108
|
-
|
109
|
-
with("
|
110
|
-
|
85
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_test_charge").
|
86
|
+
with(query: { "expand" => ["customer"] }).
|
87
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
111
88
|
|
112
89
|
ch = Stripe::Charge.retrieve({:id => 'ch_test_charge', :expand => [:customer]})
|
113
90
|
ch.refresh
|
114
91
|
end
|
115
92
|
|
116
93
|
should "send expand when fetching through ListObject" do
|
117
|
-
|
118
|
-
|
119
|
-
returns(make_response(make_customer))
|
94
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
95
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
120
96
|
|
121
|
-
|
122
|
-
with("
|
123
|
-
|
97
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/c_test_customer/sources/cc_test_card").
|
98
|
+
with(query: { "expand" => ["customer"] }).
|
99
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
124
100
|
|
125
101
|
customer = Stripe::Customer.retrieve('c_test_customer')
|
126
102
|
customer.sources.retrieve({:id => 'cc_test_card', :expand => [:customer]})
|
127
103
|
end
|
128
104
|
|
129
|
-
should "send stripe account as header when set" do
|
130
|
-
stripe_account = "acct_0000"
|
131
|
-
Stripe.expects(:execute_request).with do |opts|
|
132
|
-
opts[:headers][:stripe_account] == stripe_account
|
133
|
-
end.returns(make_response(make_charge))
|
134
|
-
|
135
|
-
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
136
|
-
{:stripe_account => stripe_account, :api_key => 'sk_test_local'})
|
137
|
-
end
|
138
|
-
|
139
|
-
should "not send stripe account as header when not set" do
|
140
|
-
Stripe.expects(:execute_request).with do |opts|
|
141
|
-
opts[:headers][:stripe_account].nil?
|
142
|
-
end.returns(make_response(make_charge))
|
143
|
-
|
144
|
-
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
145
|
-
'sk_test_local')
|
146
|
-
end
|
147
|
-
|
148
|
-
should "handle error response with empty body" do
|
149
|
-
response = make_response('', 500)
|
150
|
-
@mock.expects(:post).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
151
|
-
|
152
|
-
e = assert_raises Stripe::APIError do
|
153
|
-
Stripe::Charge.create
|
154
|
-
end
|
155
|
-
|
156
|
-
assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
|
157
|
-
end
|
158
|
-
|
159
|
-
should "handle error response with non-object error value" do
|
160
|
-
response = make_response('{"error": "foo"}', 500)
|
161
|
-
@mock.expects(:post).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
162
|
-
|
163
|
-
e = assert_raises Stripe::APIError do
|
164
|
-
Stripe::Charge.create
|
165
|
-
end
|
166
|
-
|
167
|
-
assert_equal 'Invalid response object from API: "{\"error\": \"foo\"}" (HTTP response code was 500)', e.message
|
168
|
-
end
|
169
|
-
|
170
|
-
should "have default open and read timeouts" do
|
171
|
-
assert_equal Stripe.open_timeout, 30
|
172
|
-
assert_equal Stripe.read_timeout, 80
|
173
|
-
end
|
174
|
-
|
175
|
-
should "allow configurable open and read timeouts" do
|
176
|
-
original_timeouts = Stripe.open_timeout, Stripe.read_timeout
|
177
|
-
|
178
|
-
begin
|
179
|
-
Stripe.open_timeout = 999
|
180
|
-
Stripe.read_timeout = 998
|
181
|
-
|
182
|
-
Stripe.expects(:execute_request).with do |opts|
|
183
|
-
opts[:open_timeout] == 999 && opts[:timeout] == 998
|
184
|
-
end.returns(make_response(make_charge))
|
185
|
-
|
186
|
-
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
187
|
-
'sk_test_local')
|
188
|
-
ensure
|
189
|
-
Stripe.open_timeout, Stripe.read_timeout = original_timeouts
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
105
|
context "when specifying per-object credentials" do
|
194
106
|
context "with no global API key set" do
|
195
107
|
should "use the per-object credential when creating" do
|
196
|
-
Stripe.
|
197
|
-
|
198
|
-
|
108
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
109
|
+
with(headers: {"Authorization" => "Bearer sk_test_local"}).
|
110
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
199
111
|
|
200
112
|
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
201
113
|
'sk_test_local')
|
@@ -212,23 +124,21 @@ module Stripe
|
|
212
124
|
end
|
213
125
|
|
214
126
|
should "use the per-object credential when creating" do
|
215
|
-
Stripe.
|
216
|
-
|
217
|
-
|
127
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
128
|
+
with(headers: {"Authorization" => "Bearer local"}).
|
129
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
218
130
|
|
219
131
|
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
|
220
132
|
'local')
|
221
133
|
end
|
222
134
|
|
223
135
|
should "use the per-object credential when retrieving and making other calls" do
|
224
|
-
Stripe.
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
opts[:headers]['Authorization'] == 'Bearer local'
|
231
|
-
end.returns(make_response(make_refund))
|
136
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_test_charge").
|
137
|
+
with(headers: {"Authorization" => "Bearer local"}).
|
138
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
139
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_test_charge/refunds").
|
140
|
+
with(headers: {"Authorization" => "Bearer local"}).
|
141
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:refund)))
|
232
142
|
|
233
143
|
ch = Stripe::Charge.retrieve('ch_test_charge', 'local')
|
234
144
|
ch.refunds.create
|
@@ -237,125 +147,49 @@ module Stripe
|
|
237
147
|
end
|
238
148
|
|
239
149
|
context "with valid credentials" do
|
240
|
-
should "send along the idempotency-key header" do
|
241
|
-
Stripe.expects(:execute_request).with do |opts|
|
242
|
-
opts[:headers][:idempotency_key] == 'bar'
|
243
|
-
end.returns(make_response(make_charge))
|
244
|
-
|
245
|
-
Stripe::Charge.create({:card => {:number => '4242424242424242'}}, {
|
246
|
-
:idempotency_key => 'bar',
|
247
|
-
:api_key => 'local',
|
248
|
-
})
|
249
|
-
end
|
250
|
-
|
251
150
|
should "urlencode values in GET params" do
|
252
|
-
|
253
|
-
|
151
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges").
|
152
|
+
with(query: { customer: "test customer" }).
|
153
|
+
to_return(body: JSON.generate({
|
154
|
+
data: [API_FIXTURES.fetch(:charge)]
|
155
|
+
}))
|
254
156
|
charges = Stripe::Charge.list(:customer => 'test customer').data
|
255
157
|
assert charges.kind_of? Array
|
256
158
|
end
|
257
159
|
|
258
160
|
should "construct URL properly with base query parameters" do
|
259
|
-
|
260
|
-
|
161
|
+
stub_request(:get, "#{Stripe.api_base}/v1/invoices").
|
162
|
+
with(query: { customer: "test_customer" }).
|
163
|
+
to_return(body: JSON.generate({
|
164
|
+
data: [API_FIXTURES.fetch(:invoice)],
|
165
|
+
url: "/v1/invoices"
|
166
|
+
}))
|
261
167
|
invoices = Stripe::Invoice.list(:customer => 'test_customer')
|
262
168
|
|
263
|
-
|
169
|
+
stub_request(:get, "#{Stripe.api_base}/v1/invoices").
|
170
|
+
with(query: { customer: "test_customer", paid: "true" }).
|
171
|
+
to_return(body: JSON.generate({
|
172
|
+
data: [API_FIXTURES.fetch(:invoice)],
|
173
|
+
url: "/v1/invoices"
|
174
|
+
}))
|
264
175
|
invoices.list(:paid => true)
|
265
176
|
end
|
266
177
|
|
267
|
-
should "a 400 should give an InvalidRequestError with http status, body, and JSON body" do
|
268
|
-
response = make_response(make_missing_id_error, 400)
|
269
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
270
|
-
begin
|
271
|
-
Stripe::Customer.retrieve("foo")
|
272
|
-
rescue Stripe::InvalidRequestError => e
|
273
|
-
assert_equal(400, e.http_status)
|
274
|
-
assert_equal(true, !!e.http_body)
|
275
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
should "a 401 should give an AuthenticationError with http status, body, and JSON body" do
|
280
|
-
response = make_response(make_missing_id_error, 401)
|
281
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
282
|
-
begin
|
283
|
-
Stripe::Customer.retrieve("foo")
|
284
|
-
rescue Stripe::AuthenticationError => e
|
285
|
-
assert_equal(401, e.http_status)
|
286
|
-
assert_equal(true, !!e.http_body)
|
287
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
should "a 402 should give a CardError with http status, body, and JSON body" do
|
292
|
-
response = make_response(make_missing_id_error, 402)
|
293
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
294
|
-
begin
|
295
|
-
Stripe::Customer.retrieve("foo")
|
296
|
-
rescue Stripe::CardError => e
|
297
|
-
assert_equal(402, e.http_status)
|
298
|
-
assert_equal(true, !!e.http_body)
|
299
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
should "a 403 should give a PermissionError with http status, body, and JSON body" do
|
304
|
-
response = make_response(make_missing_id_error, 403)
|
305
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 403))
|
306
|
-
begin
|
307
|
-
Stripe::Customer.retrieve("foo")
|
308
|
-
rescue Stripe::PermissionError => e
|
309
|
-
assert_equal(403, e.http_status)
|
310
|
-
assert_equal(true, !!e.http_body)
|
311
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
should "a 404 should give an InvalidRequestError with http status, body, and JSON body" do
|
316
|
-
response = make_response(make_missing_id_error, 404)
|
317
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
318
|
-
begin
|
319
|
-
Stripe::Customer.retrieve("foo")
|
320
|
-
rescue Stripe::InvalidRequestError => e
|
321
|
-
assert_equal(404, e.http_status)
|
322
|
-
assert_equal(true, !!e.http_body)
|
323
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
should "a 429 should give a RateLimitError with http status, body, and JSON body" do
|
328
|
-
response = make_response(make_rate_limit_error, 429)
|
329
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 429))
|
330
|
-
begin
|
331
|
-
Stripe::Customer.retrieve("foo")
|
332
|
-
rescue Stripe::RateLimitError => e
|
333
|
-
assert_equal(429, e.http_status)
|
334
|
-
assert_equal(true, !!e.http_body)
|
335
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
178
|
should "setting a nil value for a param should exclude that param from the request" do
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
(url =~ %r{^#{Stripe.api_base}/v1/charges?} &&
|
344
|
-
query.keys.sort == ['offset', 'sad'])
|
345
|
-
end.returns(make_response({ :count => 1, :data => [make_charge] }))
|
179
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges").
|
180
|
+
with(query: { offset: 5, sad: false }).
|
181
|
+
to_return(body: JSON.generate({ :count => 1, :data => [API_FIXTURES.fetch(:charge)] }))
|
346
182
|
Stripe::Charge.list(:count => nil, :offset => 5, :sad => false)
|
347
183
|
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
|
352
|
-
end.returns(make_response({ :count => 1, :data => [make_charge] }))
|
184
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
185
|
+
with(body: { 'amount' => '50', 'currency' => 'usd' }).
|
186
|
+
to_return(body: JSON.generate({ :count => 1, :data => [API_FIXTURES.fetch(:charge)] }))
|
353
187
|
Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
354
188
|
end
|
355
189
|
|
356
190
|
should "requesting with a unicode ID should result in a request" do
|
357
|
-
|
358
|
-
|
191
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83").
|
192
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 404)
|
359
193
|
c = Stripe::Customer.new("☃")
|
360
194
|
assert_raises(Stripe::InvalidRequestError) { c.refresh }
|
361
195
|
end
|
@@ -366,28 +200,29 @@ module Stripe
|
|
366
200
|
end
|
367
201
|
|
368
202
|
should "making a GET request with parameters should have a query string and no body" do
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
Stripe::Charge.list(
|
203
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges").
|
204
|
+
with(query: { limit: 1 }).
|
205
|
+
to_return(body: JSON.generate({ :data => [API_FIXTURES.fetch(:charge)] }))
|
206
|
+
Stripe::Charge.list({ :limit => 1 })
|
373
207
|
end
|
374
208
|
|
375
209
|
should "making a POST request with parameters should have a body and no query string" do
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
Stripe::Charge.create(params)
|
210
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
211
|
+
with(body: {'amount' => '100', 'currency' => 'usd', 'card' => 'sc_token'}).
|
212
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
213
|
+
Stripe::Charge.create({ :amount => 100, :currency => 'usd', :card => 'sc_token' })
|
381
214
|
end
|
382
215
|
|
383
216
|
should "loading an object should issue a GET request" do
|
384
|
-
|
217
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/test_customer").
|
218
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
385
219
|
c = Stripe::Customer.new("test_customer")
|
386
220
|
c.refresh
|
387
221
|
end
|
388
222
|
|
389
223
|
should "using array accessors should be the same as the method interface" do
|
390
|
-
|
224
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/test_customer").
|
225
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
391
226
|
c = Stripe::Customer.new("test_customer")
|
392
227
|
c.refresh
|
393
228
|
assert_equal c.created, c[:created]
|
@@ -397,37 +232,32 @@ module Stripe
|
|
397
232
|
end
|
398
233
|
|
399
234
|
should "accessing a property other than id or parent on an unfetched object should fetch it" do
|
400
|
-
|
235
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges").
|
236
|
+
with(query: { customer: "test_customer" }).
|
237
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
401
238
|
c = Stripe::Customer.new("test_customer")
|
402
239
|
c.charges
|
403
240
|
end
|
404
241
|
|
405
242
|
should "updating an object should issue a POST request with only the changed properties" do
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
c = Stripe::Customer.construct_from(
|
243
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
244
|
+
with(body: { 'description' => 'another_mn' }).
|
245
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
246
|
+
c = Stripe::Customer.construct_from(API_FIXTURES.fetch(:customer))
|
410
247
|
c.description = "another_mn"
|
411
248
|
c.save
|
412
249
|
end
|
413
250
|
|
414
251
|
should "updating should merge in returned properties" do
|
415
|
-
|
252
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
253
|
+
with(body: { 'description' => 'another_mn' }).
|
254
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
416
255
|
c = Stripe::Customer.new("c_test_customer")
|
417
256
|
c.description = "another_mn"
|
418
257
|
c.save
|
419
258
|
assert_equal false, c.livemode
|
420
259
|
end
|
421
260
|
|
422
|
-
should "updating should send along the idempotency-key header" do
|
423
|
-
Stripe.expects(:execute_request).with do |opts|
|
424
|
-
opts[:headers][:idempotency_key] == 'bar'
|
425
|
-
end.returns(make_response(make_customer))
|
426
|
-
c = Stripe::Customer.new
|
427
|
-
c.save({}, { :idempotency_key => 'bar' })
|
428
|
-
assert_equal false, c.livemode
|
429
|
-
end
|
430
|
-
|
431
261
|
should "updating should fail if api_key is overwritten with nil" do
|
432
262
|
c = Stripe::Customer.new
|
433
263
|
assert_raises TypeError do
|
@@ -436,124 +266,52 @@ module Stripe
|
|
436
266
|
end
|
437
267
|
|
438
268
|
should "updating should use the supplied api_key" do
|
439
|
-
Stripe.
|
440
|
-
|
441
|
-
|
269
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers").
|
270
|
+
with(headers: {"Authorization" => "Bearer sk_test_local"}).
|
271
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
442
272
|
c = Stripe::Customer.new
|
443
273
|
c.save({}, { :api_key => 'sk_test_local' })
|
444
274
|
assert_equal false, c.livemode
|
445
275
|
end
|
446
276
|
|
447
277
|
should "deleting should send no props and result in an object that has no props other deleted" do
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
c = Stripe::Customer.construct_from(make_customer)
|
278
|
+
stub_request(:delete, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
279
|
+
to_return(body: JSON.generate({ "id" => "test_customer", "deleted" => true }))
|
280
|
+
c = Stripe::Customer.construct_from(API_FIXTURES.fetch(:customer))
|
452
281
|
c.delete
|
453
|
-
assert_equal true, c.deleted
|
454
|
-
|
455
|
-
assert_raises NoMethodError do
|
456
|
-
c.livemode
|
457
|
-
end
|
458
|
-
end
|
459
|
-
|
460
|
-
should "loading an object with properties that have specific types should instantiate those classes" do
|
461
|
-
@mock.expects(:get).once.returns(make_response(make_charge))
|
462
|
-
c = Stripe::Charge.retrieve("test_charge")
|
463
|
-
assert c.card.kind_of?(Stripe::StripeObject) && c.card.object == 'card'
|
464
282
|
end
|
465
283
|
|
466
284
|
should "loading all of an APIResource should return an array of recursively instantiated objects" do
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
285
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges").
|
286
|
+
to_return(body: JSON.generate({
|
287
|
+
data: [API_FIXTURES.fetch(:charge)]
|
288
|
+
}))
|
289
|
+
charges = Stripe::Charge.list.data
|
290
|
+
assert charges.kind_of? Array
|
291
|
+
assert charges[0].kind_of? Stripe::Charge
|
292
|
+
assert charges[0].card.kind_of?(Stripe::StripeObject)
|
472
293
|
end
|
473
294
|
|
474
295
|
should "passing in a stripe_account header should pass it through on call" do
|
475
|
-
Stripe.
|
476
|
-
|
477
|
-
|
478
|
-
opts[:headers][:stripe_account] == 'acct_abc'
|
479
|
-
end.once.returns(make_response(make_customer))
|
296
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
297
|
+
with(headers: {"Stripe-Account" => "acct_abc"}).
|
298
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
480
299
|
Stripe::Customer.retrieve("c_test_customer", {:stripe_account => 'acct_abc'})
|
481
300
|
end
|
482
301
|
|
483
302
|
should "passing in a stripe_account header should pass it through on save" do
|
484
|
-
Stripe.
|
485
|
-
|
486
|
-
|
487
|
-
opts[:headers][:stripe_account] == 'acct_abc'
|
488
|
-
end.once.returns(make_response(make_customer))
|
303
|
+
stub_request(:get, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
304
|
+
with(headers: {"Stripe-Account" => "acct_abc"}).
|
305
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
489
306
|
c = Stripe::Customer.retrieve("c_test_customer", {:stripe_account => 'acct_abc'})
|
490
307
|
|
491
|
-
Stripe.
|
492
|
-
|
493
|
-
|
494
|
-
opts[:headers][:stripe_account] == 'acct_abc' &&
|
495
|
-
opts[:payload] == 'description=FOO'
|
496
|
-
end.once.returns(make_response(make_customer))
|
308
|
+
stub_request(:post, "#{Stripe.api_base}/v1/customers/c_test_customer").
|
309
|
+
with(headers: {"Stripe-Account" => "acct_abc"}).
|
310
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:customer)))
|
497
311
|
c.description = 'FOO'
|
498
312
|
c.save
|
499
313
|
end
|
500
314
|
|
501
|
-
context "error checking" do
|
502
|
-
|
503
|
-
should "404s should raise an InvalidRequestError" do
|
504
|
-
response = make_response(make_missing_id_error, 404)
|
505
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
506
|
-
|
507
|
-
rescued = false
|
508
|
-
begin
|
509
|
-
Stripe::Customer.new("test_customer").refresh
|
510
|
-
assert false #shouldn't get here either
|
511
|
-
rescue Stripe::InvalidRequestError => e # we don't use assert_raises because we want to examine e
|
512
|
-
rescued = true
|
513
|
-
assert e.kind_of? Stripe::InvalidRequestError
|
514
|
-
assert_equal "id", e.param
|
515
|
-
assert_equal "Missing id", e.message
|
516
|
-
end
|
517
|
-
|
518
|
-
assert_equal true, rescued
|
519
|
-
end
|
520
|
-
|
521
|
-
should "5XXs should raise an APIError" do
|
522
|
-
response = make_response(make_api_error, 500)
|
523
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
524
|
-
|
525
|
-
rescued = false
|
526
|
-
begin
|
527
|
-
Stripe::Customer.new("test_customer").refresh
|
528
|
-
assert false #shouldn't get here either
|
529
|
-
rescue Stripe::APIError => e # we don't use assert_raises because we want to examine e
|
530
|
-
rescued = true
|
531
|
-
assert e.kind_of? Stripe::APIError
|
532
|
-
end
|
533
|
-
|
534
|
-
assert_equal true, rescued
|
535
|
-
end
|
536
|
-
|
537
|
-
should "402s should raise a CardError" do
|
538
|
-
response = make_response(make_invalid_exp_year_error, 402)
|
539
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 402))
|
540
|
-
|
541
|
-
rescued = false
|
542
|
-
begin
|
543
|
-
Stripe::Customer.new("test_customer").refresh
|
544
|
-
assert false #shouldn't get here either
|
545
|
-
rescue Stripe::CardError => e # we don't use assert_raises because we want to examine e
|
546
|
-
rescued = true
|
547
|
-
assert e.kind_of? Stripe::CardError
|
548
|
-
assert_equal "invalid_expiry_year", e.code
|
549
|
-
assert_equal "exp_year", e.param
|
550
|
-
assert_equal "Your card's expiration year is invalid", e.message
|
551
|
-
end
|
552
|
-
|
553
|
-
assert_equal true, rescued
|
554
|
-
end
|
555
|
-
end
|
556
|
-
|
557
315
|
should 'add key to nested objects' do
|
558
316
|
acct = Stripe::Account.construct_from({
|
559
317
|
:id => 'myid',
|
@@ -564,7 +322,9 @@ module Stripe
|
|
564
322
|
}
|
565
323
|
})
|
566
324
|
|
567
|
-
|
325
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
326
|
+
with(body: { legal_entity: { first_name: "Bob" } }).
|
327
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
568
328
|
|
569
329
|
acct.legal_entity.first_name = 'Bob'
|
570
330
|
acct.save
|
@@ -572,27 +332,31 @@ module Stripe
|
|
572
332
|
|
573
333
|
should 'save nothing if nothing changes' do
|
574
334
|
acct = Stripe::Account.construct_from({
|
575
|
-
:id => '
|
335
|
+
:id => 'acct_id',
|
576
336
|
:metadata => {
|
577
337
|
:key => 'value'
|
578
338
|
}
|
579
339
|
})
|
580
340
|
|
581
|
-
|
341
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id").
|
342
|
+
with(body: {}).
|
343
|
+
to_return(body: JSON.generate({ "id" => "acct_id" }))
|
582
344
|
|
583
345
|
acct.save
|
584
346
|
end
|
585
347
|
|
586
348
|
should 'not save nested API resources' do
|
587
349
|
ch = Stripe::Charge.construct_from({
|
588
|
-
:id => '
|
350
|
+
:id => 'ch_id',
|
589
351
|
:customer => {
|
590
352
|
:object => 'customer',
|
591
353
|
:id => 'customer_id'
|
592
354
|
}
|
593
355
|
})
|
594
356
|
|
595
|
-
|
357
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id").
|
358
|
+
with(body: {}).
|
359
|
+
to_return(body: JSON.generate({ "id" => "ch_id" }))
|
596
360
|
|
597
361
|
ch.customer.description = 'Bob'
|
598
362
|
ch.save
|
@@ -610,14 +374,9 @@ module Stripe
|
|
610
374
|
}
|
611
375
|
})
|
612
376
|
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
any_of(
|
617
|
-
'legal_entity[address][line1]=Test2&legal_entity[address][city]=',
|
618
|
-
'legal_entity[address][city]=&legal_entity[address][line1]=Test2'
|
619
|
-
)
|
620
|
-
).returns(make_response({"id" => "myid"}))
|
377
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
378
|
+
with(body: { legal_entity: { address: { line1: "Test2", city: "" } } }).
|
379
|
+
to_return(body: JSON.generate({ "id" => "my_id" }))
|
621
380
|
|
622
381
|
acct.legal_entity.address = {:line1 => 'Test2'}
|
623
382
|
acct.save
|
@@ -629,7 +388,9 @@ module Stripe
|
|
629
388
|
:legal_entity => {}
|
630
389
|
})
|
631
390
|
|
632
|
-
|
391
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
392
|
+
with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } }).
|
393
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
633
394
|
|
634
395
|
acct.legal_entity.additional_owners = [{:first_name => 'Bob'}]
|
635
396
|
acct.save
|
@@ -643,7 +404,12 @@ module Stripe
|
|
643
404
|
}
|
644
405
|
})
|
645
406
|
|
646
|
-
|
407
|
+
# Note that this isn't a perfect check because we're using webmock's
|
408
|
+
# data decoding, which isn't aware of the Stripe array encoding that we
|
409
|
+
# use here.
|
410
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
411
|
+
with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } }).
|
412
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
647
413
|
|
648
414
|
acct.legal_entity.additional_owners << {:first_name => 'Bob'}
|
649
415
|
acct.save
|
@@ -657,7 +423,12 @@ module Stripe
|
|
657
423
|
}
|
658
424
|
})
|
659
425
|
|
660
|
-
|
426
|
+
# Note that this isn't a perfect check because we're using webmock's
|
427
|
+
# data decoding, which isn't aware of the Stripe array encoding that we
|
428
|
+
# use here.
|
429
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
430
|
+
with(body: { legal_entity: { additional_owners: [{ first_name: "Janet" }] } }).
|
431
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
661
432
|
|
662
433
|
acct.legal_entity.additional_owners[1].first_name = 'Janet'
|
663
434
|
acct.save
|
@@ -672,7 +443,9 @@ module Stripe
|
|
672
443
|
:currencies_supported => ['usd', 'cad']
|
673
444
|
})
|
674
445
|
|
675
|
-
|
446
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
447
|
+
with(body: {}).
|
448
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
676
449
|
|
677
450
|
acct.save
|
678
451
|
end
|
@@ -685,7 +458,9 @@ module Stripe
|
|
685
458
|
}
|
686
459
|
})
|
687
460
|
|
688
|
-
|
461
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid").
|
462
|
+
with(body: {}).
|
463
|
+
to_return(body: JSON.generate({ "id" => "myid" }))
|
689
464
|
|
690
465
|
acct.save
|
691
466
|
end
|
@@ -696,8 +471,9 @@ module Stripe
|
|
696
471
|
:display_name => nil,
|
697
472
|
})
|
698
473
|
|
699
|
-
|
700
|
-
|
474
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts").
|
475
|
+
with(body: { display_name: "stripe" }).
|
476
|
+
to_return(body: JSON.generate({ "id" => "acct_123" }))
|
701
477
|
|
702
478
|
account.display_name = 'stripe'
|
703
479
|
account.save
|
@@ -709,148 +485,12 @@ module Stripe
|
|
709
485
|
:display_name => nil,
|
710
486
|
})
|
711
487
|
|
712
|
-
|
713
|
-
|
488
|
+
stub_request(:post, "#{Stripe.api_base}/v1/accounts").
|
489
|
+
with(body: { display_name: "stripe", metadata: { key: "value" } }).
|
490
|
+
to_return(body: JSON.generate({ "id" => "acct_123" }))
|
714
491
|
|
715
492
|
account.save(:display_name => 'stripe', :metadata => {:key => 'value' })
|
716
493
|
end
|
717
494
|
end
|
718
|
-
|
719
|
-
context "with retries" do
|
720
|
-
setup do
|
721
|
-
Stripe.stubs(:max_network_retries).returns(2)
|
722
|
-
end
|
723
|
-
|
724
|
-
should 'retry failed network requests if specified and raise if error persists' do
|
725
|
-
Stripe.expects(:sleep_time).at_least_once.returns(0)
|
726
|
-
@mock.expects(:post).times(3).with('https://api.stripe.com/v1/charges', nil, 'amount=50¤cy=usd').
|
727
|
-
raises(Errno::ECONNREFUSED.new)
|
728
|
-
|
729
|
-
err = assert_raises Stripe::APIConnectionError do
|
730
|
-
Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
731
|
-
end
|
732
|
-
assert_match(/Request was retried 2 times/, err.message)
|
733
|
-
end
|
734
|
-
|
735
|
-
should 'retry failed network requests if specified and return successful response' do
|
736
|
-
Stripe.expects(:sleep_time).at_least_once.returns(0)
|
737
|
-
response = make_response({"id" => "myid"})
|
738
|
-
@mock.expects(:post).times(2).with('https://api.stripe.com/v1/charges', nil, 'amount=50¤cy=usd').
|
739
|
-
raises(Errno::ECONNREFUSED.new).
|
740
|
-
then.
|
741
|
-
returns(response)
|
742
|
-
|
743
|
-
result = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
744
|
-
assert_equal "myid", result.id
|
745
|
-
end
|
746
|
-
|
747
|
-
should 'not add an idempotency key to GET requests' do
|
748
|
-
SecureRandom.expects(:uuid).times(0)
|
749
|
-
Stripe.expects(:execute_request).with do |opts|
|
750
|
-
opts[:headers][:idempotency_key].nil?
|
751
|
-
end.returns(make_response({"id" => "myid"}))
|
752
|
-
|
753
|
-
Stripe::Charge.list
|
754
|
-
end
|
755
|
-
|
756
|
-
should 'ensure there is always an idempotency_key on POST requests' do
|
757
|
-
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
|
758
|
-
Stripe.expects(:execute_request).with do |opts|
|
759
|
-
opts[:headers]['Idempotency-Key'] == "random_key"
|
760
|
-
end.returns(make_response({"id" => "myid"}))
|
761
|
-
|
762
|
-
Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
763
|
-
end
|
764
|
-
|
765
|
-
should 'ensure there is always an idempotency_key on DELETE requests' do
|
766
|
-
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
|
767
|
-
Stripe.expects(:execute_request).with do |opts|
|
768
|
-
opts[:headers]['Idempotency-Key'] == "random_key"
|
769
|
-
end.returns(make_response({"id" => "myid"}))
|
770
|
-
|
771
|
-
c = Stripe::Customer.construct_from(make_customer)
|
772
|
-
c.delete
|
773
|
-
end
|
774
|
-
|
775
|
-
should 'not override a provided idempotency_key' do
|
776
|
-
# Note that this expectation looks like `:idempotency_key` instead of
|
777
|
-
# the header `Idempotency-Key` because it's user provided as seen
|
778
|
-
# below. The ones injected by the library itself look like headers
|
779
|
-
# (`Idempotency-Key`), but rest-client does allow this symbol
|
780
|
-
# formatting and will properly override the system generated one as
|
781
|
-
# expected.
|
782
|
-
Stripe.expects(:execute_request).with do |opts|
|
783
|
-
opts[:headers][:idempotency_key] == "provided_key"
|
784
|
-
end.returns(make_response({"id" => "myid"}))
|
785
|
-
|
786
|
-
Stripe::Charge.create({:amount => 50, :currency => 'usd', :card => { :number => nil }}, {:idempotency_key => 'provided_key'})
|
787
|
-
end
|
788
|
-
|
789
|
-
end
|
790
|
-
|
791
|
-
context ".should_retry?" do
|
792
|
-
setup do
|
793
|
-
Stripe.stubs(:max_network_retries).returns(2)
|
794
|
-
end
|
795
|
-
|
796
|
-
should 'retry on a low-level network error' do
|
797
|
-
assert Stripe.should_retry?(Errno::ECONNREFUSED.new, 0)
|
798
|
-
end
|
799
|
-
|
800
|
-
should 'retry on timeout' do
|
801
|
-
assert Stripe.should_retry?(RestClient::RequestTimeout.new, 0)
|
802
|
-
end
|
803
|
-
|
804
|
-
should 'retry on a conflict' do
|
805
|
-
assert Stripe.should_retry?(RestClient::Conflict.new, 0)
|
806
|
-
end
|
807
|
-
|
808
|
-
should 'not retry at maximum count' do
|
809
|
-
refute Stripe.should_retry?(RuntimeError.new, Stripe.max_network_retries)
|
810
|
-
end
|
811
|
-
|
812
|
-
should 'not retry on a certificate validation error' do
|
813
|
-
refute Stripe.should_retry?(RestClient::SSLCertificateNotVerified.new('message'), 0)
|
814
|
-
end
|
815
|
-
end
|
816
|
-
|
817
|
-
context ".sleep_time" do
|
818
|
-
should "should grow exponentially" do
|
819
|
-
Stripe.stubs(:rand).returns(1)
|
820
|
-
Stripe.stubs(:max_network_retry_delay).returns(999)
|
821
|
-
assert_equal(Stripe.initial_network_retry_delay, Stripe.sleep_time(1))
|
822
|
-
assert_equal(Stripe.initial_network_retry_delay * 2, Stripe.sleep_time(2))
|
823
|
-
assert_equal(Stripe.initial_network_retry_delay * 4, Stripe.sleep_time(3))
|
824
|
-
assert_equal(Stripe.initial_network_retry_delay * 8, Stripe.sleep_time(4))
|
825
|
-
end
|
826
|
-
|
827
|
-
should "enforce the max_network_retry_delay" do
|
828
|
-
Stripe.stubs(:rand).returns(1)
|
829
|
-
Stripe.stubs(:initial_network_retry_delay).returns(1)
|
830
|
-
Stripe.stubs(:max_network_retry_delay).returns(2)
|
831
|
-
assert_equal(1, Stripe.sleep_time(1))
|
832
|
-
assert_equal(2, Stripe.sleep_time(2))
|
833
|
-
assert_equal(2, Stripe.sleep_time(3))
|
834
|
-
assert_equal(2, Stripe.sleep_time(4))
|
835
|
-
end
|
836
|
-
|
837
|
-
should "add some randomness" do
|
838
|
-
random_value = 0.8
|
839
|
-
Stripe.stubs(:rand).returns(random_value)
|
840
|
-
Stripe.stubs(:initial_network_retry_delay).returns(1)
|
841
|
-
Stripe.stubs(:max_network_retry_delay).returns(8)
|
842
|
-
|
843
|
-
base_value = Stripe.initial_network_retry_delay * (0.5 * (1 + random_value))
|
844
|
-
|
845
|
-
# the initial value cannot be smaller than the base,
|
846
|
-
# so the randomness is ignored
|
847
|
-
assert_equal(Stripe.initial_network_retry_delay, Stripe.sleep_time(1))
|
848
|
-
|
849
|
-
# after the first one, the randomness is applied
|
850
|
-
assert_equal(base_value * 2, Stripe.sleep_time(2))
|
851
|
-
assert_equal(base_value * 4, Stripe.sleep_time(3))
|
852
|
-
assert_equal(base_value * 8, Stripe.sleep_time(4))
|
853
|
-
end
|
854
|
-
end
|
855
495
|
end
|
856
496
|
end
|