33from requests import Response
44from prefab_cloud_python ._requests import ApiClient
55
6+
67# Dummy options for testing.
78class DummyOptions :
89 prefab_api_urls = ["https://a.example.com" , "https://b.example.com" ]
910 version = "1.0"
1011
12+
1113class TestApiClient (unittest .TestCase ):
1214 def setUp (self ):
1315 self .options = DummyOptions ()
@@ -16,7 +18,13 @@ def setUp(self):
1618 # patch _get_attempt_number to always return 1.
1719 self .client ._get_attempt_number = lambda : 1
1820
19- def create_response (self , status_code = 200 , content = b"dummy" , headers = None , url = "https://a.example.com/api/v1/configs/0" ):
21+ def create_response (
22+ self ,
23+ status_code = 200 ,
24+ content = b"dummy" ,
25+ headers = None ,
26+ url = "https://a.example.com/api/v1/configs/0" ,
27+ ):
2028 resp = Response ()
2129 resp .status_code = status_code
2230 resp ._content = content
@@ -31,7 +39,7 @@ def test_no_cache(self, mock_send_request):
3139 status_code = 200 ,
3240 content = b"response_no_cache" ,
3341 headers = {"Cache-Control" : "max-age=60" , "ETag" : "abc" },
34- url = "https://a.example.com/api/v1/configs/0"
42+ url = "https://a.example.com/api/v1/configs/0" ,
3543 )
3644 mock_send_request .return_value = response
3745
@@ -47,7 +55,7 @@ def test_cache_miss_and_hit(self, mock_send_request):
4755 status_code = 200 ,
4856 content = b"cached_response" ,
4957 headers = {"Cache-Control" : "max-age=60" , "ETag" : "abc" },
50- url = "https://a.example.com/api/v1/configs/0"
58+ url = "https://a.example.com/api/v1/configs/0" ,
5159 )
5260 mock_send_request .return_value = response
5361
@@ -61,7 +69,7 @@ def test_cache_miss_and_hit(self, mock_send_request):
6169 status_code = 200 ,
6270 content = b"new_response" ,
6371 headers = {"Cache-Control" : "max-age=60" , "ETag" : "def" },
64- url = "https://a.example.com/api/v1/configs/0"
72+ url = "https://a.example.com/api/v1/configs/0" ,
6573 )
6674 mock_send_request .return_value = new_response
6775
@@ -78,7 +86,7 @@ def test_304_returns_cached_response(self, mock_send_request):
7886 status_code = 200 ,
7987 content = b"cached_response" ,
8088 headers = {"Cache-Control" : "max-age=60" , "ETag" : "abc" },
81- url = "https://a.example.com/api/v1/configs/0"
89+ url = "https://a.example.com/api/v1/configs/0" ,
8290 )
8391 mock_send_request .return_value = response
8492 resp1 = self .client .resilient_request ("/api/v1/configs/0" , allow_cache = True )
@@ -91,13 +99,14 @@ def test_304_returns_cached_response(self, mock_send_request):
9199 status_code = 304 ,
92100 content = b"" ,
93101 headers = {},
94- url = "https://a.example.com/api/v1/configs/0"
102+ url = "https://a.example.com/api/v1/configs/0" ,
95103 )
96104 mock_send_request .return_value = response_304
97105 resp2 = self .client .resilient_request ("/api/v1/configs/0" , allow_cache = True )
98106 self .assertEqual (resp2 .status_code , 200 )
99107 self .assertEqual (resp2 .content , b"cached_response" )
100108 self .assertEqual (resp2 .headers .get ("X-Cache" ), "HIT" )
101109
110+
102111if __name__ == "__main__" :
103- unittest .main ()
112+ unittest .main ()
0 commit comments