|
1 | 1 | # coding: utf-8 |
2 | 2 |
|
3 | | -# © Copyright IBM Corporation 2021. |
| 3 | +# © Copyright IBM Corporation 2021, 2025. |
4 | 4 | # # |
5 | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
6 | 6 | # you may not use this file except in compliance with the License. |
@@ -86,8 +86,24 @@ def get_authenticate_arguments(req): |
86 | 86 | return req.mock_calls[0] |
87 | 87 |
|
88 | 88 | @staticmethod |
89 | | - def get_request_arguments(tci, srv, idx): |
90 | | - tci.assertEqual(srv.http_client.request.call_count, idx + 1) |
| 89 | + def get_request_arguments(tci, srv, idx, call_count=None): |
| 90 | + """ |
| 91 | + Retrieve the arguments passed to a mocked HTTP request. |
| 92 | +
|
| 93 | + Parameters: |
| 94 | + tci (unittest.mock.Mock): A Mock object used for assertions. |
| 95 | + srv (Service): The instance of CloudantV1 service. |
| 96 | + idx (int): The index of the request in srv.http_client.request.mock_calls. |
| 97 | + call_count (int, optional): The total call count for the request. Defaults to None. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + list: The arguments passed to the HTTP request at the specified index. |
| 101 | +
|
| 102 | + Raises: |
| 103 | + AssertionError: If the actual call count of the request does not match the provided or calculated call_count. |
| 104 | + """ |
| 105 | + call_count = idx + 1 if call_count is None else call_count |
| 106 | + tci.assertEqual(srv.http_client.request.call_count, call_count) |
91 | 107 | return srv.http_client.request.mock_calls[idx] |
92 | 108 |
|
93 | 109 | # Every method tests an authenticator. |
@@ -142,31 +158,27 @@ def test_timeout_cloudantv1_sessionauth(self): |
142 | 158 | ) |
143 | 159 | my_service.set_service_url("http://cloudant.example") |
144 | 160 |
|
145 | | - # Mock out authentication |
146 | | - orig_request = requests.request |
147 | | - requests.request = Mock(return_value=Helpers.get_mocked_response()) |
148 | | - |
149 | 161 | # Mock out request response |
150 | 162 | Helpers.mock_out_cloudant_request(my_service) |
151 | 163 |
|
152 | | - testcases = Helpers.defineTestCases(my_service) |
| 164 | + # Call the server |
| 165 | + my_service.get_server_information() |
153 | 166 |
|
154 | | - for tc_num, tc in enumerate(testcases): |
155 | | - tc['set_timeout'](CUSTOM_TIMEOUT_CONFIG) |
156 | | - |
157 | | - # Call the server |
158 | | - my_service.get_server_information() |
| 167 | + # Assert timeout is set to the authenticator |
| 168 | + auth_args = Helpers.get_request_arguments(self, my_service.authenticator.token_manager, 0, call_count=2) |
| 169 | + self.assertEqual(auth_args.kwargs['timeout'], DEFAULT_TIMEOUT) |
159 | 170 |
|
160 | | - # Assert timeout is set to the authenticator |
161 | | - auth_args = Helpers.get_authenticate_arguments(requests.request) |
162 | | - Helpers.assert_default_timeout_setting(self, auth_args) |
| 171 | + # Assert timeout is set to the server request |
| 172 | + req_args = Helpers.get_request_arguments(self, my_service, 1, call_count=2) |
| 173 | + self.assertEqual(req_args.kwargs['timeout'], DEFAULT_TIMEOUT) |
163 | 174 |
|
164 | | - # Assert timeout is set in the server request |
165 | | - req_args = Helpers.get_request_arguments(self, my_service, tc_num) |
166 | | - tc['assert_func'](self, req_args) |
| 175 | + # Set a custom timeout and repeat the request. Client should be already authenticated. |
| 176 | + my_service.set_http_config(CUSTOM_TIMEOUT_CONFIG) |
| 177 | + my_service.get_server_information() |
167 | 178 |
|
168 | | - # Set back requests.request |
169 | | - requests.request = orig_request |
| 179 | + # Assert the custom timeout is set to the server request |
| 180 | + req_args = Helpers.get_request_arguments(self, my_service, 2, call_count=3) |
| 181 | + self.assertEqual(req_args.kwargs['timeout'], CUSTOM_TIMEOUT) |
170 | 182 |
|
171 | 183 | def test_timeout_cloudantv1_iamauth(self): |
172 | 184 | authenticator = IAMAuthenticator('apikey') |
|
0 commit comments