@@ -78,6 +78,7 @@ def test_do_request_with_authenticator_should_succeed(
7878 mock_request .status_code = 200
7979 mock_request .headers = mock_authentication_headers
8080 mock_request .auth = mock_auth
81+
8182 mock_get .return_value = mock_request
8283
8384 actual = http_service .do_request ("GET" )
@@ -173,6 +174,7 @@ def test_make_target_url_should_succeed_with_only_base_url(
173174def test_make_target_url_should_work_with_params (
174175 http_service : HttpService ,
175176 base_url : str ,
177+ mock_url : str ,
176178 mock_sub_route : str ,
177179 mock_params : Dict [str , Any ],
178180) -> None :
@@ -181,3 +183,85 @@ def test_make_target_url_should_work_with_params(
181183 actual = http_service .make_target_url (sub_route = mock_sub_route , params = mock_params )
182184
183185 assert expected == actual
186+
187+
188+ @patch (PATCHED_MODULE )
189+ def test_do_request_should_use_mtls_cert_when_enabled (
190+ mock_request : MagicMock ,
191+ mock_url : str ,
192+ ) -> None :
193+ api_service = HttpService (
194+ base_url = mock_url ,
195+ timeout = 10 ,
196+ backoff = 0.1 ,
197+ retries = 1 ,
198+ mtls_cert = "test.crt" ,
199+ mtls_key = "test.key" ,
200+ mtls_ca = "test.ca"
201+ )
202+
203+ mock_response = MagicMock ()
204+ mock_response .status_code = 200
205+ mock_request .return_value = mock_response
206+
207+ api_service .do_request ("GET" , mock_url )
208+
209+ mock_request .assert_called_once ()
210+ call_kwargs = mock_request .call_args [1 ]
211+ assert call_kwargs ["cert" ] == ("test.crt" , "test.key" )
212+ assert call_kwargs ["verify" ] == "test.ca"
213+
214+
215+ @patch (PATCHED_MODULE )
216+ def test_do_request_should_use_ca_file_for_verification_when_provided (
217+ mock_request : MagicMock ,
218+ mock_url : str ,
219+ ) -> None :
220+ api_service = HttpService (
221+ base_url = mock_url ,
222+ timeout = 10 ,
223+ backoff = 0.1 ,
224+ retries = 1 ,
225+ mtls_cert = "test.crt" ,
226+ mtls_key = "test.key" ,
227+ mtls_ca = "ca.crt"
228+ )
229+
230+ mock_response = MagicMock ()
231+ mock_response .status_code = 200
232+ mock_request .return_value = mock_response
233+
234+ api_service .do_request ("GET" , mock_url )
235+
236+ mock_request .assert_called_once ()
237+ call_kwargs = mock_request .call_args [1 ]
238+ assert call_kwargs ["cert" ] == ("test.crt" , "test.key" )
239+ assert call_kwargs ["verify" ] == "ca.crt"
240+
241+
242+ @patch (PATCHED_MODULE )
243+ def test_do_request_should_not_use_cert_when_mtls_disabled (
244+ mock_request : MagicMock ,
245+ mock_url : str ,
246+ ) -> None :
247+ api_service = HttpService (
248+ base_url = mock_url ,
249+ timeout = 10 ,
250+ backoff = 0.1 ,
251+ retries = 1 ,
252+ mtls_cert = None ,
253+ mtls_key = None ,
254+ mtls_ca = None
255+ )
256+
257+ mock_response = MagicMock ()
258+ mock_response .status_code = 200
259+ mock_request .return_value = mock_response
260+
261+ api_service .do_request ("GET" , mock_url )
262+
263+ mock_request .assert_called_once ()
264+ call_kwargs = mock_request .call_args [1 ]
265+ assert call_kwargs ["cert" ] is None
266+ assert call_kwargs ["verify" ] is True
267+
0 commit comments