stripe 5.1.0 → 5.55.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +241 -17
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +9 -5
  5. data/Makefile +7 -0
  6. data/README.md +105 -37
  7. data/VERSION +1 -1
  8. data/lib/stripe/api_operations/create.rb +1 -1
  9. data/lib/stripe/api_operations/delete.rb +7 -3
  10. data/lib/stripe/api_operations/list.rb +1 -6
  11. data/lib/stripe/api_operations/nested_resource.rb +29 -26
  12. data/lib/stripe/api_operations/request.rb +82 -6
  13. data/lib/stripe/api_operations/save.rb +7 -4
  14. data/lib/stripe/api_operations/search.rb +19 -0
  15. data/lib/stripe/api_resource.rb +13 -19
  16. data/lib/stripe/api_resource_test_helpers.rb +47 -0
  17. data/lib/stripe/connection_manager.rb +85 -26
  18. data/lib/stripe/error_object.rb +2 -3
  19. data/lib/stripe/errors.rb +9 -1
  20. data/lib/stripe/instrumentation.rb +84 -0
  21. data/lib/stripe/list_object.rb +32 -4
  22. data/lib/stripe/oauth.rb +8 -6
  23. data/lib/stripe/object_types.rb +26 -1
  24. data/lib/stripe/resources/account.rb +5 -9
  25. data/lib/stripe/resources/account_link.rb +1 -0
  26. data/lib/stripe/resources/apple_pay_domain.rb +1 -0
  27. data/lib/stripe/resources/application_fee.rb +1 -0
  28. data/lib/stripe/resources/application_fee_refund.rb +2 -1
  29. data/lib/stripe/resources/balance.rb +1 -0
  30. data/lib/stripe/resources/balance_transaction.rb +1 -0
  31. data/lib/stripe/resources/bank_account.rb +2 -1
  32. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  33. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  34. data/lib/stripe/resources/bitcoin_receiver.rb +1 -0
  35. data/lib/stripe/resources/bitcoin_transaction.rb +3 -2
  36. data/lib/stripe/resources/capability.rb +2 -1
  37. data/lib/stripe/resources/card.rb +1 -0
  38. data/lib/stripe/resources/cash_balance.rb +22 -0
  39. data/lib/stripe/resources/charge.rb +10 -0
  40. data/lib/stripe/resources/checkout/session.rb +16 -0
  41. data/lib/stripe/resources/country_spec.rb +1 -0
  42. data/lib/stripe/resources/coupon.rb +1 -0
  43. data/lib/stripe/resources/credit_note.rb +11 -0
  44. data/lib/stripe/resources/credit_note_line_item.rb +8 -0
  45. data/lib/stripe/resources/customer.rb +41 -2
  46. data/lib/stripe/resources/customer_balance_transaction.rb +3 -2
  47. data/lib/stripe/resources/discount.rb +1 -0
  48. data/lib/stripe/resources/dispute.rb +1 -0
  49. data/lib/stripe/resources/ephemeral_key.rb +1 -0
  50. data/lib/stripe/resources/event.rb +1 -0
  51. data/lib/stripe/resources/exchange_rate.rb +1 -0
  52. data/lib/stripe/resources/file.rb +3 -1
  53. data/lib/stripe/resources/file_link.rb +1 -0
  54. data/lib/stripe/resources/financial_connections/account.rb +31 -0
  55. data/lib/stripe/resources/financial_connections/account_owner.rb +10 -0
  56. data/lib/stripe/resources/financial_connections/account_ownership.rb +10 -0
  57. data/lib/stripe/resources/financial_connections/session.rb +12 -0
  58. data/lib/stripe/resources/funding_instructions.rb +16 -0
  59. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  60. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  61. data/lib/stripe/resources/invoice.rb +12 -2
  62. data/lib/stripe/resources/invoice_item.rb +1 -0
  63. data/lib/stripe/resources/invoice_line_item.rb +1 -0
  64. data/lib/stripe/resources/issuing/authorization.rb +1 -0
  65. data/lib/stripe/resources/issuing/card.rb +1 -0
  66. data/lib/stripe/resources/issuing/card_details.rb +2 -1
  67. data/lib/stripe/resources/issuing/cardholder.rb +1 -0
  68. data/lib/stripe/resources/issuing/dispute.rb +12 -0
  69. data/lib/stripe/resources/issuing/transaction.rb +1 -0
  70. data/lib/stripe/resources/line_item.rb +8 -0
  71. data/lib/stripe/resources/login_link.rb +1 -0
  72. data/lib/stripe/resources/mandate.rb +8 -0
  73. data/lib/stripe/resources/order.rb +1 -0
  74. data/lib/stripe/resources/order_return.rb +1 -0
  75. data/lib/stripe/resources/payment_intent.rb +40 -0
  76. data/lib/stripe/resources/payment_link.rb +23 -0
  77. data/lib/stripe/resources/payment_method.rb +1 -0
  78. data/lib/stripe/resources/payout.rb +11 -0
  79. data/lib/stripe/resources/person.rb +1 -0
  80. data/lib/stripe/resources/plan.rb +1 -0
  81. data/lib/stripe/resources/price.rb +21 -0
  82. data/lib/stripe/resources/product.rb +10 -0
  83. data/lib/stripe/resources/promotion_code.rb +12 -0
  84. data/lib/stripe/resources/quote.rb +105 -0
  85. data/lib/stripe/resources/radar/early_fraud_warning.rb +1 -0
  86. data/lib/stripe/resources/radar/value_list.rb +1 -0
  87. data/lib/stripe/resources/radar/value_list_item.rb +1 -0
  88. data/lib/stripe/resources/recipient.rb +1 -0
  89. data/lib/stripe/resources/refund.rb +31 -0
  90. data/lib/stripe/resources/reporting/report_run.rb +1 -0
  91. data/lib/stripe/resources/reporting/report_type.rb +1 -0
  92. data/lib/stripe/resources/reversal.rb +3 -2
  93. data/lib/stripe/resources/review.rb +1 -0
  94. data/lib/stripe/resources/setup_attempt.rb +10 -0
  95. data/lib/stripe/resources/setup_intent.rb +11 -0
  96. data/lib/stripe/resources/shipping_rate.rb +12 -0
  97. data/lib/stripe/resources/sigma/scheduled_query_run.rb +1 -0
  98. data/lib/stripe/resources/sku.rb +1 -0
  99. data/lib/stripe/resources/source.rb +10 -3
  100. data/lib/stripe/resources/source_transaction.rb +1 -0
  101. data/lib/stripe/resources/subscription.rb +10 -0
  102. data/lib/stripe/resources/subscription_item.rb +7 -1
  103. data/lib/stripe/resources/subscription_schedule.rb +1 -0
  104. data/lib/stripe/resources/tax_code.rb +10 -0
  105. data/lib/stripe/resources/tax_id.rb +1 -0
  106. data/lib/stripe/resources/tax_rate.rb +1 -0
  107. data/lib/stripe/resources/terminal/configuration.rb +15 -0
  108. data/lib/stripe/resources/terminal/connection_token.rb +1 -0
  109. data/lib/stripe/resources/terminal/location.rb +1 -0
  110. data/lib/stripe/resources/terminal/reader.rb +61 -0
  111. data/lib/stripe/resources/test_helpers/test_clock.rb +25 -0
  112. data/lib/stripe/resources/three_d_secure.rb +1 -0
  113. data/lib/stripe/resources/token.rb +1 -0
  114. data/lib/stripe/resources/topup.rb +1 -0
  115. data/lib/stripe/resources/transfer.rb +1 -0
  116. data/lib/stripe/resources/usage_record.rb +1 -0
  117. data/lib/stripe/resources/usage_record_summary.rb +1 -0
  118. data/lib/stripe/resources/webhook_endpoint.rb +1 -0
  119. data/lib/stripe/resources.rb +23 -0
  120. data/lib/stripe/search_result_object.rb +86 -0
  121. data/lib/stripe/stripe_client.rb +412 -146
  122. data/lib/stripe/stripe_configuration.rb +194 -0
  123. data/lib/stripe/stripe_object.rb +26 -2
  124. data/lib/stripe/stripe_response.rb +80 -52
  125. data/lib/stripe/util.rb +74 -7
  126. data/lib/stripe/version.rb +1 -1
  127. data/lib/stripe/webhook.rb +38 -7
  128. data/lib/stripe.rb +39 -168
  129. data/stripe.gemspec +12 -5
  130. metadata +36 -186
  131. data/.editorconfig +0 -10
  132. data/.gitattributes +0 -4
  133. data/.github/ISSUE_TEMPLATE.md +0 -5
  134. data/.gitignore +0 -8
  135. data/.rubocop.yml +0 -56
  136. data/.rubocop_todo.yml +0 -39
  137. data/.travis.yml +0 -39
  138. data/.vscode/extensions.json +0 -7
  139. data/.vscode/settings.json +0 -8
  140. data/test/api_stub_helpers.rb +0 -1
  141. data/test/openapi/README.md +0 -9
  142. data/test/stripe/account_link_test.rb +0 -18
  143. data/test/stripe/account_test.rb +0 -412
  144. data/test/stripe/alipay_account_test.rb +0 -37
  145. data/test/stripe/api_operations_test.rb +0 -80
  146. data/test/stripe/api_resource_test.rb +0 -613
  147. data/test/stripe/apple_pay_domain_test.rb +0 -46
  148. data/test/stripe/application_fee_refund_test.rb +0 -37
  149. data/test/stripe/application_fee_test.rb +0 -58
  150. data/test/stripe/balance_test.rb +0 -13
  151. data/test/stripe/balance_transaction_test.rb +0 -20
  152. data/test/stripe/bank_account_test.rb +0 -36
  153. data/test/stripe/capability_test.rb +0 -45
  154. data/test/stripe/charge_test.rb +0 -64
  155. data/test/stripe/checkout/session_test.rb +0 -41
  156. data/test/stripe/connection_manager_test.rb +0 -138
  157. data/test/stripe/country_spec_test.rb +0 -20
  158. data/test/stripe/coupon_test.rb +0 -61
  159. data/test/stripe/credit_note_test.rb +0 -61
  160. data/test/stripe/customer_balance_transaction_test.rb +0 -37
  161. data/test/stripe/customer_card_test.rb +0 -42
  162. data/test/stripe/customer_test.rb +0 -226
  163. data/test/stripe/dispute_test.rb +0 -51
  164. data/test/stripe/ephemeral_key_test.rb +0 -93
  165. data/test/stripe/errors_test.rb +0 -41
  166. data/test/stripe/exchange_rate_test.rb +0 -20
  167. data/test/stripe/file_link_test.rb +0 -41
  168. data/test/stripe/file_test.rb +0 -87
  169. data/test/stripe/invoice_item_test.rb +0 -66
  170. data/test/stripe/invoice_line_item_test.rb +0 -8
  171. data/test/stripe/invoice_test.rb +0 -229
  172. data/test/stripe/issuing/authorization_test.rb +0 -72
  173. data/test/stripe/issuing/card_test.rb +0 -62
  174. data/test/stripe/issuing/cardholder_test.rb +0 -53
  175. data/test/stripe/issuing/dispute_test.rb +0 -45
  176. data/test/stripe/issuing/transaction_test.rb +0 -48
  177. data/test/stripe/list_object_test.rb +0 -140
  178. data/test/stripe/login_link_test.rb +0 -37
  179. data/test/stripe/multipart_encoder_test.rb +0 -130
  180. data/test/stripe/oauth_test.rb +0 -88
  181. data/test/stripe/order_return_test.rb +0 -21
  182. data/test/stripe/order_test.rb +0 -82
  183. data/test/stripe/payment_intent_test.rb +0 -107
  184. data/test/stripe/payment_method_test.rb +0 -84
  185. data/test/stripe/payout_test.rb +0 -57
  186. data/test/stripe/person_test.rb +0 -46
  187. data/test/stripe/plan_test.rb +0 -98
  188. data/test/stripe/product_test.rb +0 -59
  189. data/test/stripe/radar/early_fraud_warning_test.rb +0 -22
  190. data/test/stripe/radar/value_list_item_test.rb +0 -48
  191. data/test/stripe/radar/value_list_test.rb +0 -61
  192. data/test/stripe/recipient_test.rb +0 -62
  193. data/test/stripe/refund_test.rb +0 -39
  194. data/test/stripe/reporting/report_run_test.rb +0 -33
  195. data/test/stripe/reporting/report_type_test.rb +0 -22
  196. data/test/stripe/reversal_test.rb +0 -43
  197. data/test/stripe/review_test.rb +0 -27
  198. data/test/stripe/setup_intent_test.rb +0 -84
  199. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  200. data/test/stripe/sku_test.rb +0 -60
  201. data/test/stripe/source_test.rb +0 -81
  202. data/test/stripe/source_transaction_test.rb +0 -19
  203. data/test/stripe/stripe_client_test.rb +0 -1039
  204. data/test/stripe/stripe_object_test.rb +0 -497
  205. data/test/stripe/stripe_response_test.rb +0 -95
  206. data/test/stripe/subscription_item_test.rb +0 -75
  207. data/test/stripe/subscription_schedule_test.rb +0 -82
  208. data/test/stripe/subscription_test.rb +0 -80
  209. data/test/stripe/tax_id_test.rb +0 -31
  210. data/test/stripe/tax_rate_test.rb +0 -43
  211. data/test/stripe/terminal/connection_token_test.rb +0 -16
  212. data/test/stripe/terminal/location_test.rb +0 -68
  213. data/test/stripe/terminal/reader_test.rb +0 -62
  214. data/test/stripe/three_d_secure_test.rb +0 -23
  215. data/test/stripe/topup_test.rb +0 -62
  216. data/test/stripe/transfer_test.rb +0 -88
  217. data/test/stripe/usage_record_summary_test.rb +0 -19
  218. data/test/stripe/util_test.rb +0 -402
  219. data/test/stripe/webhook_endpoint_test.rb +0 -59
  220. data/test/stripe/webhook_test.rb +0 -96
  221. data/test/stripe_mock.rb +0 -78
  222. data/test/stripe_test.rb +0 -50
  223. data/test/test_data.rb +0 -61
  224. data/test/test_helper.rb +0 -76
@@ -1,1039 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../test_helper", __dir__)
4
-
5
- module Stripe
6
- class StripeClientTest < Test::Unit::TestCase
7
- context ".active_client" do
8
- should "be .default_client outside of #request" do
9
- assert_equal StripeClient.default_client, StripeClient.active_client
10
- end
11
-
12
- should "be active client inside of #request" do
13
- client = StripeClient.new
14
- client.request do
15
- assert_equal client, StripeClient.active_client
16
- end
17
- end
18
- end
19
-
20
- context ".clear_all_connection_managers" do
21
- should "clear connection managers across all threads" do
22
- stub_request(:post, "#{Stripe.api_base}/path")
23
- .to_return(body: JSON.generate(object: "account"))
24
-
25
- num_threads = 3
26
-
27
- # Poorly named class -- note this is actually a concurrent queue.
28
- recv_queue = Queue.new
29
- send_queue = Queue.new
30
-
31
- threads = num_threads.times.map do |_|
32
- Thread.start do
33
- # Causes a connection manager to be created on this thread and a
34
- # connection within that manager to be created for API access.
35
- manager = StripeClient.default_connection_manager
36
- manager.execute_request(:post, "#{Stripe.api_base}/path")
37
-
38
- # Signal to the main thread we're ready.
39
- recv_queue << true
40
-
41
- # Wait for the main thread to signal continue.
42
- send_queue.pop
43
-
44
- # This check isn't great, but it's otherwise difficult to tell that
45
- # anything happened with just the public-facing API.
46
- assert_equal({}, manager.instance_variable_get(:@active_connections))
47
- end
48
- end
49
-
50
- # Wait for threads to start up.
51
- threads.each { recv_queue.pop }
52
-
53
- # Do the clear (the method we're actually trying to test).
54
- StripeClient.clear_all_connection_managers
55
-
56
- # Tell threads to run their check.
57
- threads.each { send_queue << true }
58
-
59
- # And finally, give all threads time to perform their check.
60
- threads.each(&:join)
61
- end
62
- end
63
-
64
- context ".default_client" do
65
- should "be a StripeClient" do
66
- assert_kind_of StripeClient, StripeClient.default_client
67
- end
68
-
69
- should "be a different client on each thread" do
70
- other_thread_client = nil
71
- thread = Thread.new do
72
- other_thread_client = StripeClient.default_client
73
- end
74
- thread.join
75
- refute_equal StripeClient.default_client, other_thread_client
76
- end
77
- end
78
-
79
- context ".default_connection_manager" do
80
- should "be a ConnectionManager" do
81
- assert_kind_of ConnectionManager,
82
- StripeClient.default_connection_manager
83
- end
84
-
85
- should "be a different connection on each thread" do
86
- other_thread_manager = nil
87
- thread = Thread.new do
88
- other_thread_manager = StripeClient.default_connection_manager
89
- end
90
- thread.join
91
- refute_equal StripeClient.default_connection_manager, other_thread_manager
92
- end
93
- end
94
-
95
- context ".should_retry?" do
96
- setup do
97
- Stripe.stubs(:max_network_retries).returns(2)
98
- end
99
-
100
- should "retry on Errno::ECONNREFUSED" do
101
- assert StripeClient.should_retry?(Errno::ECONNREFUSED.new,
102
- method: :post, num_retries: 0)
103
- end
104
-
105
- should "retry on Net::OpenTimeout" do
106
- assert StripeClient.should_retry?(Net::OpenTimeout.new,
107
- method: :post, num_retries: 0)
108
- end
109
-
110
- should "retry on Net::ReadTimeout" do
111
- assert StripeClient.should_retry?(Net::ReadTimeout.new,
112
- method: :post, num_retries: 0)
113
- end
114
-
115
- should "retry on SocketError" do
116
- assert StripeClient.should_retry?(SocketError.new,
117
- method: :post, num_retries: 0)
118
- end
119
-
120
- should "retry on a 409 Conflict" do
121
- assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 409),
122
- method: :post, num_retries: 0)
123
- end
124
-
125
- should "retry on a 429 Too Many Requests when lock timeout" do
126
- assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 429,
127
- code: "lock_timeout"),
128
- method: :post, num_retries: 0)
129
- end
130
-
131
- should "retry on a 500 Internal Server Error when non-POST" do
132
- assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
133
- method: :get, num_retries: 0)
134
- end
135
-
136
- should "retry on a 503 Service Unavailable" do
137
- assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 503),
138
- method: :post, num_retries: 0)
139
- end
140
-
141
- should "not retry at maximum count" do
142
- refute StripeClient.should_retry?(RuntimeError.new,
143
- method: :post, num_retries: Stripe.max_network_retries)
144
- end
145
-
146
- should "not retry on a certificate validation error" do
147
- refute StripeClient.should_retry?(OpenSSL::SSL::SSLError.new,
148
- method: :post, num_retries: 0)
149
- end
150
-
151
- should "not retry on a 429 Too Many Requests when not lock timeout" do
152
- refute StripeClient.should_retry?(Stripe::StripeError.new(http_status: 429,
153
- code: "rate_limited"),
154
- method: :post, num_retries: 0)
155
- end
156
-
157
- should "not retry on a 500 Internal Server Error when POST" do
158
- refute StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
159
- method: :post, num_retries: 0)
160
- end
161
- end
162
-
163
- context ".sleep_time" do
164
- should "should grow exponentially" do
165
- StripeClient.stubs(:rand).returns(1)
166
- Stripe.stubs(:max_network_retry_delay).returns(999)
167
- assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
168
- assert_equal(Stripe.initial_network_retry_delay * 2, StripeClient.sleep_time(2))
169
- assert_equal(Stripe.initial_network_retry_delay * 4, StripeClient.sleep_time(3))
170
- assert_equal(Stripe.initial_network_retry_delay * 8, StripeClient.sleep_time(4))
171
- end
172
-
173
- should "enforce the max_network_retry_delay" do
174
- StripeClient.stubs(:rand).returns(1)
175
- Stripe.stubs(:initial_network_retry_delay).returns(1)
176
- Stripe.stubs(:max_network_retry_delay).returns(2)
177
- assert_equal(1, StripeClient.sleep_time(1))
178
- assert_equal(2, StripeClient.sleep_time(2))
179
- assert_equal(2, StripeClient.sleep_time(3))
180
- assert_equal(2, StripeClient.sleep_time(4))
181
- end
182
-
183
- should "add some randomness" do
184
- random_value = 0.8
185
- StripeClient.stubs(:rand).returns(random_value)
186
- Stripe.stubs(:initial_network_retry_delay).returns(1)
187
- Stripe.stubs(:max_network_retry_delay).returns(8)
188
-
189
- base_value = Stripe.initial_network_retry_delay * (0.5 * (1 + random_value))
190
-
191
- # the initial value cannot be smaller than the base,
192
- # so the randomness is ignored
193
- assert_equal(Stripe.initial_network_retry_delay, StripeClient.sleep_time(1))
194
-
195
- # after the first one, the randomness is applied
196
- assert_equal(base_value * 2, StripeClient.sleep_time(2))
197
- assert_equal(base_value * 4, StripeClient.sleep_time(3))
198
- assert_equal(base_value * 8, StripeClient.sleep_time(4))
199
- end
200
- end
201
-
202
- context "#initialize" do
203
- should "set Stripe.default_connection_manager" do
204
- client = StripeClient.new
205
- assert_equal StripeClient.default_connection_manager,
206
- client.connection_manager
207
- end
208
-
209
- should "set a different connection if one was specified" do
210
- connection_manager = ConnectionManager.new
211
- client = StripeClient.new(connection_manager)
212
- assert_equal connection_manager, client.connection_manager
213
- end
214
- end
215
-
216
- context "#execute_request" do
217
- context "headers" do
218
- should "support literal headers" do
219
- stub_request(:post, "#{Stripe.api_base}/v1/account")
220
- .with(headers: { "Stripe-Account" => "bar" })
221
- .to_return(body: JSON.generate(object: "account"))
222
-
223
- client = StripeClient.new
224
- client.execute_request(:post, "/v1/account",
225
- headers: { "Stripe-Account" => "bar" })
226
- end
227
-
228
- should "support RestClient-style header keys" do
229
- stub_request(:post, "#{Stripe.api_base}/v1/account")
230
- .with(headers: { "Stripe-Account" => "bar" })
231
- .to_return(body: JSON.generate(object: "account"))
232
-
233
- client = StripeClient.new
234
- client.execute_request(:post, "/v1/account",
235
- headers: { stripe_account: "bar" })
236
- end
237
- end
238
-
239
- context "logging" do
240
- setup do
241
- # Freeze time for the purposes of the `elapsed` parameter that we
242
- # emit for responses. I didn't want to bring in a new dependency for
243
- # this, but Mocha's `anything` parameter can't match inside of a hash
244
- # and is therefore not useful for this purpose. If we switch over to
245
- # rspec-mocks at some point, we can probably remove Timecop from the
246
- # project.
247
- Timecop.freeze(Time.local(1990))
248
- end
249
-
250
- teardown do
251
- Timecop.return
252
- end
253
-
254
- should "produce appropriate logging" do
255
- body = JSON.generate(object: "account")
256
-
257
- Util.expects(:log_info).with("Request to Stripe API",
258
- account: "acct_123",
259
- api_version: "2010-11-12",
260
- idempotency_key: "abc",
261
- method: :post,
262
- num_retries: 0,
263
- path: "/v1/account")
264
- Util.expects(:log_debug).with("Request details",
265
- body: "",
266
- idempotency_key: "abc",
267
- query: nil)
268
-
269
- Util.expects(:log_info).with("Response from Stripe API",
270
- account: "acct_123",
271
- api_version: "2010-11-12",
272
- elapsed: 0.0,
273
- idempotency_key: "abc",
274
- method: :post,
275
- path: "/v1/account",
276
- request_id: "req_123",
277
- status: 200)
278
- Util.expects(:log_debug).with("Response details",
279
- body: body,
280
- idempotency_key: "abc",
281
- request_id: "req_123")
282
- Util.expects(:log_debug).with("Dashboard link for request",
283
- idempotency_key: "abc",
284
- request_id: "req_123",
285
- url: Util.request_id_dashboard_url("req_123", Stripe.api_key))
286
-
287
- stub_request(:post, "#{Stripe.api_base}/v1/account")
288
- .to_return(
289
- body: body,
290
- headers: {
291
- "Idempotency-Key" => "abc",
292
- "Request-Id" => "req_123",
293
- "Stripe-Account" => "acct_123",
294
- "Stripe-Version" => "2010-11-12",
295
- }
296
- )
297
-
298
- client = StripeClient.new
299
- client.execute_request(:post, "/v1/account",
300
- headers: {
301
- "Idempotency-Key" => "abc",
302
- "Stripe-Account" => "acct_123",
303
- "Stripe-Version" => "2010-11-12",
304
- })
305
- end
306
-
307
- should "produce logging on API error" do
308
- Util.expects(:log_info).with("Request to Stripe API",
309
- account: nil,
310
- api_version: nil,
311
- idempotency_key: nil,
312
- method: :post,
313
- num_retries: 0,
314
- path: "/v1/account")
315
- Util.expects(:log_info).with("Response from Stripe API",
316
- account: nil,
317
- api_version: nil,
318
- elapsed: 0.0,
319
- idempotency_key: nil,
320
- method: :post,
321
- path: "/v1/account",
322
- request_id: nil,
323
- status: 500)
324
-
325
- error = {
326
- code: "code",
327
- message: "message",
328
- param: "param",
329
- type: "type",
330
- }
331
- Util.expects(:log_error).with("Stripe API error",
332
- status: 500,
333
- error_code: error[:code],
334
- error_message: error[:message],
335
- error_param: error[:param],
336
- error_type: error[:type],
337
- idempotency_key: nil,
338
- request_id: nil)
339
-
340
- stub_request(:post, "#{Stripe.api_base}/v1/account")
341
- .to_return(
342
- body: JSON.generate(error: error),
343
- status: 500
344
- )
345
-
346
- client = StripeClient.new
347
- assert_raises Stripe::APIError do
348
- client.execute_request(:post, "/v1/account")
349
- end
350
- end
351
-
352
- should "produce logging on OAuth error" do
353
- Util.expects(:log_info).with("Request to Stripe API",
354
- account: nil,
355
- api_version: nil,
356
- idempotency_key: nil,
357
- method: :post,
358
- num_retries: 0,
359
- path: "/oauth/token")
360
- Util.expects(:log_info).with("Response from Stripe API",
361
- account: nil,
362
- api_version: nil,
363
- elapsed: 0.0,
364
- idempotency_key: nil,
365
- method: :post,
366
- path: "/oauth/token",
367
- request_id: nil,
368
- status: 400)
369
-
370
- Util.expects(:log_error).with("Stripe OAuth error",
371
- status: 400,
372
- error_code: "invalid_request",
373
- error_description: "No grant type specified",
374
- idempotency_key: nil,
375
- request_id: nil)
376
-
377
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
378
- .to_return(body: JSON.generate(error: "invalid_request",
379
- error_description: "No grant type specified"), status: 400)
380
-
381
- client = StripeClient.new
382
- opts = { api_base: Stripe.connect_base }
383
- assert_raises Stripe::OAuth::InvalidRequestError do
384
- client.execute_request(:post, "/oauth/token", opts)
385
- end
386
- end
387
- end
388
-
389
- context "Stripe-Account header" do
390
- should "use a globally set header" do
391
- begin
392
- old = Stripe.stripe_account
393
- Stripe.stripe_account = "acct_1234"
394
-
395
- stub_request(:post, "#{Stripe.api_base}/v1/account")
396
- .with(headers: { "Stripe-Account" => Stripe.stripe_account })
397
- .to_return(body: JSON.generate(object: "account"))
398
-
399
- client = StripeClient.new
400
- client.execute_request(:post, "/v1/account")
401
- ensure
402
- Stripe.stripe_account = old
403
- end
404
- end
405
-
406
- should "use a locally set header" do
407
- stripe_account = "acct_0000"
408
- stub_request(:post, "#{Stripe.api_base}/v1/account")
409
- .with(headers: { "Stripe-Account" => stripe_account })
410
- .to_return(body: JSON.generate(object: "account"))
411
-
412
- client = StripeClient.new
413
- client.execute_request(:post, "/v1/account",
414
- headers: { stripe_account: stripe_account })
415
- end
416
-
417
- should "not send it otherwise" do
418
- stub_request(:post, "#{Stripe.api_base}/v1/account")
419
- .with do |req|
420
- req.headers["Stripe-Account"].nil?
421
- end.to_return(body: JSON.generate(object: "account"))
422
-
423
- client = StripeClient.new
424
- client.execute_request(:post, "/v1/account")
425
- end
426
- end
427
-
428
- context "app_info" do
429
- should "send app_info if set" do
430
- begin
431
- old = Stripe.app_info
432
- Stripe.set_app_info(
433
- "MyAwesomePlugin",
434
- partner_id: "partner_1234",
435
- url: "https://myawesomeplugin.info",
436
- version: "1.2.34"
437
- )
438
-
439
- stub_request(:post, "#{Stripe.api_base}/v1/account")
440
- .with do |req|
441
- assert_equal \
442
- "Stripe/v1 RubyBindings/#{Stripe::VERSION} " \
443
- "MyAwesomePlugin/1.2.34 (https://myawesomeplugin.info)",
444
- req.headers["User-Agent"]
445
-
446
- data = JSON.parse(req.headers["X-Stripe-Client-User-Agent"],
447
- symbolize_names: true)
448
-
449
- assert_equal({
450
- name: "MyAwesomePlugin",
451
- partner_id: "partner_1234",
452
- url: "https://myawesomeplugin.info",
453
- version: "1.2.34",
454
- }, data[:application])
455
-
456
- true
457
- end.to_return(body: JSON.generate(object: "account"))
458
-
459
- client = StripeClient.new
460
- client.execute_request(:post, "/v1/account")
461
- ensure
462
- Stripe.app_info = old
463
- end
464
- end
465
- end
466
-
467
- context "error handling" do
468
- should "handle error response with empty body" do
469
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
470
- .to_return(body: "", status: 500)
471
-
472
- client = StripeClient.new
473
- e = assert_raises Stripe::APIError do
474
- client.execute_request(:post, "/v1/charges")
475
- end
476
-
477
- assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
478
- end
479
-
480
- should "handle success response with empty body" do
481
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
482
- .to_return(body: "", status: 200)
483
-
484
- client = StripeClient.new
485
- e = assert_raises Stripe::APIError do
486
- client.execute_request(:post, "/v1/charges")
487
- end
488
-
489
- assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
490
- end
491
-
492
- should "handle low level error" do
493
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
494
- .to_raise(Errno::ECONNREFUSED.new)
495
-
496
- client = StripeClient.new
497
- e = assert_raises Stripe::APIConnectionError do
498
- client.execute_request(:post, "/v1/charges")
499
- end
500
-
501
- assert_equal StripeClient::ERROR_MESSAGE_CONNECTION % Stripe.api_base +
502
- "\n\n(Network error: Connection refused)",
503
- e.message
504
- end
505
-
506
- should "handle error response with unknown value" do
507
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
508
- .to_return(body: JSON.generate(bar: "foo"), status: 500)
509
-
510
- client = StripeClient.new
511
- e = assert_raises Stripe::APIError do
512
- client.execute_request(:post, "/v1/charges")
513
- end
514
-
515
- assert_equal 'Invalid response object from API: "{\"bar\":\"foo\"}" (HTTP response code was 500)', e.message
516
- end
517
-
518
- should "raise IdempotencyError on 400 of type idempotency_error" do
519
- data = make_missing_id_error
520
- data[:error][:type] = "idempotency_error"
521
-
522
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
523
- .to_return(body: JSON.generate(data), status: 400)
524
- client = StripeClient.new
525
-
526
- e = assert_raises Stripe::IdempotencyError do
527
- client.execute_request(:post, "/v1/charges")
528
- end
529
- assert_equal(400, e.http_status)
530
- assert_equal(true, e.json_body.is_a?(Hash))
531
- end
532
-
533
- should "raise InvalidRequestError on other 400s" do
534
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
535
- .to_return(body: JSON.generate(make_missing_id_error), status: 400)
536
- client = StripeClient.new
537
- begin
538
- client.execute_request(:post, "/v1/charges")
539
- rescue Stripe::InvalidRequestError => e
540
- assert_equal(400, e.http_status)
541
- assert_equal(true, e.json_body.is_a?(Hash))
542
- end
543
- end
544
-
545
- should "raise AuthenticationError on 401" do
546
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
547
- .to_return(body: JSON.generate(make_missing_id_error), status: 401)
548
- client = StripeClient.new
549
- begin
550
- client.execute_request(:post, "/v1/charges")
551
- rescue Stripe::AuthenticationError => e
552
- assert_equal(401, e.http_status)
553
- assert_equal(true, e.json_body.is_a?(Hash))
554
- end
555
- end
556
-
557
- should "raise CardError on 402" do
558
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
559
- .to_return(body: JSON.generate(make_invalid_exp_year_error), status: 402)
560
- client = StripeClient.new
561
- begin
562
- client.execute_request(:post, "/v1/charges")
563
- rescue Stripe::CardError => e
564
- assert_equal(402, e.http_status)
565
- assert_equal(true, e.json_body.is_a?(Hash))
566
- assert_equal("invalid_expiry_year", e.code)
567
- assert_equal("exp_year", e.param)
568
- end
569
- end
570
-
571
- should "raise PermissionError on 403" do
572
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
573
- .to_return(body: JSON.generate(make_missing_id_error), status: 403)
574
- client = StripeClient.new
575
- begin
576
- client.execute_request(:post, "/v1/charges")
577
- rescue Stripe::PermissionError => e
578
- assert_equal(403, e.http_status)
579
- assert_equal(true, e.json_body.is_a?(Hash))
580
- end
581
- end
582
-
583
- should "raise InvalidRequestError on 404" do
584
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
585
- .to_return(body: JSON.generate(make_missing_id_error), status: 404)
586
- client = StripeClient.new
587
- begin
588
- client.execute_request(:post, "/v1/charges")
589
- rescue Stripe::InvalidRequestError => e
590
- assert_equal(404, e.http_status)
591
- assert_equal(true, e.json_body.is_a?(Hash))
592
- end
593
- end
594
-
595
- should "raise RateLimitError on 429" do
596
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
597
- .to_return(body: JSON.generate(make_rate_limit_error), status: 429)
598
- client = StripeClient.new
599
- begin
600
- client.execute_request(:post, "/v1/charges")
601
- rescue Stripe::RateLimitError => e
602
- assert_equal(429, e.http_status)
603
- assert_equal(true, e.json_body.is_a?(Hash))
604
- end
605
- end
606
-
607
- should "raise OAuth::InvalidRequestError when error is a string with value 'invalid_request'" do
608
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
609
- .to_return(body: JSON.generate(error: "invalid_request",
610
- error_description: "No grant type specified"), status: 400)
611
-
612
- client = StripeClient.new
613
- opts = { api_base: Stripe.connect_base }
614
- e = assert_raises Stripe::OAuth::InvalidRequestError do
615
- client.execute_request(:post, "/oauth/token", opts)
616
- end
617
-
618
- assert_equal(400, e.http_status)
619
- assert_equal("No grant type specified", e.message)
620
- end
621
-
622
- should "raise OAuth::InvalidGrantError when error is a string with value 'invalid_grant'" do
623
- stub_request(:post, "#{Stripe.connect_base}/oauth/token")
624
- .to_return(body: JSON.generate(error: "invalid_grant",
625
- error_description: "This authorization code has already been used. All tokens issued with this code have been revoked."), status: 400)
626
-
627
- client = StripeClient.new
628
- opts = { api_base: Stripe.connect_base }
629
- e = assert_raises Stripe::OAuth::InvalidGrantError do
630
- client.execute_request(:post, "/oauth/token", opts)
631
- end
632
-
633
- assert_equal(400, e.http_status)
634
- assert_equal("invalid_grant", e.code)
635
- assert_equal("This authorization code has already been used. All tokens issued with this code have been revoked.", e.message)
636
- end
637
-
638
- should "raise OAuth::InvalidClientError when error is a string with value 'invalid_client'" do
639
- stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
640
- .to_return(body: JSON.generate(error: "invalid_client",
641
- error_description: "This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist."), status: 401)
642
-
643
- client = StripeClient.new
644
- opts = { api_base: Stripe.connect_base }
645
- e = assert_raises Stripe::OAuth::InvalidClientError do
646
- client.execute_request(:post, "/oauth/deauthorize", opts)
647
- end
648
-
649
- assert_equal(401, e.http_status)
650
- assert_equal("invalid_client", e.code)
651
- assert_equal("This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist.", e.message)
652
- end
653
-
654
- should "raise Stripe::OAuthError on indeterminate OAuth error" do
655
- stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize")
656
- .to_return(body: JSON.generate(error: "new_code_not_recognized",
657
- error_description: "Something."), status: 401)
658
-
659
- client = StripeClient.new
660
- opts = { api_base: Stripe.connect_base }
661
- e = assert_raises Stripe::OAuth::OAuthError do
662
- client.execute_request(:post, "/oauth/deauthorize", opts)
663
- end
664
-
665
- assert_equal(401, e.http_status)
666
- assert_equal("new_code_not_recognized", e.code)
667
- assert_equal("Something.", e.message)
668
- end
669
- end
670
-
671
- context "idempotency keys" do
672
- setup do
673
- Stripe.stubs(:max_network_retries).returns(2)
674
- end
675
-
676
- should "not add an idempotency key to GET requests" do
677
- SecureRandom.expects(:uuid).times(0)
678
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
679
- .with do |req|
680
- req.headers["Idempotency-Key"].nil?
681
- end.to_return(body: JSON.generate(object: "charge"))
682
- client = StripeClient.new
683
- client.execute_request(:get, "/v1/charges/ch_123")
684
- end
685
-
686
- should "ensure there is always an idempotency_key on POST requests" do
687
- SecureRandom.expects(:uuid).at_least_once.returns("random_key")
688
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
689
- .with(headers: { "Idempotency-Key" => "random_key" })
690
- .to_return(body: JSON.generate(object: "charge"))
691
- client = StripeClient.new
692
- client.execute_request(:post, "/v1/charges")
693
- end
694
-
695
- should "ensure there is always an idempotency_key on DELETE requests" do
696
- SecureRandom.expects(:uuid).at_least_once.returns("random_key")
697
- stub_request(:delete, "#{Stripe.api_base}/v1/charges/ch_123")
698
- .with(headers: { "Idempotency-Key" => "random_key" })
699
- .to_return(body: JSON.generate(object: "charge"))
700
- client = StripeClient.new
701
- client.execute_request(:delete, "/v1/charges/ch_123")
702
- end
703
-
704
- should "not override a provided idempotency_key" do
705
- # Note that this expectation looks like `:idempotency_key` instead of
706
- # the header `Idempotency-Key` because it's user provided as seen
707
- # below. The ones injected by the library itself look like headers
708
- # (`Idempotency-Key`), but rest-client does allow this symbol
709
- # formatting and will properly override the system generated one as
710
- # expected.
711
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
712
- .with(headers: { "Idempotency-Key" => "provided_key" })
713
- .to_return(body: JSON.generate(object: "charge"))
714
-
715
- client = StripeClient.new
716
- client.execute_request(:post, "/v1/charges",
717
- headers: { idempotency_key: "provided_key" })
718
- end
719
- end
720
-
721
- context "retry logic" do
722
- setup do
723
- Stripe.stubs(:max_network_retries).returns(2)
724
- end
725
-
726
- should "retry failed requests and raise if error persists" do
727
- StripeClient.expects(:sleep_time).at_least_once.returns(0)
728
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
729
- .to_raise(Errno::ECONNREFUSED.new)
730
-
731
- client = StripeClient.new
732
- err = assert_raises Stripe::APIConnectionError do
733
- client.execute_request(:post, "/v1/charges")
734
- end
735
- assert_match(/Request was retried 2 times/, err.message)
736
- end
737
-
738
- should "retry failed requests and return successful response" do
739
- StripeClient.expects(:sleep_time).at_least_once.returns(0)
740
-
741
- i = 0
742
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
743
- .to_return do |_|
744
- if i < 2
745
- i += 1
746
- raise Errno::ECONNREFUSED
747
- else
748
- { body: JSON.generate("id" => "myid") }
749
- end
750
- end
751
-
752
- client = StripeClient.new
753
- client.execute_request(:post, "/v1/charges")
754
- end
755
- end
756
-
757
- context "params serialization" do
758
- should "allows empty strings in params" do
759
- client = StripeClient.new
760
- client.execute_request(:get, "/v1/invoices/upcoming", params: {
761
- customer: "cus_123",
762
- coupon: "",
763
- })
764
- assert_requested(
765
- :get,
766
- "#{Stripe.api_base}/v1/invoices/upcoming?",
767
- query: {
768
- customer: "cus_123",
769
- coupon: "",
770
- }
771
- )
772
- end
773
-
774
- should "filter nils in params" do
775
- client = StripeClient.new
776
- client.execute_request(:get, "/v1/invoices/upcoming", params: {
777
- customer: "cus_123",
778
- coupon: nil,
779
- })
780
- assert_requested(
781
- :get,
782
- "#{Stripe.api_base}/v1/invoices/upcoming?",
783
- query: {
784
- customer: "cus_123",
785
- }
786
- )
787
- end
788
-
789
- should "merge query parameters in URL and params" do
790
- client = StripeClient.new
791
- client.execute_request(:get, "/v1/invoices/upcoming?coupon=25OFF", params: {
792
- customer: "cus_123",
793
- })
794
- assert_requested(
795
- :get,
796
- "#{Stripe.api_base}/v1/invoices/upcoming?",
797
- query: {
798
- coupon: "25OFF",
799
- customer: "cus_123",
800
- }
801
- )
802
- end
803
-
804
- should "prefer query parameters in params when specified in URL as well" do
805
- client = StripeClient.new
806
- client.execute_request(:get, "/v1/invoices/upcoming?customer=cus_query", params: {
807
- customer: "cus_param",
808
- })
809
- assert_requested(
810
- :get,
811
- "#{Stripe.api_base}/v1/invoices/upcoming?",
812
- query: {
813
- customer: "cus_param",
814
- }
815
- )
816
- end
817
- end
818
- end
819
-
820
- context "#request" do
821
- should "return a result and response object" do
822
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
823
- .to_return(body: JSON.generate(object: "charge"))
824
-
825
- client = StripeClient.new
826
- charge, resp = client.request { Charge.create }
827
-
828
- assert charge.is_a?(Charge)
829
- assert resp.is_a?(StripeResponse)
830
- assert_equal 200, resp.http_status
831
- end
832
-
833
- should "return the value of given block" do
834
- client = StripeClient.new
835
- ret, = client.request { 7 }
836
- assert_equal 7, ret
837
- end
838
-
839
- should "reset local thread state after a call" do
840
- begin
841
- StripeClient.current_thread_context.active_client = :stripe_client
842
-
843
- client = StripeClient.new
844
- client.request {}
845
-
846
- assert_equal :stripe_client,
847
- StripeClient.current_thread_context.active_client
848
- ensure
849
- StripeClient.current_thread_context.active_client = nil
850
- end
851
- end
852
-
853
- should "correctly return last responses despite multiple clients" do
854
- charge_resp = { object: "charge" }
855
- coupon_resp = { object: "coupon" }
856
-
857
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
858
- .to_return(body: JSON.generate(charge_resp))
859
- stub_request(:post, "#{Stripe.api_base}/v1/coupons")
860
- .to_return(body: JSON.generate(coupon_resp))
861
-
862
- client1 = StripeClient.new
863
- client2 = StripeClient.new
864
-
865
- client2_resp = nil
866
- _charge, client1_resp = client1.request do
867
- Charge.create
868
-
869
- # This is contrived, but we run one client nested in the `request`
870
- # block of another one just to ensure that the parent is still
871
- # unwinding when this goes through. If the parent's last response
872
- # were to be overridden by this client (through a bug), then it would
873
- # happen here.
874
- _coupon, client2_resp = client2.request do
875
- Coupon.create
876
- end
877
- end
878
-
879
- assert_equal charge_resp, client1_resp.data
880
- assert_equal coupon_resp, client2_resp.data
881
- end
882
-
883
- should "correctly return last responses despite multiple threads" do
884
- charge_resp = { object: "charge" }
885
- coupon_resp = { object: "coupon" }
886
-
887
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
888
- .to_return(body: JSON.generate(charge_resp))
889
- stub_request(:post, "#{Stripe.api_base}/v1/coupons")
890
- .to_return(body: JSON.generate(coupon_resp))
891
-
892
- client = StripeClient.new
893
-
894
- # Poorly named class -- note this is actually a concurrent queue.
895
- recv_queue = Queue.new
896
- send_queue = Queue.new
897
-
898
- # Start a thread, make an API request, but then idle in the `request`
899
- # block until the main thread has been able to make its own API request
900
- # and signal that it's done. If this thread's last response were to be
901
- # overridden by the main thread (through a bug), then this routine
902
- # should suss it out.
903
- resp1 = nil
904
- thread = Thread.start do
905
- _charge, resp1 = client.request do
906
- Charge.create
907
-
908
- # Idle in `request` block until main thread signals.
909
- send_queue.pop
910
- end
911
-
912
- # Signal main thread that we're done and it can run its checks.
913
- recv_queue << true
914
- end
915
-
916
- # Make an API request.
917
- _coupon, resp2 = client.request do
918
- Coupon.create
919
- end
920
-
921
- # Tell background thread to finish `request`, then wait for it to
922
- # signal back to us that it's ready.
923
- send_queue << true
924
- recv_queue.pop
925
-
926
- assert_equal charge_resp, resp1.data
927
- assert_equal coupon_resp, resp2.data
928
-
929
- # And for maximum hygiene, make sure that our thread rejoins.
930
- thread.join
931
- end
932
-
933
- should "error if calls to #request are nested on the same thread" do
934
- client = StripeClient.new
935
- client.request do
936
- e = assert_raises(RuntimeError) do
937
- client.request {}
938
- end
939
- assert_equal "calls to StripeClient#request cannot be nested within a thread",
940
- e.message
941
- end
942
- end
943
- end
944
-
945
- context "#proxy" do
946
- should "run the request through the proxy" do
947
- begin
948
- StripeClient.current_thread_context.default_connection_manager = nil
949
-
950
- Stripe.proxy = "http://user:pass@localhost:8080"
951
-
952
- client = StripeClient.new
953
- client.request {}
954
-
955
- connection = Stripe::StripeClient.default_connection_manager.connection_for(Stripe.api_base)
956
-
957
- assert_equal "localhost", connection.proxy_address
958
- assert_equal 8080, connection.proxy_port
959
- assert_equal "user", connection.proxy_user
960
- assert_equal "pass", connection.proxy_pass
961
- ensure
962
- Stripe.proxy = nil
963
-
964
- StripeClient.current_thread_context.default_connection_manager = nil
965
- end
966
- end
967
- end
968
-
969
- context "#telemetry" do
970
- teardown do
971
- # make sure to always set telemetry back to false
972
- # to not mutate global state
973
- Stripe.enable_telemetry = false
974
- end
975
-
976
- should "not send metrics if enable trace flag is not set" do
977
- Stripe.enable_telemetry = false
978
-
979
- trace_metrics_header = nil
980
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
981
- .with do |req|
982
- trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
983
- false
984
- end.to_return(body: JSON.generate(object: "charge"))
985
-
986
- Stripe::Charge.list
987
- assert(trace_metrics_header.nil?)
988
-
989
- Stripe::Charge.list
990
- assert(trace_metrics_header.nil?)
991
- end
992
-
993
- should "send metrics if enabled telemetry is true" do
994
- Stripe.enable_telemetry = true
995
-
996
- trace_metrics_header = nil
997
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
998
- .with do |req|
999
- trace_metrics_header = req.headers["X-Stripe-Client-Telemetry"]
1000
- false
1001
- end.to_return(body: JSON.generate(object: "charge"))
1002
-
1003
- Stripe::Charge.list
1004
- Stripe::Charge.list
1005
-
1006
- assert(!trace_metrics_header.nil?)
1007
-
1008
- trace_payload = JSON.parse(trace_metrics_header)
1009
- assert(trace_payload["last_request_metrics"]["request_id"] == "req_123")
1010
- assert(!trace_payload["last_request_metrics"]["request_duration_ms"].nil?)
1011
- end
1012
- end
1013
- end
1014
-
1015
- class SystemProfilerTest < Test::Unit::TestCase
1016
- context "#uname" do
1017
- should "run without failure" do
1018
- # Don't actually check the result because we try a variety of different
1019
- # strategies that will have different results depending on where this
1020
- # test and running. We're mostly making sure that no exception is thrown.
1021
- _ = StripeClient::SystemProfiler.uname
1022
- end
1023
- end
1024
-
1025
- context "#uname_from_system" do
1026
- should "run without failure" do
1027
- # as above, just verify that an exception is not thrown
1028
- _ = StripeClient::SystemProfiler.uname_from_system
1029
- end
1030
- end
1031
-
1032
- context "#uname_from_system_ver" do
1033
- should "run without failure" do
1034
- # as above, just verify that an exception is not thrown
1035
- _ = StripeClient::SystemProfiler.uname_from_system_ver
1036
- end
1037
- end
1038
- end
1039
- end