5454 _remove_certs_for_non_https ,
5555 json_dumps ,
5656)
57+ from tests .conftest import REQUEST_PATH , fake_response
5758
58- REQUEST = "crate.client.http.Server.request"
5959CA_CERT_PATH = certifi .where ()
6060
6161mocked_request = MagicMock (spec = urllib3 .response .HTTPResponse )
@@ -75,14 +75,6 @@ def request(*args, **kwargs):
7575 return request
7676
7777
78- def fake_response (status , reason = None , content_type = "application/json" ):
79- m = MagicMock (spec = urllib3 .response .HTTPResponse )
80- m .status = status
81- m .reason = reason or ""
82- m .headers = {"content-type" : content_type }
83- return m
84-
85-
8678def fake_redirect (location : str ) -> MagicMock :
8779 m = fake_response (307 )
8880 m .get_redirect_location .return_value = location
@@ -117,7 +109,7 @@ def test_connection_reset_exception():
117109
118110 expected_exception_msg = ("No more Servers available,"
119111 " exception from last server: Service Unavailable" )
120- with patch (REQUEST , side_effect = [
112+ with patch (REQUEST_PATH , side_effect = [
121113 fake_response (200 ),
122114 fake_response (104 , "Connection reset by peer" ),
123115 fake_response (503 , "Service Unavailable" ),
@@ -148,7 +140,7 @@ def test_http_error_is_re_raised():
148140 """
149141 client = Client ()
150142
151- with patch (REQUEST , side_effect = Exception ):
143+ with patch (REQUEST_PATH , side_effect = Exception ):
152144 client .sql ("select foo" )
153145 with pytest .raises (ProgrammingError ) as e :
154146 client .sql ("select foo" )
@@ -161,15 +153,15 @@ def test_programming_error_contains_http_error_response_content():
161153 contains the error message from the original error.
162154 """
163155 expected_msg = "this message should appear"
164- with patch (REQUEST , side_effect = Exception (expected_msg )):
156+ with patch (REQUEST_PATH , side_effect = Exception (expected_msg )):
165157 client = Client ()
166158 with pytest .raises (ProgrammingError , match = expected_msg ):
167159 client .sql ("select 1" )
168160
169161
170162def test_connect ():
171163 """
172- Verify the correctness `server` parameter in `Client` instantiation .
164+ Verify the correctness of `server` parameter when `Client` is instantiated .
173165 """
174166 client = Client (servers = "localhost:4200 localhost:4201" )
175167 assert client ._active_servers == \
@@ -187,7 +179,7 @@ def test_redirect_handling():
187179 """
188180 Verify that when a redirect happens, that redirect uri gets added to the server pool.
189181 """
190- with patch (REQUEST , return_value = fake_redirect ("http://localhost:4201" )):
182+ with patch (REQUEST_PATH , return_value = fake_redirect ("http://localhost:4201" )):
191183 client = Client (servers = "localhost:4200" )
192184
193185 # Don't try to print the exception or use `match`, otherwise
@@ -216,7 +208,7 @@ def test_server_infos():
216208 Verify that when a `MaxRetryError` is raised, a `ConnectionError` is raised.
217209 """
218210 error = urllib3 .exceptions .MaxRetryError (None , "/" )
219- with patch (REQUEST , side_effect = error ):
211+ with patch (REQUEST_PATH , side_effect = error ):
220212 client = Client (servers = "localhost:4200 localhost:4201" )
221213 with pytest .raises (ConnectionError ):
222214 client .server_infos ("http://localhost:4200" )
@@ -227,7 +219,7 @@ def test_server_infos_401():
227219 Verify that when a 401 status code is returned, a `ProgrammingError` is raised.
228220 """
229221 response = fake_response (401 , "Unauthorized" , "text/html" )
230- with patch (REQUEST , return_value = response ):
222+ with patch (REQUEST_PATH , return_value = response ):
231223 client = Client (servers = "localhost:4200" )
232224 with pytest .raises (ProgrammingError , match = "401 Client Error: Unauthorized" ):
233225 client .server_infos ("http://localhost:4200" )
@@ -253,7 +245,7 @@ def test_bad_bulk_400():
253245 ).encode ()
254246
255247 client = Client (servers = "localhost:4200" )
256- with patch (REQUEST , return_value = response ):
248+ with patch (REQUEST_PATH , return_value = response ):
257249 with pytest .raises (ProgrammingError , match = 'an error occurred\n another error' ):
258250 client .sql (
259251 "Insert into users (name) values(?)" ,
0 commit comments