|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import json |
| 16 | +import os |
16 | 17 | import unittest |
17 | 18 | import warnings |
18 | 19 |
|
@@ -165,6 +166,7 @@ def _make_mock_one(self, *args, **kw): |
165 | 166 | class MockConnection(self._get_target_class()): |
166 | 167 | API_URL_TEMPLATE = "{api_base_url}/mock/{api_version}{path}" |
167 | 168 | API_BASE_URL = "http://mock" |
| 169 | + API_BASE_MTLS_URL = "https://mock.mtls" |
168 | 170 | API_VERSION = "vMOCK" |
169 | 171 |
|
170 | 172 | return MockConnection(*args, **kw) |
@@ -230,6 +232,50 @@ def test_build_api_url_w_extra_query_params_tuples(self): |
230 | 232 | self.assertEqual(parms["qux"], ["quux", "corge"]) |
231 | 233 | self.assertEqual(parms["prettyPrint"], ["false"]) |
232 | 234 |
|
| 235 | + def test_get_api_base_url_for_mtls_w_api_base_url(self): |
| 236 | + client = object() |
| 237 | + conn = self._make_mock_one(client) |
| 238 | + uri = conn.get_api_base_url_for_mtls(api_base_url="http://foo") |
| 239 | + self.assertEqual(uri, "http://foo") |
| 240 | + |
| 241 | + def test_get_api_base_url_for_mtls_env_always(self): |
| 242 | + client = object() |
| 243 | + conn = self._make_mock_one(client) |
| 244 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): |
| 245 | + uri = conn.get_api_base_url_for_mtls() |
| 246 | + self.assertEqual(uri, "https://mock.mtls") |
| 247 | + |
| 248 | + def test_get_api_base_url_for_mtls_env_never(self): |
| 249 | + client = object() |
| 250 | + conn = self._make_mock_one(client) |
| 251 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): |
| 252 | + uri = conn.get_api_base_url_for_mtls() |
| 253 | + self.assertEqual(uri, "http://mock") |
| 254 | + |
| 255 | + def test_get_api_base_url_for_mtls_env_auto(self): |
| 256 | + client = mock.Mock() |
| 257 | + client._http = mock.Mock() |
| 258 | + client._http.is_mtls = False |
| 259 | + conn = self._make_mock_one(client) |
| 260 | + |
| 261 | + # ALLOW_AUTO_SWITCH_TO_MTLS_URL is False, so use regular endpoint. |
| 262 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}): |
| 263 | + uri = conn.get_api_base_url_for_mtls() |
| 264 | + self.assertEqual(uri, "http://mock") |
| 265 | + |
| 266 | + # ALLOW_AUTO_SWITCH_TO_MTLS_URL is True, so now endpoint dependes |
| 267 | + # on client._http.is_mtls |
| 268 | + conn.ALLOW_AUTO_SWITCH_TO_MTLS_URL = True |
| 269 | + |
| 270 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}): |
| 271 | + uri = conn.get_api_base_url_for_mtls() |
| 272 | + self.assertEqual(uri, "http://mock") |
| 273 | + |
| 274 | + client._http.is_mtls = True |
| 275 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}): |
| 276 | + uri = conn.get_api_base_url_for_mtls() |
| 277 | + self.assertEqual(uri, "https://mock.mtls") |
| 278 | + |
233 | 279 | def test__make_request_no_data_no_content_type_no_headers(self): |
234 | 280 | from google.cloud._http import CLIENT_INFO_HEADER |
235 | 281 |
|
|
0 commit comments