1+ from typing import Any
2+ from typing_extensions import Type
13import pytest
24import json
35
46import stripe
57from stripe import _http_client
68from stripe ._encode import _api_encode
9+ from stripe import APIConnectionError
710import urllib3
8- from stripe import util
11+ from stripe import _util
912
1013VALID_API_METHODS = ("get" , "post" , "delete" )
1114
@@ -72,26 +75,26 @@ def mock_max_delay(self, new_value):
7275
7376 def test_sleep_time_exponential_back_off (self ):
7477 client = _http_client .new_default_http_client ()
75- client ._add_jitter_time = lambda t : t
78+ client ._add_jitter_time = lambda sleep_seconds : sleep_seconds
7679 with self .mock_max_delay (10 ):
7780 self .assert_sleep_times (client , [])
7881
7982 def test_initial_delay_as_minimum (self ):
8083 client = _http_client .new_default_http_client ()
81- client ._add_jitter_time = lambda t : t * 0.001
84+ client ._add_jitter_time = lambda sleep_seconds : sleep_seconds * 0.001
8285 initial_delay = _http_client .HTTPClient .INITIAL_DELAY
8386 self .assert_sleep_times (client , [initial_delay ] * 5 )
8487
8588 def test_maximum_delay (self ):
8689 client = _http_client .new_default_http_client ()
87- client ._add_jitter_time = lambda t : t
90+ client ._add_jitter_time = lambda sleep_seconds : sleep_seconds
8891 max_delay = _http_client .HTTPClient .MAX_DELAY
8992 expected = [0.5 , 1.0 , max_delay , max_delay , max_delay ]
9093 self .assert_sleep_times (client , expected )
9194
9295 def test_retry_after_header (self ):
9396 client = _http_client .new_default_http_client ()
94- client ._add_jitter_time = lambda t : t
97+ client ._add_jitter_time = lambda sleep_seconds : sleep_seconds
9598
9699 # Prefer retry-after if it's bigger
97100 assert 30 == client ._sleep_time_seconds (
@@ -109,7 +112,9 @@ def test_retry_after_header(self):
109112 def test_randomness_added (self ):
110113 client = _http_client .new_default_http_client ()
111114 random_value = 0.8
112- client ._add_jitter_time = lambda t : t * random_value
115+ client ._add_jitter_time = (
116+ lambda sleep_seconds : sleep_seconds * random_value
117+ )
113118 base_value = _http_client .HTTPClient .INITIAL_DELAY * random_value
114119
115120 with self .mock_max_delay (10 ):
@@ -240,6 +245,8 @@ class TestClient(_http_client.HTTPClient):
240245
241246
242247class ClientTestBase (object ):
248+ REQUEST_CLIENT : Type [_http_client .HTTPClient ]
249+
243250 @pytest .fixture
244251 def request_mock (self , request_mocks ):
245252 return request_mocks [self .REQUEST_CLIENT .name ]
@@ -342,7 +349,7 @@ def test_request_stream(
342349
343350 def test_exception (self , request_mock , mock_error ):
344351 mock_error (request_mock )
345- with pytest .raises (stripe . error . APIConnectionError ):
352+ with pytest .raises (APIConnectionError ):
346353 self .make_request ("get" , self .valid_url , {}, None )
347354
348355
@@ -352,7 +359,9 @@ def __eq__(self, other):
352359
353360
354361class TestRequestsClient (StripeClientTestCase , ClientTestBase ):
355- REQUEST_CLIENT = _http_client .RequestsClient
362+ REQUEST_CLIENT : Type [
363+ _http_client .RequestsClient
364+ ] = _http_client .RequestsClient
356365
357366 @pytest .fixture
358367 def session (self , mocker , request_mocks ):
@@ -366,7 +375,7 @@ def mock_response(mock, body, code):
366375 result .status_code = code
367376 result .headers = {}
368377 result .raw = urllib3 .response .HTTPResponse (
369- body = util .io .BytesIO (str .encode (body )),
378+ body = _util .io .BytesIO (str .encode (body )),
370379 preload_content = False ,
371380 status = code ,
372381 )
@@ -466,7 +475,7 @@ def response(code=200, headers={}):
466475 result .status_code = code
467476 result .headers = headers
468477 result .raw = urllib3 .response .HTTPResponse (
469- body = util .io .BytesIO (str .encode (result .content )),
478+ body = _util .io .BytesIO (str .encode (result .content )),
470479 preload_content = False ,
471480 status = code ,
472481 )
@@ -530,7 +539,7 @@ def make_client(self):
530539 verify_ssl_certs = True , timeout = 80 , proxy = "http://slap/"
531540 )
532541 # Override sleep time to speed up tests
533- client ._sleep_time = lambda _ : 0.0001
542+ client ._sleep_time_seconds = lambda num_retries , response = None : 0.0001
534543 # Override configured max retries
535544 client ._max_network_retries = lambda : self .max_retries ()
536545 return client
@@ -557,14 +566,14 @@ def test_retry_error_until_exceeded(
557566 self , mock_retry , response , check_call_numbers
558567 ):
559568 mock_retry (retry_error_num = self .max_retries ())
560- with pytest .raises (stripe . error . APIConnectionError ):
569+ with pytest .raises (APIConnectionError ):
561570 self .make_request ()
562571
563572 check_call_numbers (self .max_retries ())
564573
565574 def test_no_retry_error (self , mock_retry , response , check_call_numbers ):
566575 mock_retry (no_retry_error_num = self .max_retries ())
567- with pytest .raises (stripe . error . APIConnectionError ):
576+ with pytest .raises (APIConnectionError ):
568577 self .make_request ()
569578 check_call_numbers (1 )
570579
@@ -594,7 +603,7 @@ def test_retry_request_stream_error_until_exceeded(
594603 self , mock_retry , response , check_call_numbers
595604 ):
596605 mock_retry (retry_error_num = self .max_retries ())
597- with pytest .raises (stripe . error . APIConnectionError ):
606+ with pytest .raises (APIConnectionError ):
598607 self .make_request_stream ()
599608
600609 check_call_numbers (self .max_retries (), is_streaming = True )
@@ -603,7 +612,7 @@ def test_no_retry_request_stream_error(
603612 self , mock_retry , response , check_call_numbers
604613 ):
605614 mock_retry (no_retry_error_num = self .max_retries ())
606- with pytest .raises (stripe . error . APIConnectionError ):
615+ with pytest .raises (APIConnectionError ):
607616 self .make_request_stream ()
608617 check_call_numbers (1 , is_streaming = True )
609618
@@ -628,7 +637,7 @@ def connection_error(self, session):
628637 client = self .REQUEST_CLIENT ()
629638
630639 def connection_error (given_exception ):
631- with pytest .raises (stripe . error . APIConnectionError ) as error :
640+ with pytest .raises (APIConnectionError ) as error :
632641 client ._handle_request_error (given_exception )
633642 return error .value
634643
@@ -686,6 +695,7 @@ def mock_response(mock, body, code):
686695 result .headers = {}
687696
688697 mock .fetch = mocker .Mock (return_value = result )
698+ return result
689699
690700 return mock_response
691701
@@ -715,7 +725,11 @@ def check_call(
715725
716726
717727class TestUrllib2Client (StripeClientTestCase , ClientTestBase ):
718- REQUEST_CLIENT = _http_client .Urllib2Client
728+ REQUEST_CLIENT : Type [
729+ _http_client .Urllib2Client
730+ ] = _http_client .Urllib2Client
731+
732+ request_object : Any
719733
720734 def make_client (self , proxy ):
721735 self .client = self .REQUEST_CLIENT (verify_ssl_certs = True , proxy = proxy )
@@ -822,7 +836,7 @@ def make_request_stream(self, method, url, headers, post_data, proxy=None):
822836
823837
824838class TestPycurlClient (StripeClientTestCase , ClientTestBase ):
825- REQUEST_CLIENT = _http_client .PycurlClient
839+ REQUEST_CLIENT : Type [ _http_client . PycurlClient ] = _http_client .PycurlClient
826840
827841 def make_client (self , proxy ):
828842 self .client = self .REQUEST_CLIENT (verify_ssl_certs = True , proxy = proxy )
0 commit comments