|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from unittest import mock |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from google.auth.exceptions import MutualTLSChannelError |
| 21 | + |
| 22 | +from google.api_core.gapic_v1.routing import ( |
| 23 | + get_api_endpoint, |
| 24 | + get_default_mtls_endpoint, |
| 25 | + get_universe_domain, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def test_get_default_mtls_endpoint(): |
| 30 | + # Test valid API endpoints |
| 31 | + assert get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com" |
| 32 | + assert ( |
| 33 | + get_default_mtls_endpoint("foo.sandbox.googleapis.com") |
| 34 | + == "foo.mtls.sandbox.googleapis.com" |
| 35 | + ) |
| 36 | + |
| 37 | + # Test endpoints that shouldn't be converted |
| 38 | + assert ( |
| 39 | + get_default_mtls_endpoint("foo.mtls.googleapis.com") |
| 40 | + == "foo.mtls.googleapis.com" |
| 41 | + ) |
| 42 | + assert get_default_mtls_endpoint("foo.com") == "foo.com" |
| 43 | + |
| 44 | + # Test empty/None endpoints |
| 45 | + assert get_default_mtls_endpoint("") == "" |
| 46 | + assert get_default_mtls_endpoint(None) is None |
| 47 | + |
| 48 | + |
| 49 | +def test_get_api_endpoint_override(): |
| 50 | + # If api_override is provided, it should be returned |
| 51 | + # regardless of other args |
| 52 | + endpoint = get_api_endpoint( |
| 53 | + api_override="custom.endpoint.com", |
| 54 | + client_cert_source=None, |
| 55 | + universe_domain="googleapis.com", |
| 56 | + use_mtls_endpoint="auto", |
| 57 | + default_universe="googleapis.com", |
| 58 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 59 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 60 | + ) |
| 61 | + assert endpoint == "custom.endpoint.com" |
| 62 | + |
| 63 | + |
| 64 | +def test_get_api_endpoint_mtls_always(): |
| 65 | + # use_mtls_endpoint == "always" should use the default mtls endpoint |
| 66 | + endpoint = get_api_endpoint( |
| 67 | + api_override=None, |
| 68 | + client_cert_source=None, |
| 69 | + universe_domain="googleapis.com", |
| 70 | + use_mtls_endpoint="always", |
| 71 | + default_universe="googleapis.com", |
| 72 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 73 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 74 | + ) |
| 75 | + assert endpoint == "foo.mtls.googleapis.com" |
| 76 | + |
| 77 | + |
| 78 | +def test_get_api_endpoint_mtls_auto_with_cert(): |
| 79 | + # "auto" with client_cert_source should use mtls |
| 80 | + endpoint = get_api_endpoint( |
| 81 | + api_override=None, |
| 82 | + client_cert_source=mock.Mock(), |
| 83 | + universe_domain="googleapis.com", |
| 84 | + use_mtls_endpoint="auto", |
| 85 | + default_universe="googleapis.com", |
| 86 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 87 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 88 | + ) |
| 89 | + assert endpoint == "foo.mtls.googleapis.com" |
| 90 | + |
| 91 | + |
| 92 | +def test_get_api_endpoint_mtls_auto_no_cert(): |
| 93 | + # "auto" without client_cert_source should use the default template |
| 94 | + endpoint = get_api_endpoint( |
| 95 | + api_override=None, |
| 96 | + client_cert_source=None, |
| 97 | + universe_domain="googleapis.com", |
| 98 | + use_mtls_endpoint="auto", |
| 99 | + default_universe="googleapis.com", |
| 100 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 101 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 102 | + ) |
| 103 | + assert endpoint == "foo.googleapis.com" |
| 104 | + |
| 105 | + |
| 106 | +def test_get_api_endpoint_mtls_universe_mismatch(): |
| 107 | + # mTLS is only supported in the default universe |
| 108 | + with pytest.raises(MutualTLSChannelError, match="mTLS is not supported"): |
| 109 | + get_api_endpoint( |
| 110 | + api_override=None, |
| 111 | + client_cert_source=mock.Mock(), |
| 112 | + universe_domain="custom-universe.com", |
| 113 | + use_mtls_endpoint="auto", |
| 114 | + default_universe="googleapis.com", |
| 115 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 116 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def test_get_api_endpoint_mtls_case_insensitive(): |
| 121 | + # mTLS universe check should be case insensitive |
| 122 | + endpoint = get_api_endpoint( |
| 123 | + api_override=None, |
| 124 | + client_cert_source=mock.Mock(), |
| 125 | + universe_domain="GOOGLEAPIS.COM", |
| 126 | + use_mtls_endpoint="auto", |
| 127 | + default_universe="googleapis.com", |
| 128 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 129 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 130 | + ) |
| 131 | + assert endpoint == "foo.mtls.googleapis.com" |
| 132 | + |
| 133 | + |
| 134 | +def test_get_universe_domain(): |
| 135 | + # client_universe_domain takes precedence |
| 136 | + assert ( |
| 137 | + get_universe_domain("client.com", "env.com", "default.com") # noqa: E501 |
| 138 | + == "client.com" |
| 139 | + ) |
| 140 | + |
| 141 | + # env takes precedence over default |
| 142 | + assert ( |
| 143 | + get_universe_domain(None, "env.com", "default.com") == "env.com" # noqa: E501 |
| 144 | + ) |
| 145 | + |
| 146 | + # fallback to default |
| 147 | + assert get_universe_domain(None, None, "default.com") == "default.com" # noqa: E501 |
| 148 | + |
| 149 | + |
| 150 | +def test_get_universe_domain_strip(): |
| 151 | + # check that whitespace is stripped |
| 152 | + assert ( |
| 153 | + get_universe_domain(" client.com ", "env.com", "default.com") == "client.com" |
| 154 | + ) |
| 155 | + assert get_universe_domain(None, " env.com ", "default.com") == "env.com" |
| 156 | + |
| 157 | + |
| 158 | +def test_get_universe_domain_empty(): |
| 159 | + with pytest.raises(ValueError, match="cannot be an empty string"): |
| 160 | + get_universe_domain("", None, "default.com") |
| 161 | + with pytest.raises(ValueError, match="cannot be an empty string"): |
| 162 | + get_universe_domain(" ", None, "default.com") |
| 163 | + |
| 164 | + |
| 165 | +def test_get_api_endpoint_none_template(): |
| 166 | + endpoint = get_api_endpoint( |
| 167 | + api_override=None, |
| 168 | + client_cert_source=None, |
| 169 | + universe_domain="googleapis.com", |
| 170 | + use_mtls_endpoint="never", |
| 171 | + default_universe="googleapis.com", |
| 172 | + default_mtls_endpoint=None, |
| 173 | + default_endpoint_template=None, |
| 174 | + ) |
| 175 | + assert endpoint is None |
0 commit comments