stripe 4.9.0 → 5.28.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (251) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +10 -0
  3. data/.rubocop.yml +56 -8
  4. data/.rubocop_todo.yml +8 -25
  5. data/.travis.yml +4 -8
  6. data/.vscode/extensions.json +7 -0
  7. data/.vscode/settings.json +8 -0
  8. data/CHANGELOG.md +206 -2
  9. data/CODE_OF_CONDUCT.md +77 -0
  10. data/Gemfile +10 -13
  11. data/README.md +178 -58
  12. data/Rakefile +8 -7
  13. data/VERSION +1 -1
  14. data/lib/stripe.rb +41 -195
  15. data/lib/stripe/api_operations/create.rb +1 -1
  16. data/lib/stripe/api_operations/delete.rb +28 -2
  17. data/lib/stripe/api_operations/list.rb +1 -12
  18. data/lib/stripe/api_operations/nested_resource.rb +38 -28
  19. data/lib/stripe/api_operations/request.rb +47 -8
  20. data/lib/stripe/api_operations/save.rb +8 -7
  21. data/lib/stripe/api_resource.rb +52 -3
  22. data/lib/stripe/connection_manager.rb +162 -0
  23. data/lib/stripe/error_object.rb +94 -0
  24. data/lib/stripe/errors.rb +31 -10
  25. data/lib/stripe/instrumentation.rb +82 -0
  26. data/lib/stripe/list_object.rb +41 -7
  27. data/lib/stripe/multipart_encoder.rb +131 -0
  28. data/lib/stripe/oauth.rb +4 -3
  29. data/lib/stripe/object_types.rb +101 -0
  30. data/lib/stripe/resources.rb +84 -0
  31. data/lib/stripe/{account.rb → resources/account.rb} +50 -27
  32. data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
  33. data/lib/stripe/resources/alipay_account.rb +34 -0
  34. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
  35. data/lib/stripe/resources/application_fee.rb +14 -0
  36. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  37. data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
  38. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
  39. data/lib/stripe/resources/bank_account.rb +43 -0
  40. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  41. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
  42. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  43. data/lib/stripe/resources/capability.rb +33 -0
  44. data/lib/stripe/{card.rb → resources/card.rb} +13 -4
  45. data/lib/stripe/resources/charge.rb +23 -0
  46. data/lib/stripe/resources/checkout/session.rb +16 -0
  47. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
  48. data/lib/stripe/{coupon.rb → resources/coupon.rb} +3 -2
  49. data/lib/stripe/resources/credit_note.rb +33 -0
  50. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  51. data/lib/stripe/resources/customer.rb +36 -0
  52. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  53. data/lib/stripe/resources/discount.rb +7 -0
  54. data/lib/stripe/resources/dispute.rb +22 -0
  55. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
  56. data/lib/stripe/{event.rb → resources/event.rb} +2 -1
  57. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
  58. data/lib/stripe/{file.rb → resources/file.rb} +9 -11
  59. data/lib/stripe/{file_link.rb → resources/file_link.rb} +3 -2
  60. data/lib/stripe/resources/invoice.rb +74 -0
  61. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +3 -2
  62. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  63. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  64. data/lib/stripe/resources/issuing/card.rb +25 -0
  65. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  66. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +3 -2
  67. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  68. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +3 -2
  69. data/lib/stripe/resources/line_item.rb +7 -0
  70. data/lib/stripe/resources/login_link.rb +14 -0
  71. data/lib/stripe/resources/mandate.rb +8 -0
  72. data/lib/stripe/resources/order.rb +33 -0
  73. data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
  74. data/lib/stripe/resources/payment_intent.rb +43 -0
  75. data/lib/stripe/resources/payment_method.rb +33 -0
  76. data/lib/stripe/resources/payout.rb +33 -0
  77. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  78. data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
  79. data/lib/stripe/resources/price.rb +12 -0
  80. data/lib/stripe/{product.rb → resources/product.rb} +4 -3
  81. data/lib/stripe/resources/promotion_code.rb +12 -0
  82. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  83. data/lib/stripe/{radar → resources/radar}/value_list.rb +3 -2
  84. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +3 -2
  85. data/lib/stripe/{recipient.rb → resources/recipient.rb} +3 -6
  86. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  87. data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
  88. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +3 -2
  89. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +3 -2
  90. data/lib/stripe/resources/reversal.rb +29 -0
  91. data/lib/stripe/resources/review.rb +21 -0
  92. data/lib/stripe/resources/setup_attempt.rb +10 -0
  93. data/lib/stripe/resources/setup_intent.rb +33 -0
  94. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +3 -2
  95. data/lib/stripe/{sku.rb → resources/sku.rb} +4 -3
  96. data/lib/stripe/resources/source.rb +47 -0
  97. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  98. data/lib/stripe/resources/subscription.rb +26 -0
  99. data/lib/stripe/resources/subscription_item.rb +26 -0
  100. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  101. data/lib/stripe/resources/tax_id.rb +26 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/{terminal → resources/terminal}/location.rb +4 -2
  105. data/lib/stripe/{terminal → resources/terminal}/reader.rb +4 -2
  106. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
  107. data/lib/stripe/{token.rb → resources/token.rb} +2 -1
  108. data/lib/stripe/resources/topup.rb +23 -0
  109. data/lib/stripe/resources/transfer.rb +27 -0
  110. data/lib/stripe/resources/usage_record.rb +7 -0
  111. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  112. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +3 -2
  113. data/lib/stripe/singleton_api_resource.rb +3 -1
  114. data/lib/stripe/stripe_client.rb +493 -236
  115. data/lib/stripe/stripe_configuration.rb +178 -0
  116. data/lib/stripe/stripe_object.rb +75 -61
  117. data/lib/stripe/stripe_response.rb +53 -21
  118. data/lib/stripe/util.rb +66 -109
  119. data/lib/stripe/version.rb +1 -1
  120. data/lib/stripe/webhook.rb +43 -10
  121. data/stripe.gemspec +14 -5
  122. data/test/stripe/account_link_test.rb +4 -4
  123. data/test/stripe/account_test.rb +193 -32
  124. data/test/stripe/alipay_account_test.rb +1 -1
  125. data/test/stripe/api_operations_test.rb +3 -4
  126. data/test/stripe/api_resource_test.rb +159 -37
  127. data/test/stripe/apple_pay_domain_test.rb +18 -5
  128. data/test/stripe/application_fee_refund_test.rb +1 -1
  129. data/test/stripe/application_fee_test.rb +45 -1
  130. data/test/stripe/balance_test.rb +1 -1
  131. data/test/stripe/balance_transaction_test.rb +20 -0
  132. data/test/stripe/bank_account_test.rb +1 -1
  133. data/test/stripe/billing_portal/session_test.rb +18 -0
  134. data/test/stripe/capability_test.rb +45 -0
  135. data/test/stripe/charge_test.rb +13 -8
  136. data/test/stripe/checkout/session_test.rb +19 -1
  137. data/test/stripe/connection_manager_test.rb +167 -0
  138. data/test/stripe/country_spec_test.rb +1 -1
  139. data/test/stripe/coupon_test.rb +16 -6
  140. data/test/stripe/credit_note_test.rb +90 -0
  141. data/test/stripe/customer_balance_transaction_test.rb +37 -0
  142. data/test/stripe/customer_card_test.rb +7 -1
  143. data/test/stripe/customer_test.rb +151 -40
  144. data/test/stripe/dispute_test.rb +10 -1
  145. data/test/stripe/ephemeral_key_test.rb +8 -1
  146. data/test/stripe/errors_test.rb +42 -9
  147. data/test/stripe/exchange_rate_test.rb +1 -1
  148. data/test/stripe/file_link_test.rb +1 -1
  149. data/test/stripe/file_test.rb +19 -5
  150. data/test/stripe/instrumentation_test.rb +74 -0
  151. data/test/stripe/invoice_item_test.rb +18 -7
  152. data/test/stripe/invoice_line_item_test.rb +1 -1
  153. data/test/stripe/invoice_test.rb +77 -9
  154. data/test/stripe/issuing/authorization_test.rb +33 -11
  155. data/test/stripe/issuing/card_test.rb +27 -6
  156. data/test/stripe/issuing/cardholder_test.rb +1 -1
  157. data/test/stripe/issuing/dispute_test.rb +23 -14
  158. data/test/stripe/issuing/transaction_test.rb +1 -1
  159. data/test/stripe/list_object_test.rb +70 -24
  160. data/test/stripe/login_link_test.rb +2 -2
  161. data/test/stripe/mandate_test.rb +14 -0
  162. data/test/stripe/multipart_encoder_test.rb +130 -0
  163. data/test/stripe/oauth_test.rb +17 -1
  164. data/test/stripe/order_return_test.rb +1 -1
  165. data/test/stripe/order_test.rb +28 -3
  166. data/test/stripe/payment_intent_test.rb +31 -4
  167. data/test/stripe/payment_method_test.rb +84 -0
  168. data/test/stripe/payout_test.rb +23 -1
  169. data/test/stripe/person_test.rb +1 -1
  170. data/test/stripe/plan_test.rb +26 -20
  171. data/test/stripe/price_test.rb +48 -0
  172. data/test/stripe/product_test.rb +17 -8
  173. data/test/stripe/promotion_code_test.rb +42 -0
  174. data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
  175. data/test/stripe/radar/value_list_item_test.rb +16 -6
  176. data/test/stripe/radar/value_list_test.rb +16 -6
  177. data/test/stripe/recipient_test.rb +18 -5
  178. data/test/stripe/refund_test.rb +1 -1
  179. data/test/stripe/reporting/report_run_test.rb +1 -1
  180. data/test/stripe/reporting/report_type_test.rb +1 -1
  181. data/test/stripe/reversal_test.rb +1 -1
  182. data/test/stripe/review_test.rb +1 -1
  183. data/test/stripe/setup_attempt_test.rb +16 -0
  184. data/test/stripe/setup_intent_test.rb +84 -0
  185. data/test/stripe/sigma/scheduled_query_run_test.rb +1 -1
  186. data/test/stripe/sku_test.rb +16 -6
  187. data/test/stripe/source_test.rb +52 -19
  188. data/test/stripe/stripe_client_test.rb +546 -78
  189. data/test/stripe/stripe_configuration_test.rb +131 -0
  190. data/test/stripe/stripe_object_test.rb +16 -41
  191. data/test/stripe/stripe_response_test.rb +71 -25
  192. data/test/stripe/subscription_item_test.rb +38 -7
  193. data/test/stripe/subscription_schedule_test.rb +19 -1
  194. data/test/stripe/subscription_test.rb +29 -9
  195. data/test/stripe/tax_id_test.rb +31 -0
  196. data/test/stripe/tax_rate_test.rb +43 -0
  197. data/test/stripe/terminal/connection_token_test.rb +1 -1
  198. data/test/stripe/terminal/location_test.rb +18 -1
  199. data/test/stripe/terminal/reader_test.rb +18 -1
  200. data/test/stripe/three_d_secure_test.rb +1 -1
  201. data/test/stripe/topup_test.rb +9 -1
  202. data/test/stripe/transfer_test.rb +46 -1
  203. data/test/stripe/usage_record_summary_test.rb +15 -5
  204. data/test/stripe/util_test.rb +1 -1
  205. data/test/stripe/webhook_endpoint_test.rb +18 -1
  206. data/test/stripe/webhook_test.rb +48 -9
  207. data/test/stripe_mock.rb +4 -3
  208. data/test/stripe_test.rb +102 -33
  209. data/test/test_helper.rb +16 -12
  210. metadata +141 -129
  211. data/lib/stripe/alipay_account.rb +0 -27
  212. data/lib/stripe/application_fee.rb +0 -23
  213. data/lib/stripe/application_fee_refund.rb +0 -22
  214. data/lib/stripe/bank_account.rb +0 -32
  215. data/lib/stripe/charge.rb +0 -84
  216. data/lib/stripe/checkout/session.rb +0 -11
  217. data/lib/stripe/customer.rb +0 -90
  218. data/lib/stripe/dispute.rb +0 -19
  219. data/lib/stripe/invoice.rb +0 -48
  220. data/lib/stripe/issuer_fraud_record.rb +0 -9
  221. data/lib/stripe/issuing/authorization.rb +0 -22
  222. data/lib/stripe/issuing/card.rb +0 -18
  223. data/lib/stripe/issuing/dispute.rb +0 -13
  224. data/lib/stripe/login_link.rb +0 -11
  225. data/lib/stripe/order.rb +0 -31
  226. data/lib/stripe/payment_intent.rb +0 -26
  227. data/lib/stripe/payout.rb +0 -20
  228. data/lib/stripe/reversal.rb +0 -22
  229. data/lib/stripe/review.rb +0 -14
  230. data/lib/stripe/source.rb +0 -38
  231. data/lib/stripe/subscription.rb +0 -25
  232. data/lib/stripe/subscription_item.rb +0 -17
  233. data/lib/stripe/subscription_schedule.rb +0 -32
  234. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  235. data/lib/stripe/terminal/connection_token.rb +0 -11
  236. data/lib/stripe/topup.rb +0 -16
  237. data/lib/stripe/transfer.rb +0 -23
  238. data/lib/stripe/usage_record.rb +0 -14
  239. data/test/api_stub_helpers.rb +0 -1
  240. data/test/stripe/account_external_accounts_operations_test.rb +0 -69
  241. data/test/stripe/account_login_links_operations_test.rb +0 -21
  242. data/test/stripe/account_persons_operations_test.rb +0 -70
  243. data/test/stripe/application_fee_refunds_operations_test.rb +0 -56
  244. data/test/stripe/customer_sources_operations_test.rb +0 -64
  245. data/test/stripe/file_upload_test.rb +0 -76
  246. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  247. data/test/stripe/source_transaction_test.rb +0 -19
  248. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  249. data/test/stripe/subscription_schedule_revisions_operations_test.rb +0 -35
  250. data/test/stripe/transfer_reversals_operations_test.rb +0 -57
  251. data/test/stripe/usage_record_test.rb +0 -28
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require ::File.expand_path("../../test_helper", __FILE__)
3
+ require ::File.expand_path("../test_helper", __dir__)
4
4
 
5
5
  module Stripe
6
6
  class StripeClientTest < Test::Unit::TestCase
@@ -17,6 +17,120 @@ module Stripe
17
17
  end
18
18
  end
19
19
 
20
+ context ".maybe_gc_connection_managers" do
21
+ should "garbage collect old connection managers when appropriate" do
22
+ stub_request(:post, "#{Stripe.api_base}/v1/path")
23
+ .to_return(body: JSON.generate(object: "account"))
24
+
25
+ # Make sure we start with a blank slate (state may have been left in
26
+ # place by other tests).
27
+ StripeClient.clear_all_connection_managers
28
+
29
+ # Establish a base time.
30
+ t = 0.0
31
+
32
+ # And pretend that `StripeClient` was just initialized for the first
33
+ # time. (Don't access instance variables like this, but it's tricky to
34
+ # test properly otherwise.)
35
+ StripeClient.instance_variable_set(:@last_connection_manager_gc, t)
36
+
37
+ #
38
+ # t
39
+ #
40
+ Util.stubs(:monotonic_time).returns(t)
41
+
42
+ # Execute an initial request to ensure that a connection manager was
43
+ # created.
44
+ client = StripeClient.new
45
+ client.execute_request(:post, "/v1/path")
46
+
47
+ # The GC shouldn't run yet (a `nil` return indicates that GC didn't run).
48
+ assert_equal nil, StripeClient.maybe_gc_connection_managers
49
+
50
+ #
51
+ # t + StripeClient::CONNECTION_MANAGER_GC_PERIOD - 1
52
+ #
53
+ # Move time to just *before* garbage collection is eligible to run.
54
+ # Nothing should happen.
55
+ #
56
+ Util.stubs(:monotonic_time).returns(t + StripeClient::CONNECTION_MANAGER_GC_PERIOD - 1)
57
+
58
+ assert_equal nil, StripeClient.maybe_gc_connection_managers
59
+
60
+ #
61
+ # t + StripeClient::CONNECTION_MANAGER_GC_PERIOD + 1
62
+ #
63
+ # Move time to just *after* garbage collection is eligible to run.
64
+ # Garbage collection will run, but because the connection manager is
65
+ # not passed its expiry age, it will not be collected. Zero is returned
66
+ # to indicate so.
67
+ #
68
+ Util.stubs(:monotonic_time).returns(t + StripeClient::CONNECTION_MANAGER_GC_PERIOD + 1)
69
+
70
+ assert_equal 0, StripeClient.maybe_gc_connection_managers
71
+
72
+ #
73
+ # t + StripeClient::CONNECTION_MANAGER_GC_LAST_USED_EXPIRY + 1
74
+ #
75
+ # Move us far enough into the future that we're passed the horizons for
76
+ # both a GC run as well as well as the expiry age of a connection
77
+ # manager. That means the GC will run and collect the connection
78
+ # manager that we created above.
79
+ #
80
+ Util.stubs(:monotonic_time).returns(t + StripeClient::CONNECTION_MANAGER_GC_LAST_USED_EXPIRY + 1)
81
+
82
+ assert_equal 1, StripeClient.maybe_gc_connection_managers
83
+
84
+ # And as an additional check, the connection manager of the current
85
+ # thread context should have been set to `nil` as it was GCed.
86
+ assert_nil StripeClient.current_thread_context.default_connection_manager
87
+ end
88
+ end
89
+
90
+ context ".clear_all_connection_managers" do
91
+ should "clear connection managers across all threads" do
92
+ stub_request(:post, "#{Stripe.api_base}/path")
93
+ .to_return(body: JSON.generate(object: "account"))
94
+
95
+ num_threads = 3
96
+
97
+ # Poorly named class -- note this is actually a concurrent queue.
98
+ recv_queue = Queue.new
99
+ send_queue = Queue.new
100
+
101
+ threads = num_threads.times.map do |_|
102
+ Thread.start do
103
+ # Causes a connection manager to be created on this thread and a
104
+ # connection within that manager to be created for API access.
105
+ manager = StripeClient.default_connection_manager
106
+ manager.execute_request(:post, "#{Stripe.api_base}/path")
107
+
108
+ # Signal to the main thread we're ready.
109
+ recv_queue << true
110
+
111
+ # Wait for the main thread to signal continue.
112
+ send_queue.pop
113
+
114
+ # This check isn't great, but it's otherwise difficult to tell that
115
+ # anything happened with just the public-facing API.
116
+ assert_equal({}, manager.instance_variable_get(:@active_connections))
117
+ end
118
+ end
119
+
120
+ # Wait for threads to start up.
121
+ threads.each { recv_queue.pop }
122
+
123
+ # Do the clear (the method we're actually trying to test).
124
+ StripeClient.clear_all_connection_managers
125
+
126
+ # Tell threads to run their check.
127
+ threads.each { send_queue << true }
128
+
129
+ # And finally, give all threads time to perform their check.
130
+ threads.each(&:join)
131
+ end
132
+ end
133
+
20
134
  context ".default_client" do
21
135
  should "be a StripeClient" do
22
136
  assert_kind_of StripeClient, StripeClient.default_client
@@ -32,18 +146,19 @@ module Stripe
32
146
  end
33
147
  end
34
148
 
35
- context ".default_conn" do
36
- should "be a Faraday::Connection" do
37
- assert_kind_of Faraday::Connection, StripeClient.default_conn
149
+ context ".default_connection_manager" do
150
+ should "be a ConnectionManager" do
151
+ assert_kind_of ConnectionManager,
152
+ StripeClient.default_connection_manager
38
153
  end
39
154
 
40
155
  should "be a different connection on each thread" do
41
- other_thread_conn = nil
156
+ other_thread_manager = nil
42
157
  thread = Thread.new do
43
- other_thread_conn = StripeClient.default_conn
158
+ other_thread_manager = StripeClient.default_connection_manager
44
159
  end
45
160
  thread.join
46
- refute_equal StripeClient.default_conn, other_thread_conn
161
+ refute_equal StripeClient.default_connection_manager, other_thread_manager
47
162
  end
48
163
  end
49
164
 
@@ -52,26 +167,108 @@ module Stripe
52
167
  Stripe.stubs(:max_network_retries).returns(2)
53
168
  end
54
169
 
55
- should "retry on timeout" do
56
- assert StripeClient.should_retry?(Faraday::TimeoutError.new(""), 0)
170
+ should "retry on Errno::ECONNREFUSED" do
171
+ assert StripeClient.should_retry?(Errno::ECONNREFUSED.new,
172
+ method: :post, num_retries: 0)
57
173
  end
58
174
 
59
- should "retry on a failed connection" do
60
- assert StripeClient.should_retry?(Faraday::ConnectionFailed.new(""), 0)
175
+ should "retry on EOFError" do
176
+ assert StripeClient.should_retry?(EOFError.new,
177
+ method: :post, num_retries: 0)
61
178
  end
62
179
 
63
- should "retry on a conflict" do
64
- error = make_rate_limit_error
65
- e = Faraday::ClientError.new(error[:error][:message], status: 409)
66
- assert StripeClient.should_retry?(e, 0)
180
+ should "retry on Errno::ECONNRESET" do
181
+ assert StripeClient.should_retry?(Errno::ECONNRESET.new,
182
+ method: :post, num_retries: 0)
183
+ end
184
+
185
+ should "retry on Errno::ETIMEDOUT" do
186
+ assert StripeClient.should_retry?(Errno::ETIMEDOUT.new,
187
+ method: :post, num_retries: 0)
188
+ end
189
+
190
+ should "retry on Errno::EHOSTUNREACH" do
191
+ assert StripeClient.should_retry?(Errno::EHOSTUNREACH.new,
192
+ method: :post, num_retries: 0)
193
+ end
194
+
195
+ should "retry on Net::OpenTimeout" do
196
+ assert StripeClient.should_retry?(Net::OpenTimeout.new,
197
+ method: :post, num_retries: 0)
198
+ end
199
+
200
+ should "retry on Net::ReadTimeout" do
201
+ assert StripeClient.should_retry?(Net::ReadTimeout.new,
202
+ method: :post, num_retries: 0)
203
+ end
204
+
205
+ should "retry on SocketError" do
206
+ assert StripeClient.should_retry?(SocketError.new,
207
+ method: :post, num_retries: 0)
208
+ end
209
+
210
+ should "retry when the `Stripe-Should-Retry` header is `true`" do
211
+ headers = StripeResponse::Headers.new(
212
+ "Stripe-Should-Retry" => ["true"]
213
+ )
214
+
215
+ # Note we send status 400 here, which would normally not be retried.
216
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_headers: headers,
217
+ http_status: 400),
218
+ method: :post, num_retries: 0)
219
+ end
220
+
221
+ should "not retry when the `Stripe-Should-Retry` header is `false`" do
222
+ headers = StripeResponse::Headers.new(
223
+ "Stripe-Should-Retry" => ["false"]
224
+ )
225
+
226
+ # Note we send status 409 here, which would normally be retried.
227
+ refute StripeClient.should_retry?(Stripe::StripeError.new(http_headers: headers,
228
+ http_status: 409),
229
+ method: :post, num_retries: 0)
230
+ end
231
+
232
+ should "retry on a 409 Conflict" do
233
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 409),
234
+ method: :post, num_retries: 0)
235
+ end
236
+
237
+ should "retry on a 429 Too Many Requests when lock timeout" do
238
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 429,
239
+ code: "lock_timeout"),
240
+ method: :post, num_retries: 0)
241
+ end
242
+
243
+ should "retry on a 500 Internal Server Error when non-POST" do
244
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
245
+ method: :get, num_retries: 0)
246
+ end
247
+
248
+ should "retry on a 503 Service Unavailable" do
249
+ assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 503),
250
+ method: :post, num_retries: 0)
67
251
  end
68
252
 
69
253
  should "not retry at maximum count" do
70
- refute StripeClient.should_retry?(RuntimeError.new, Stripe.max_network_retries)
254
+ refute StripeClient.should_retry?(RuntimeError.new,
255
+ method: :post, num_retries: Stripe.max_network_retries)
71
256
  end
72
257
 
73
258
  should "not retry on a certificate validation error" do
74
- refute StripeClient.should_retry?(Faraday::SSLError.new(""), 0)
259
+ refute StripeClient.should_retry?(OpenSSL::SSL::SSLError.new,
260
+ method: :post, num_retries: 0)
261
+ end
262
+
263
+ should "not retry on a 429 Too Many Requests when not lock timeout" do
264
+ refute StripeClient.should_retry?(Stripe::StripeError.new(http_status: 429,
265
+ code: "rate_limited"),
266
+ method: :post, num_retries: 0)
267
+ end
268
+
269
+ should "not retry on a 500 Internal Server Error when POST" do
270
+ refute StripeClient.should_retry?(Stripe::StripeError.new(http_status: 500),
271
+ method: :post, num_retries: 0)
75
272
  end
76
273
  end
77
274
 
@@ -114,19 +311,6 @@ module Stripe
114
311
  end
115
312
  end
116
313
 
117
- context "#initialize" do
118
- should "set Stripe.default_conn" do
119
- client = StripeClient.new
120
- assert_equal StripeClient.default_conn, client.conn
121
- end
122
-
123
- should "set a different connection if one was specified" do
124
- conn = Faraday.new
125
- client = StripeClient.new(conn)
126
- assert_equal conn, client.conn
127
- end
128
- end
129
-
130
314
  context "#execute_request" do
131
315
  context "headers" do
132
316
  should "support literal headers" do
@@ -153,16 +337,11 @@ module Stripe
153
337
  context "logging" do
154
338
  setup do
155
339
  # Freeze time for the purposes of the `elapsed` parameter that we
156
- # emit for responses. I didn't want to bring in a new dependency for
157
- # this, but Mocha's `anything` parameter can't match inside of a hash
158
- # and is therefore not useful for this purpose. If we switch over to
159
- # rspec-mocks at some point, we can probably remove Timecop from the
160
- # project.
161
- Timecop.freeze(Time.local(1990))
162
- end
163
-
164
- teardown do
165
- Timecop.return
340
+ # emit for responses. Mocha's `anything` parameter can't match inside
341
+ # of a hash and is therefore not useful for this purpose. If we
342
+ # switch over to rspec-mocks at some point, we can probably remove
343
+ # this.
344
+ Util.stubs(:monotonic_time).returns(0.0)
166
345
  end
167
346
 
168
347
  should "produce appropriate logging" do
@@ -178,7 +357,7 @@ module Stripe
178
357
  Util.expects(:log_debug).with("Request details",
179
358
  body: "",
180
359
  idempotency_key: "abc",
181
- query_params: nil)
360
+ query: nil)
182
361
 
183
362
  Util.expects(:log_info).with("Response from Stripe API",
184
363
  account: "acct_123",
@@ -295,7 +474,7 @@ module Stripe
295
474
  client = StripeClient.new
296
475
  opts = { api_base: Stripe.connect_base }
297
476
  assert_raises Stripe::OAuth::InvalidRequestError do
298
- client.execute_request(:post, "/oauth/token", opts)
477
+ client.execute_request(:post, "/oauth/token", **opts)
299
478
  end
300
479
  end
301
480
  end
@@ -384,10 +563,10 @@ module Stripe
384
563
  .to_return(body: "", status: 500)
385
564
 
386
565
  client = StripeClient.new
566
+
387
567
  e = assert_raises Stripe::APIError do
388
568
  client.execute_request(:post, "/v1/charges")
389
569
  end
390
-
391
570
  assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message
392
571
  end
393
572
 
@@ -396,22 +575,50 @@ module Stripe
396
575
  .to_return(body: "", status: 200)
397
576
 
398
577
  client = StripeClient.new
578
+
399
579
  e = assert_raises Stripe::APIError do
400
580
  client.execute_request(:post, "/v1/charges")
401
581
  end
402
-
403
582
  assert_equal 'Invalid response object from API: "" (HTTP response code was 200)', e.message
404
583
  end
405
584
 
585
+ should "feed a request ID through to the error object" do
586
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
587
+ .to_return(body: JSON.generate(make_missing_id_error),
588
+ headers: { "Request-ID": "req_123" },
589
+ status: 400)
590
+
591
+ client = StripeClient.new
592
+
593
+ e = assert_raises Stripe::InvalidRequestError do
594
+ client.execute_request(:post, "/v1/charges")
595
+ end
596
+ assert_equal("req_123", e.request_id)
597
+ end
598
+
599
+ should "handle low level error" do
600
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
601
+ .to_raise(Errno::ECONNREFUSED.new)
602
+
603
+ client = StripeClient.new
604
+
605
+ e = assert_raises Stripe::APIConnectionError do
606
+ client.execute_request(:post, "/v1/charges")
607
+ end
608
+ assert_equal StripeClient::ERROR_MESSAGE_CONNECTION % Stripe.api_base +
609
+ "\n\n(Network error: Connection refused)",
610
+ e.message
611
+ end
612
+
406
613
  should "handle error response with unknown value" do
407
614
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
408
615
  .to_return(body: JSON.generate(bar: "foo"), status: 500)
409
616
 
410
617
  client = StripeClient.new
618
+
411
619
  e = assert_raises Stripe::APIError do
412
620
  client.execute_request(:post, "/v1/charges")
413
621
  end
414
-
415
622
  assert_equal 'Invalid response object from API: "{\"bar\":\"foo\"}" (HTTP response code was 500)', e.message
416
623
  end
417
624
 
@@ -421,6 +628,7 @@ module Stripe
421
628
 
422
629
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
423
630
  .to_return(body: JSON.generate(data), status: 400)
631
+
424
632
  client = StripeClient.new
425
633
 
426
634
  e = assert_raises Stripe::IdempotencyError do
@@ -433,75 +641,81 @@ module Stripe
433
641
  should "raise InvalidRequestError on other 400s" do
434
642
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
435
643
  .to_return(body: JSON.generate(make_missing_id_error), status: 400)
644
+
436
645
  client = StripeClient.new
437
- begin
646
+
647
+ e = assert_raises Stripe::InvalidRequestError do
438
648
  client.execute_request(:post, "/v1/charges")
439
- rescue Stripe::InvalidRequestError => e
440
- assert_equal(400, e.http_status)
441
- assert_equal(true, e.json_body.is_a?(Hash))
442
649
  end
650
+ assert_equal(400, e.http_status)
651
+ assert_equal(true, e.json_body.is_a?(Hash))
443
652
  end
444
653
 
445
654
  should "raise AuthenticationError on 401" do
446
655
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
447
656
  .to_return(body: JSON.generate(make_missing_id_error), status: 401)
657
+
448
658
  client = StripeClient.new
449
- begin
659
+
660
+ e = assert_raises Stripe::AuthenticationError do
450
661
  client.execute_request(:post, "/v1/charges")
451
- rescue Stripe::AuthenticationError => e
452
- assert_equal(401, e.http_status)
453
- assert_equal(true, e.json_body.is_a?(Hash))
454
662
  end
663
+ assert_equal(401, e.http_status)
664
+ assert_equal(true, e.json_body.is_a?(Hash))
455
665
  end
456
666
 
457
667
  should "raise CardError on 402" do
458
668
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
459
669
  .to_return(body: JSON.generate(make_invalid_exp_year_error), status: 402)
670
+
460
671
  client = StripeClient.new
461
- begin
672
+
673
+ e = assert_raises Stripe::CardError do
462
674
  client.execute_request(:post, "/v1/charges")
463
- rescue Stripe::CardError => e
464
- assert_equal(402, e.http_status)
465
- assert_equal(true, e.json_body.is_a?(Hash))
466
- assert_equal("invalid_expiry_year", e.code)
467
- assert_equal("exp_year", e.param)
468
675
  end
676
+ assert_equal(402, e.http_status)
677
+ assert_equal(true, e.json_body.is_a?(Hash))
678
+ assert_equal("invalid_expiry_year", e.code)
679
+ assert_equal("exp_year", e.param)
469
680
  end
470
681
 
471
682
  should "raise PermissionError on 403" do
472
683
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
473
684
  .to_return(body: JSON.generate(make_missing_id_error), status: 403)
685
+
474
686
  client = StripeClient.new
475
- begin
687
+
688
+ e = assert_raises Stripe::PermissionError do
476
689
  client.execute_request(:post, "/v1/charges")
477
- rescue Stripe::PermissionError => e
478
- assert_equal(403, e.http_status)
479
- assert_equal(true, e.json_body.is_a?(Hash))
480
690
  end
691
+ assert_equal(403, e.http_status)
692
+ assert_equal(true, e.json_body.is_a?(Hash))
481
693
  end
482
694
 
483
695
  should "raise InvalidRequestError on 404" do
484
696
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
485
697
  .to_return(body: JSON.generate(make_missing_id_error), status: 404)
698
+
486
699
  client = StripeClient.new
487
- begin
700
+
701
+ e = assert_raises Stripe::InvalidRequestError do
488
702
  client.execute_request(:post, "/v1/charges")
489
- rescue Stripe::InvalidRequestError => e
490
- assert_equal(404, e.http_status)
491
- assert_equal(true, e.json_body.is_a?(Hash))
492
703
  end
704
+ assert_equal(404, e.http_status)
705
+ assert_equal(true, e.json_body.is_a?(Hash))
493
706
  end
494
707
 
495
708
  should "raise RateLimitError on 429" do
496
709
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
497
710
  .to_return(body: JSON.generate(make_rate_limit_error), status: 429)
711
+
498
712
  client = StripeClient.new
499
- begin
713
+
714
+ e = assert_raises Stripe::RateLimitError do
500
715
  client.execute_request(:post, "/v1/charges")
501
- rescue Stripe::RateLimitError => e
502
- assert_equal(429, e.http_status)
503
- assert_equal(true, e.json_body.is_a?(Hash))
504
716
  end
717
+ assert_equal(429, e.http_status)
718
+ assert_equal(true, e.json_body.is_a?(Hash))
505
719
  end
506
720
 
507
721
  should "raise OAuth::InvalidRequestError when error is a string with value 'invalid_request'" do
@@ -510,9 +724,10 @@ module Stripe
510
724
  error_description: "No grant type specified"), status: 400)
511
725
 
512
726
  client = StripeClient.new
727
+
513
728
  opts = { api_base: Stripe.connect_base }
514
729
  e = assert_raises Stripe::OAuth::InvalidRequestError do
515
- client.execute_request(:post, "/oauth/token", opts)
730
+ client.execute_request(:post, "/oauth/token", **opts)
516
731
  end
517
732
 
518
733
  assert_equal(400, e.http_status)
@@ -525,9 +740,10 @@ module Stripe
525
740
  error_description: "This authorization code has already been used. All tokens issued with this code have been revoked."), status: 400)
526
741
 
527
742
  client = StripeClient.new
743
+
528
744
  opts = { api_base: Stripe.connect_base }
529
745
  e = assert_raises Stripe::OAuth::InvalidGrantError do
530
- client.execute_request(:post, "/oauth/token", opts)
746
+ client.execute_request(:post, "/oauth/token", **opts)
531
747
  end
532
748
 
533
749
  assert_equal(400, e.http_status)
@@ -541,9 +757,10 @@ module Stripe
541
757
  error_description: "This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist."), status: 401)
542
758
 
543
759
  client = StripeClient.new
760
+
544
761
  opts = { api_base: Stripe.connect_base }
545
762
  e = assert_raises Stripe::OAuth::InvalidClientError do
546
- client.execute_request(:post, "/oauth/deauthorize", opts)
763
+ client.execute_request(:post, "/oauth/deauthorize", **opts)
547
764
  end
548
765
 
549
766
  assert_equal(401, e.http_status)
@@ -557,9 +774,10 @@ module Stripe
557
774
  error_description: "Something."), status: 401)
558
775
 
559
776
  client = StripeClient.new
777
+
560
778
  opts = { api_base: Stripe.connect_base }
561
779
  e = assert_raises Stripe::OAuth::OAuthError do
562
- client.execute_request(:post, "/oauth/deauthorize", opts)
780
+ client.execute_request(:post, "/oauth/deauthorize", **opts)
563
781
  end
564
782
 
565
783
  assert_equal(401, e.http_status)
@@ -717,6 +935,22 @@ module Stripe
717
935
  end
718
936
  end
719
937
 
938
+ context "#connection_manager" do
939
+ should "warn that #connection_manager is deprecated" do
940
+ old_stderr = $stderr
941
+ $stderr = StringIO.new
942
+ begin
943
+ client = StripeClient.new
944
+ client.connection_manager
945
+ message = "NOTE: Stripe::StripeClient#connection_manager is " \
946
+ "deprecated"
947
+ assert_match Regexp.new(message), $stderr.string
948
+ ensure
949
+ $stderr = old_stderr
950
+ end
951
+ end
952
+ end
953
+
720
954
  context "#request" do
721
955
  should "return a result and response object" do
722
956
  stub_request(:post, "#{Stripe.api_base}/v1/charges")
@@ -738,14 +972,130 @@ module Stripe
738
972
 
739
973
  should "reset local thread state after a call" do
740
974
  begin
741
- Thread.current[:stripe_client] = :stripe_client
975
+ StripeClient.current_thread_context.active_client = :stripe_client
742
976
 
743
977
  client = StripeClient.new
744
978
  client.request {}
745
979
 
746
- assert_equal :stripe_client, Thread.current[:stripe_client]
980
+ assert_equal :stripe_client,
981
+ StripeClient.current_thread_context.active_client
747
982
  ensure
748
- Thread.current[:stripe_client] = nil
983
+ StripeClient.current_thread_context.active_client = nil
984
+ end
985
+ end
986
+
987
+ should "correctly return last responses despite multiple clients" do
988
+ charge_resp = { object: "charge" }
989
+ coupon_resp = { object: "coupon" }
990
+
991
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
992
+ .to_return(body: JSON.generate(charge_resp))
993
+ stub_request(:post, "#{Stripe.api_base}/v1/coupons")
994
+ .to_return(body: JSON.generate(coupon_resp))
995
+
996
+ client1 = StripeClient.new
997
+ client2 = StripeClient.new
998
+
999
+ client2_resp = nil
1000
+ _charge, client1_resp = client1.request do
1001
+ Charge.create
1002
+
1003
+ # This is contrived, but we run one client nested in the `request`
1004
+ # block of another one just to ensure that the parent is still
1005
+ # unwinding when this goes through. If the parent's last response
1006
+ # were to be overridden by this client (through a bug), then it would
1007
+ # happen here.
1008
+ _coupon, client2_resp = client2.request do
1009
+ Coupon.create
1010
+ end
1011
+ end
1012
+
1013
+ assert_equal charge_resp, client1_resp.data
1014
+ assert_equal coupon_resp, client2_resp.data
1015
+ end
1016
+
1017
+ should "correctly return last responses despite multiple threads" do
1018
+ charge_resp = { object: "charge" }
1019
+ coupon_resp = { object: "coupon" }
1020
+
1021
+ stub_request(:post, "#{Stripe.api_base}/v1/charges")
1022
+ .to_return(body: JSON.generate(charge_resp))
1023
+ stub_request(:post, "#{Stripe.api_base}/v1/coupons")
1024
+ .to_return(body: JSON.generate(coupon_resp))
1025
+
1026
+ client = StripeClient.new
1027
+
1028
+ # Poorly named class -- note this is actually a concurrent queue.
1029
+ recv_queue = Queue.new
1030
+ send_queue = Queue.new
1031
+
1032
+ # Start a thread, make an API request, but then idle in the `request`
1033
+ # block until the main thread has been able to make its own API request
1034
+ # and signal that it's done. If this thread's last response were to be
1035
+ # overridden by the main thread (through a bug), then this routine
1036
+ # should suss it out.
1037
+ resp1 = nil
1038
+ thread = Thread.start do
1039
+ _charge, resp1 = client.request do
1040
+ Charge.create
1041
+
1042
+ # Idle in `request` block until main thread signals.
1043
+ send_queue.pop
1044
+ end
1045
+
1046
+ # Signal main thread that we're done and it can run its checks.
1047
+ recv_queue << true
1048
+ end
1049
+
1050
+ # Make an API request.
1051
+ _coupon, resp2 = client.request do
1052
+ Coupon.create
1053
+ end
1054
+
1055
+ # Tell background thread to finish `request`, then wait for it to
1056
+ # signal back to us that it's ready.
1057
+ send_queue << true
1058
+ recv_queue.pop
1059
+
1060
+ assert_equal charge_resp, resp1.data
1061
+ assert_equal coupon_resp, resp2.data
1062
+
1063
+ # And for maximum hygiene, make sure that our thread rejoins.
1064
+ thread.join
1065
+ end
1066
+
1067
+ should "error if calls to #request are nested on the same thread" do
1068
+ client = StripeClient.new
1069
+ client.request do
1070
+ e = assert_raises(RuntimeError) do
1071
+ client.request {}
1072
+ end
1073
+ assert_equal "calls to StripeClient#request cannot be nested within a thread",
1074
+ e.message
1075
+ end
1076
+ end
1077
+ end
1078
+
1079
+ context "#proxy" do
1080
+ should "run the request through the proxy" do
1081
+ begin
1082
+ StripeClient.current_thread_context.default_connection_manager = nil
1083
+
1084
+ Stripe.proxy = "http://user:pass@localhost:8080"
1085
+
1086
+ client = StripeClient.new
1087
+ client.request {}
1088
+
1089
+ connection = Stripe::StripeClient.default_connection_manager.connection_for(Stripe.api_base)
1090
+
1091
+ assert_equal "localhost", connection.proxy_address
1092
+ assert_equal 8080, connection.proxy_port
1093
+ assert_equal "user", connection.proxy_user
1094
+ assert_equal "pass", connection.proxy_pass
1095
+ ensure
1096
+ Stripe.proxy = nil
1097
+
1098
+ StripeClient.current_thread_context.default_connection_manager = nil
749
1099
  end
750
1100
  end
751
1101
  end
@@ -794,6 +1144,124 @@ module Stripe
794
1144
  assert(!trace_payload["last_request_metrics"]["request_duration_ms"].nil?)
795
1145
  end
796
1146
  end
1147
+
1148
+ context "instrumentation" do
1149
+ teardown do
1150
+ Stripe::Instrumentation.unsubscribe(:request, :test)
1151
+ end
1152
+
1153
+ should "notify a subscribe on HTTP request start" do
1154
+ events = []
1155
+ Stripe::Instrumentation.subscribe(:request_end, :test) { |event| events << event }
1156
+
1157
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1158
+ .to_return(body: JSON.generate(object: "charge"))
1159
+ Stripe::Charge.list
1160
+
1161
+ assert_equal(1, events.size)
1162
+ event = events.first
1163
+ assert_equal(:get, event.method)
1164
+ assert_equal("/v1/charges", event.path)
1165
+ end
1166
+
1167
+ should "notify a subscriber of a successful HTTP request" do
1168
+ events = []
1169
+ Stripe::Instrumentation.subscribe(:request_end, :test) { |event| events << event }
1170
+
1171
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1172
+ .to_return(body: JSON.generate(object: "charge"))
1173
+ Stripe::Charge.list
1174
+
1175
+ assert_equal(1, events.size)
1176
+ event = events.first
1177
+ assert_equal(:get, event.method)
1178
+ assert_equal("/v1/charges", event.path)
1179
+ assert_equal(200, event.http_status)
1180
+ assert(event.duration.positive?)
1181
+ assert_equal(0, event.num_retries)
1182
+ end
1183
+
1184
+ should "notify a subscriber of a StripeError" do
1185
+ events = []
1186
+ Stripe::Instrumentation.subscribe(:request_end, :test) { |event| events << event }
1187
+
1188
+ error = {
1189
+ code: "code",
1190
+ message: "message",
1191
+ param: "param",
1192
+ type: "type",
1193
+ }
1194
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1195
+ .to_return(
1196
+ body: JSON.generate(error: error),
1197
+ status: 500
1198
+ )
1199
+ assert_raises(Stripe::APIError) do
1200
+ Stripe::Charge.list
1201
+ end
1202
+
1203
+ assert_equal(1, events.size)
1204
+ event = events.first
1205
+ assert_equal(:get, event.method)
1206
+ assert_equal("/v1/charges", event.path)
1207
+ assert_equal(500, event.http_status)
1208
+ assert(event.duration.positive?)
1209
+ assert_equal(0, event.num_retries)
1210
+ end
1211
+
1212
+ should "notify a subscriber of a network error" do
1213
+ events = []
1214
+ Stripe::Instrumentation.subscribe(:request_end, :test) { |event| events << event }
1215
+
1216
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1217
+ .to_raise(Net::OpenTimeout)
1218
+ assert_raises(Stripe::APIConnectionError) do
1219
+ Stripe::Charge.list
1220
+ end
1221
+
1222
+ assert_equal(1, events.size)
1223
+ event = events.first
1224
+ assert_equal(:get, event.method)
1225
+ assert_equal("/v1/charges", event.path)
1226
+ assert_nil(event.http_status)
1227
+ assert(event.duration.positive?)
1228
+ assert_equal(0, event.num_retries)
1229
+ end
1230
+
1231
+ should "pass `user_data` from `request_begin` to `request_end`" do
1232
+ actual_user_data = nil
1233
+
1234
+ Stripe::Instrumentation.subscribe(:request_begin) do |event|
1235
+ event.user_data[:foo] = :bar
1236
+ end
1237
+ Stripe::Instrumentation.subscribe(:request_end) do |event|
1238
+ actual_user_data = event.user_data
1239
+ end
1240
+
1241
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1242
+ .to_return(body: JSON.generate(object: "charge"))
1243
+ Stripe::Charge.list
1244
+
1245
+ assert_equal({ foo: :bar }, actual_user_data)
1246
+ end
1247
+
1248
+ should "provide backward compatibility on `request` topic" do
1249
+ events = []
1250
+ Stripe::Instrumentation.subscribe(:request, :test) { |event| events << event }
1251
+
1252
+ stub_request(:get, "#{Stripe.api_base}/v1/charges")
1253
+ .to_return(body: JSON.generate(object: "charge"))
1254
+ Stripe::Charge.list
1255
+
1256
+ assert_equal(1, events.size)
1257
+ event = events.first
1258
+ assert_equal(:get, event.method)
1259
+ assert_equal("/v1/charges", event.path)
1260
+ assert_equal(200, event.http_status)
1261
+ assert(event.duration.positive?)
1262
+ assert_equal(0, event.num_retries)
1263
+ end
1264
+ end
797
1265
  end
798
1266
 
799
1267
  class SystemProfilerTest < Test::Unit::TestCase