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
data/test/stripe/source_test.rb
CHANGED
@@ -2,82 +2,42 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
2
2
|
|
3
3
|
module Stripe
|
4
4
|
class SourceTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
@mock.expects(:post).once.returns(make_response(make_source_card))
|
7
|
-
src = Stripe::Source.create(
|
8
|
-
type: 'card',
|
9
|
-
token: 'tok_test',
|
10
|
-
)
|
11
|
-
assert_equal 'src_test_card', src.id
|
12
|
-
end
|
5
|
+
FIXTURE = API_FIXTURES.fetch(:source)
|
13
6
|
|
14
|
-
should
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
should "be retrievable" do
|
8
|
+
source = Stripe::Source.retrieve(FIXTURE[:id])
|
9
|
+
assert_requested :get, "#{Stripe.api_base}/v1/sources/#{FIXTURE[:id]}"
|
10
|
+
assert source.kind_of?(Stripe::Source)
|
18
11
|
end
|
19
12
|
|
20
|
-
should
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
.returns(make_response(make_source_card(metadata: {foo: 'bar'})))
|
28
|
-
src = Stripe::Source.update('src_test_card', metadata: {foo: 'bar'})
|
29
|
-
assert_equal 'bar', src.metadata['foo']
|
13
|
+
should "be creatable" do
|
14
|
+
source = Stripe::Source.create(
|
15
|
+
type: 'card',
|
16
|
+
token: API_FIXTURES.fetch(:token)[:id]
|
17
|
+
)
|
18
|
+
assert_requested :post, "#{Stripe.api_base}/v1/sources"
|
19
|
+
assert source.kind_of?(Stripe::Card)
|
30
20
|
end
|
31
21
|
|
32
|
-
should
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
.with(
|
38
|
-
"#{Stripe.api_base}/v1/sources/src_test_card",
|
39
|
-
nil,
|
40
|
-
'metadata[foo]=bar'
|
41
|
-
)
|
42
|
-
.returns(make_response(make_source_card(metadata: {foo: 'bar'})))
|
43
|
-
src.metadata['foo'] = 'bar'
|
44
|
-
src.save
|
45
|
-
assert_equal 'bar', src.metadata['foo']
|
22
|
+
should "be saveable" do
|
23
|
+
source = Stripe::Source.retrieve(FIXTURE[:id])
|
24
|
+
source.metadata['key'] = 'value'
|
25
|
+
source.save
|
26
|
+
assert_requested :post, "#{Stripe.api_base}/v1/sources/#{FIXTURE[:id]}"
|
46
27
|
end
|
47
28
|
|
48
|
-
should
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
assert_raises NoMethodError do
|
53
|
-
src.delete
|
54
|
-
end
|
29
|
+
should "be updateable" do
|
30
|
+
source = Stripe::Source.update(FIXTURE[:id], metadata: {foo: 'bar'})
|
31
|
+
assert_requested :post, "#{Stripe.api_base}/v1/sources/#{FIXTURE[:id]}"
|
32
|
+
assert source.kind_of?(Stripe::Card)
|
55
33
|
end
|
56
34
|
|
57
|
-
|
58
|
-
|
59
|
-
Stripe::Source.
|
35
|
+
context "#verify" do
|
36
|
+
should "verify the source" do
|
37
|
+
source = Stripe::Source.retrieve(FIXTURE[:id])
|
38
|
+
source = source.verify(:values => [1,2])
|
39
|
+
assert source.kind_of?(Stripe::Source)
|
60
40
|
end
|
61
41
|
end
|
62
|
-
|
63
|
-
should 'be verifiable' do
|
64
|
-
@mock.expects(:get).once.returns(make_response(make_source_ach_debit))
|
65
|
-
src = Stripe::Source.retrieve('src_test_ach_debit')
|
66
|
-
|
67
|
-
@mock.expects(:post).once
|
68
|
-
.with(
|
69
|
-
'https://api.stripe.com/v1/sources/src_test_ach_debit/verify',
|
70
|
-
nil,
|
71
|
-
'values[]=32&values[]=45'
|
72
|
-
)
|
73
|
-
.returns(make_response(make_source_ach_debit(
|
74
|
-
verification: {
|
75
|
-
attempts_remaining: 0,
|
76
|
-
status: 'succeeded',
|
77
|
-
}
|
78
|
-
)))
|
79
|
-
src.verify(values: [32, 45])
|
80
|
-
assert_equal 'succeeded', src.verification.status
|
81
|
-
end
|
82
42
|
end
|
83
43
|
end
|
@@ -0,0 +1,428 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
class StripeClientTest < Test::Unit::TestCase
|
5
|
+
context ".active_client" do
|
6
|
+
should "be .default_client outside of #request" do
|
7
|
+
assert_equal StripeClient.default_client, StripeClient.active_client
|
8
|
+
end
|
9
|
+
|
10
|
+
should "be active client inside of #request" do
|
11
|
+
client = StripeClient.new
|
12
|
+
client.request do
|
13
|
+
assert_equal client, StripeClient.active_client
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context ".default_client" do
|
19
|
+
should "be a StripeClient" do
|
20
|
+
assert_kind_of StripeClient, StripeClient.default_client
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context ".default_conn" do
|
25
|
+
should "be a Faraday::Connection" do
|
26
|
+
assert_kind_of Faraday::Connection, StripeClient.default_conn
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be a different connection on each thread" do
|
30
|
+
other_thread_conn = nil
|
31
|
+
thread = Thread.new do
|
32
|
+
other_thread_conn = StripeClient.default_conn
|
33
|
+
end
|
34
|
+
thread.join
|
35
|
+
refute_equal StripeClient.default_conn, other_thread_conn
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context ".should_retry?" do
|
40
|
+
setup do
|
41
|
+
Stripe.stubs(:max_network_retries).returns(2)
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'retry on timeout' do
|
45
|
+
assert StripeClient.should_retry?(Faraday::TimeoutError.new(""), 0)
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'retry on a failed connection' do
|
49
|
+
assert StripeClient.should_retry?(Faraday::ConnectionFailed.new(""), 0)
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'retry on a conflict' do
|
53
|
+
error = make_rate_limit_error
|
54
|
+
e = Faraday::ClientError.new(error[:error][:message], { status: 409 })
|
55
|
+
assert StripeClient.should_retry?(e, 0)
|
56
|
+
end
|
57
|
+
|
58
|
+
should 'not retry at maximum count' do
|
59
|
+
refute StripeClient.should_retry?(RuntimeError.new, Stripe.max_network_retries)
|
60
|
+
end
|
61
|
+
|
62
|
+
should 'not retry on a certificate validation error' do
|
63
|
+
refute StripeClient.should_retry?(Faraday::SSLError.new(""), 0)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context ".sleep_time" do
|
68
|
+
should "should grow exponentially" do
|
69
|
+
StripeClient.stubs(:rand).returns(1)
|
70
|
+
Stripe.stubs(:max_network_retry_delay).returns(999)
|
71
|
+
assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
|
72
|
+
assert_equal(Stripe.initial_network_retry_delay * 2, StripeClient.sleep_time(2))
|
73
|
+
assert_equal(Stripe.initial_network_retry_delay * 4, StripeClient.sleep_time(3))
|
74
|
+
assert_equal(Stripe.initial_network_retry_delay * 8, StripeClient.sleep_time(4))
|
75
|
+
end
|
76
|
+
|
77
|
+
should "enforce the max_network_retry_delay" do
|
78
|
+
StripeClient.stubs(:rand).returns(1)
|
79
|
+
Stripe.stubs(:initial_network_retry_delay).returns(1)
|
80
|
+
Stripe.stubs(:max_network_retry_delay).returns(2)
|
81
|
+
assert_equal(1, StripeClient.sleep_time(1))
|
82
|
+
assert_equal(2, StripeClient.sleep_time(2))
|
83
|
+
assert_equal(2, StripeClient.sleep_time(3))
|
84
|
+
assert_equal(2, StripeClient.sleep_time(4))
|
85
|
+
end
|
86
|
+
|
87
|
+
should "add some randomness" do
|
88
|
+
random_value = 0.8
|
89
|
+
StripeClient.stubs(:rand).returns(random_value)
|
90
|
+
Stripe.stubs(:initial_network_retry_delay).returns(1)
|
91
|
+
Stripe.stubs(:max_network_retry_delay).returns(8)
|
92
|
+
|
93
|
+
base_value = Stripe.initial_network_retry_delay * (0.5 * (1 + random_value))
|
94
|
+
|
95
|
+
# the initial value cannot be smaller than the base,
|
96
|
+
# so the randomness is ignored
|
97
|
+
assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
|
98
|
+
|
99
|
+
# after the first one, the randomness is applied
|
100
|
+
assert_equal(base_value * 2, StripeClient.sleep_time(2))
|
101
|
+
assert_equal(base_value * 4, StripeClient.sleep_time(3))
|
102
|
+
assert_equal(base_value * 8, StripeClient.sleep_time(4))
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "#initialize" do
|
107
|
+
should "set Stripe.default_conn" do
|
108
|
+
client = StripeClient.new
|
109
|
+
assert_equal StripeClient.default_conn, client.conn
|
110
|
+
end
|
111
|
+
|
112
|
+
should "set a different connection if one was specified" do
|
113
|
+
conn = Faraday.new
|
114
|
+
client = StripeClient.new(conn)
|
115
|
+
assert_equal conn, client.conn
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "#execute_request" do
|
120
|
+
context "headers" do
|
121
|
+
should "support literal headers" do
|
122
|
+
stub_request(:post, "#{Stripe.api_base}/v1/account").
|
123
|
+
with(headers: { "Stripe-Account" => "bar" }).
|
124
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
|
125
|
+
|
126
|
+
client = StripeClient.new
|
127
|
+
client.execute_request(:post, '/v1/account',
|
128
|
+
headers: { "Stripe-Account" => "bar" }
|
129
|
+
)
|
130
|
+
end
|
131
|
+
|
132
|
+
should "support RestClient-style header keys" do
|
133
|
+
stub_request(:post, "#{Stripe.api_base}/v1/account").
|
134
|
+
with(headers: { "Stripe-Account" => "bar" }).
|
135
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
|
136
|
+
|
137
|
+
client = StripeClient.new
|
138
|
+
client.execute_request(:post, '/v1/account',
|
139
|
+
headers: { :stripe_account => "bar" }
|
140
|
+
)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context "Stripe-Account header" do
|
145
|
+
should "use a globally set header" do
|
146
|
+
Stripe.stripe_account = 'acct_1234'
|
147
|
+
|
148
|
+
stub_request(:post, "#{Stripe.api_base}/v1/account").
|
149
|
+
with(headers: {"Stripe-Account" => Stripe.stripe_account}).
|
150
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
|
151
|
+
|
152
|
+
client = StripeClient.new
|
153
|
+
client.execute_request(:post, '/v1/account')
|
154
|
+
end
|
155
|
+
|
156
|
+
should "use a locally set header" do
|
157
|
+
stripe_account = "acct_0000"
|
158
|
+
stub_request(:post, "#{Stripe.api_base}/v1/account").
|
159
|
+
with(headers: {"Stripe-Account" => stripe_account}).
|
160
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
|
161
|
+
|
162
|
+
client = StripeClient.new
|
163
|
+
client.execute_request(:post, '/v1/account',
|
164
|
+
headers: { :stripe_account => stripe_account }
|
165
|
+
)
|
166
|
+
end
|
167
|
+
|
168
|
+
should "not send it otherwise" do
|
169
|
+
stub_request(:post, "#{Stripe.api_base}/v1/account").
|
170
|
+
with { |req|
|
171
|
+
req.headers["Stripe-Account"].nil?
|
172
|
+
}.to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
|
173
|
+
|
174
|
+
client = StripeClient.new
|
175
|
+
client.execute_request(:post, '/v1/account')
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
context "error handling" do
|
180
|
+
should "handle error response with empty body" do
|
181
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
182
|
+
to_return(body: '', status: 500)
|
183
|
+
|
184
|
+
client = StripeClient.new
|
185
|
+
e = assert_raises Stripe::APIError do
|
186
|
+
client.execute_request(:post, '/v1/charges')
|
187
|
+
end
|
188
|
+
|
189
|
+
assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
|
190
|
+
end
|
191
|
+
|
192
|
+
should "handle error response with non-object error value" do
|
193
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
194
|
+
to_return(body: JSON.generate({ error: "foo" }), status: 500)
|
195
|
+
|
196
|
+
client = StripeClient.new
|
197
|
+
e = assert_raises Stripe::APIError do
|
198
|
+
client.execute_request(:post, '/v1/charges')
|
199
|
+
end
|
200
|
+
|
201
|
+
assert_equal 'Invalid response object from API: "{\"error\":\"foo\"}" (HTTP response code was 500)', e.message
|
202
|
+
end
|
203
|
+
|
204
|
+
should "raise InvalidRequestError on 400" do
|
205
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
206
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 400)
|
207
|
+
client = StripeClient.new
|
208
|
+
begin
|
209
|
+
client.execute_request(:post, '/v1/charges')
|
210
|
+
rescue Stripe::InvalidRequestError => e
|
211
|
+
assert_equal(400, e.http_status)
|
212
|
+
assert_equal(true, !!e.http_body)
|
213
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
should "raise AuthenticationError on 401" do
|
218
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
219
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 401)
|
220
|
+
client = StripeClient.new
|
221
|
+
begin
|
222
|
+
client.execute_request(:post, '/v1/charges')
|
223
|
+
rescue Stripe::AuthenticationError => e
|
224
|
+
assert_equal(401, e.http_status)
|
225
|
+
assert_equal(true, !!e.http_body)
|
226
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
should "raise CardError on 402" do
|
231
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
232
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 402)
|
233
|
+
client = StripeClient.new
|
234
|
+
begin
|
235
|
+
client.execute_request(:post, '/v1/charges')
|
236
|
+
rescue Stripe::CardError => e
|
237
|
+
assert_equal(402, e.http_status)
|
238
|
+
assert_equal(true, !!e.http_body)
|
239
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
should "raise PermissionError on 403" do
|
244
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
245
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 403)
|
246
|
+
client = StripeClient.new
|
247
|
+
begin
|
248
|
+
client.execute_request(:post, '/v1/charges')
|
249
|
+
rescue Stripe::PermissionError => e
|
250
|
+
assert_equal(403, e.http_status)
|
251
|
+
assert_equal(true, !!e.http_body)
|
252
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
should "raise InvalidRequestError on 404" do
|
257
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
258
|
+
to_return(body: JSON.generate(make_missing_id_error), status: 404)
|
259
|
+
client = StripeClient.new
|
260
|
+
begin
|
261
|
+
client.execute_request(:post, '/v1/charges')
|
262
|
+
rescue Stripe::InvalidRequestError => e
|
263
|
+
assert_equal(404, e.http_status)
|
264
|
+
assert_equal(true, !!e.http_body)
|
265
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
should "raise RateLimitError on 429" do
|
270
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
271
|
+
to_return(body: JSON.generate(make_rate_limit_error), status: 429)
|
272
|
+
client = StripeClient.new
|
273
|
+
begin
|
274
|
+
client.execute_request(:post, '/v1/charges')
|
275
|
+
rescue Stripe::RateLimitError => e
|
276
|
+
assert_equal(429, e.http_status)
|
277
|
+
assert_equal(true, !!e.http_body)
|
278
|
+
assert_equal(true, e.json_body.kind_of?(Hash))
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
context "idempotency keys" do
|
284
|
+
setup do
|
285
|
+
Stripe.stubs(:max_network_retries).returns(2)
|
286
|
+
end
|
287
|
+
|
288
|
+
should 'not add an idempotency key to GET requests' do
|
289
|
+
SecureRandom.expects(:uuid).times(0)
|
290
|
+
stub_request(:get, "#{Stripe.api_base}/v1/charges/#{API_FIXTURES.fetch(:charge)[:id]}").
|
291
|
+
with { |req|
|
292
|
+
req.headers['Idempotency-Key'].nil?
|
293
|
+
}.to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
294
|
+
client = StripeClient.new
|
295
|
+
client.execute_request(:get, "/v1/charges/#{API_FIXTURES.fetch(:charge)[:id]}")
|
296
|
+
end
|
297
|
+
|
298
|
+
should 'ensure there is always an idempotency_key on POST requests' do
|
299
|
+
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
|
300
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
301
|
+
with(headers: {"Idempotency-Key" => "random_key"}).
|
302
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
303
|
+
client = StripeClient.new
|
304
|
+
client.execute_request(:post, "/v1/charges")
|
305
|
+
end
|
306
|
+
|
307
|
+
should 'ensure there is always an idempotency_key on DELETE requests' do
|
308
|
+
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
|
309
|
+
stub_request(:delete, "#{Stripe.api_base}/v1/charges/#{API_FIXTURES.fetch(:charge)[:id]}").
|
310
|
+
with(headers: {"Idempotency-Key" => "random_key"}).
|
311
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
312
|
+
client = StripeClient.new
|
313
|
+
client.execute_request(:delete, "/v1/charges/#{API_FIXTURES.fetch(:charge)[:id]}")
|
314
|
+
end
|
315
|
+
|
316
|
+
should 'not override a provided idempotency_key' do
|
317
|
+
# Note that this expectation looks like `:idempotency_key` instead of
|
318
|
+
# the header `Idempotency-Key` because it's user provided as seen
|
319
|
+
# below. The ones injected by the library itself look like headers
|
320
|
+
# (`Idempotency-Key`), but rest-client does allow this symbol
|
321
|
+
# formatting and will properly override the system generated one as
|
322
|
+
# expected.
|
323
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
324
|
+
with(headers: {"Idempotency-Key" => "provided_key"}).
|
325
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
326
|
+
|
327
|
+
client = StripeClient.new
|
328
|
+
client.execute_request(:post, "/v1/charges",
|
329
|
+
headers: {:idempotency_key => 'provided_key'})
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
context "retry logic" do
|
334
|
+
setup do
|
335
|
+
Stripe.stubs(:max_network_retries).returns(2)
|
336
|
+
end
|
337
|
+
|
338
|
+
should 'retry failed requests and raise if error persists' do
|
339
|
+
StripeClient.expects(:sleep_time).at_least_once.returns(0)
|
340
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
341
|
+
to_raise(Errno::ECONNREFUSED.new)
|
342
|
+
|
343
|
+
client = StripeClient.new
|
344
|
+
err = assert_raises Stripe::APIConnectionError do
|
345
|
+
client.execute_request(:post, '/v1/charges')
|
346
|
+
end
|
347
|
+
assert_match(/Request was retried 2 times/, err.message)
|
348
|
+
end
|
349
|
+
|
350
|
+
should 'retry failed requests and return successful response' do
|
351
|
+
StripeClient.expects(:sleep_time).at_least_once.returns(0)
|
352
|
+
|
353
|
+
i = 0
|
354
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
355
|
+
to_return { |_|
|
356
|
+
if i < 2
|
357
|
+
i += 1
|
358
|
+
raise Errno::ECONNREFUSED.new
|
359
|
+
else
|
360
|
+
{ body: JSON.generate({"id" => "myid"}) }
|
361
|
+
end
|
362
|
+
}
|
363
|
+
|
364
|
+
client = StripeClient.new
|
365
|
+
client.execute_request(:post, '/v1/charges')
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
context "#request" do
|
371
|
+
should "return a result and response object" do
|
372
|
+
stub_request(:post, "#{Stripe.api_base}/v1/charges").
|
373
|
+
to_return(body: JSON.generate(API_FIXTURES.fetch(:charge)))
|
374
|
+
|
375
|
+
client = StripeClient.new
|
376
|
+
charge, resp = client.request { Charge.create }
|
377
|
+
|
378
|
+
assert charge.is_a?(Charge)
|
379
|
+
assert resp.is_a?(StripeResponse)
|
380
|
+
assert_equal 200, resp.http_status
|
381
|
+
end
|
382
|
+
|
383
|
+
should "return the value of given block" do
|
384
|
+
client = StripeClient.new
|
385
|
+
ret, _ = client.request { 7 }
|
386
|
+
assert_equal 7, ret
|
387
|
+
end
|
388
|
+
|
389
|
+
should "reset local thread state after a call" do
|
390
|
+
begin
|
391
|
+
Thread.current[:stripe_client] = :stripe_client
|
392
|
+
|
393
|
+
client = StripeClient.new
|
394
|
+
client.request {}
|
395
|
+
|
396
|
+
assert_equal :stripe_client, Thread.current[:stripe_client]
|
397
|
+
ensure
|
398
|
+
Thread.current[:stripe_client] = nil
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
class SystemProfilerTest < Test::Unit::TestCase
|
405
|
+
context "#get_uname" do
|
406
|
+
should "run without failure" do
|
407
|
+
# Don't actually check the result because we try a variety of different
|
408
|
+
# strategies that will have different results depending on where this
|
409
|
+
# test and running. We're mostly making sure that no exception is thrown.
|
410
|
+
_ = StripeClient::SystemProfiler.get_uname
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
context "#get_uname_from_system" do
|
415
|
+
should "run without failure" do
|
416
|
+
# as above, just verify that an exception is not thrown
|
417
|
+
_ = StripeClient::SystemProfiler.get_uname_from_system
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
context "#get_uname_from_system_ver" do
|
422
|
+
should "run without failure" do
|
423
|
+
# as above, just verify that an exception is not thrown
|
424
|
+
_ = StripeClient::SystemProfiler.get_uname_from_system_ver
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
end
|