|
15 | 15 | import pytest |
16 | 16 |
|
17 | 17 | from google.api_core import universe |
| 18 | +from google.auth.exceptions import MutualTLSChannelError |
18 | 19 |
|
19 | 20 |
|
20 | 21 | class _Fake_Credentials: |
@@ -62,3 +63,190 @@ def test_compare_domains(): |
62 | 63 | universe.compare_domains(fake_domain, _Fake_Credentials(another_fake_domain)) |
63 | 64 | assert str(excinfo.value).find(fake_domain) >= 0 |
64 | 65 | assert str(excinfo.value).find(another_fake_domain) >= 0 |
| 66 | + |
| 67 | + |
| 68 | +def test_get_universe_domain(): |
| 69 | + # When universe_domain is provided |
| 70 | + assert ( |
| 71 | + universe.get_universe_domain("foo.com", default_universe="default.com") |
| 72 | + == "foo.com" |
| 73 | + ) |
| 74 | + assert ( |
| 75 | + universe.get_universe_domain(" foo.com ", default_universe="default.com") |
| 76 | + == "foo.com" |
| 77 | + ) |
| 78 | + |
| 79 | + # When universe_domain is None, falls back to default_universe |
| 80 | + assert ( |
| 81 | + universe.get_universe_domain(None, default_universe="default.com") |
| 82 | + == "default.com" |
| 83 | + ) |
| 84 | + |
| 85 | + # When multiple potential universes are provided, resolves in order of preference |
| 86 | + assert ( |
| 87 | + universe.get_universe_domain( |
| 88 | + "foo.com", "bar.com", default_universe="default.com" |
| 89 | + ) |
| 90 | + == "foo.com" |
| 91 | + ) |
| 92 | + assert ( |
| 93 | + universe.get_universe_domain(None, "bar.com", default_universe="default.com") |
| 94 | + == "bar.com" |
| 95 | + ) |
| 96 | + assert ( |
| 97 | + universe.get_universe_domain(None, None, default_universe="default.com") |
| 98 | + == "default.com" |
| 99 | + ) |
| 100 | + |
| 101 | + # EmptyUniverseError raised when resolved value is empty string |
| 102 | + with pytest.raises(universe.EmptyUniverseError) as excinfo: |
| 103 | + universe.get_universe_domain("", default_universe="default.com") |
| 104 | + assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
| 105 | + |
| 106 | + with pytest.raises(universe.EmptyUniverseError) as excinfo: |
| 107 | + universe.get_universe_domain(" ", default_universe="default.com") |
| 108 | + assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
| 109 | + |
| 110 | + with pytest.raises(universe.EmptyUniverseError) as excinfo: |
| 111 | + universe.get_universe_domain(None, "", default_universe="default.com") |
| 112 | + assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
| 113 | + |
| 114 | + |
| 115 | +def test_get_default_mtls_endpoint(): |
| 116 | + # Test valid API endpoints |
| 117 | + assert universe.get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com" |
| 118 | + assert ( |
| 119 | + universe.get_default_mtls_endpoint("foo.sandbox.googleapis.com") |
| 120 | + == "foo.mtls.sandbox.googleapis.com" |
| 121 | + ) |
| 122 | + # Test case-insensitivity |
| 123 | + assert universe.get_default_mtls_endpoint("foo.GoogleAPIs.com") == "foo.mtls.googleapis.com" |
| 124 | + assert ( |
| 125 | + universe.get_default_mtls_endpoint("foo.Sandbox.GoogleAPIs.com") |
| 126 | + == "foo.mtls.sandbox.googleapis.com" |
| 127 | + ) |
| 128 | + |
| 129 | + # Test valid API endpoints with schemes |
| 130 | + assert ( |
| 131 | + universe.get_default_mtls_endpoint("https://foo.googleapis.com") |
| 132 | + == "https://foo.mtls.googleapis.com" |
| 133 | + ) |
| 134 | + assert ( |
| 135 | + universe.get_default_mtls_endpoint("http://foo.googleapis.com:8080/v1") |
| 136 | + == "http://foo.mtls.googleapis.com:8080/v1" |
| 137 | + ) |
| 138 | + |
| 139 | + # Test valid API endpoints with ports |
| 140 | + assert ( |
| 141 | + universe.get_default_mtls_endpoint("foo.googleapis.com:443") |
| 142 | + == "foo.mtls.googleapis.com:443" |
| 143 | + ) |
| 144 | + assert ( |
| 145 | + universe.get_default_mtls_endpoint("foo.sandbox.googleapis.com:443") |
| 146 | + == "foo.mtls.sandbox.googleapis.com:443" |
| 147 | + ) |
| 148 | + # Test case-insensitivity with ports |
| 149 | + assert ( |
| 150 | + universe.get_default_mtls_endpoint("foo.GoogleAPIs.com:443") |
| 151 | + == "foo.mtls.googleapis.com:443" |
| 152 | + ) |
| 153 | + assert ( |
| 154 | + universe.get_default_mtls_endpoint("foo.Sandbox.GoogleAPIs.com:443") |
| 155 | + == "foo.mtls.sandbox.googleapis.com:443" |
| 156 | + ) |
| 157 | + |
| 158 | + # Test endpoints that shouldn't be converted |
| 159 | + assert ( |
| 160 | + universe.get_default_mtls_endpoint("foo.mtls.googleapis.com") |
| 161 | + == "foo.mtls.googleapis.com" |
| 162 | + ) |
| 163 | + assert universe.get_default_mtls_endpoint("foo.com") == "foo.com" |
| 164 | + assert universe.get_default_mtls_endpoint("foo.com:8080") == "foo.com:8080" |
| 165 | + |
| 166 | + # Test empty/None endpoints |
| 167 | + assert universe.get_default_mtls_endpoint("") == "" |
| 168 | + assert universe.get_default_mtls_endpoint(None) is None |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.parametrize( |
| 172 | + "api_override,universe_domain,default_universe,default_mtls_endpoint,default_endpoint_template,use_mtls,expected", |
| 173 | + [ |
| 174 | + ( |
| 175 | + "foo.com", |
| 176 | + "googleapis.com", |
| 177 | + "googleapis.com", |
| 178 | + "foo.mtls.googleapis.com", |
| 179 | + "foo.{UNIVERSE_DOMAIN}", |
| 180 | + True, |
| 181 | + "foo.com", |
| 182 | + ), |
| 183 | + ( |
| 184 | + None, |
| 185 | + "googleapis.com", |
| 186 | + "googleapis.com", |
| 187 | + "foo.mtls.googleapis.com", |
| 188 | + "foo.{UNIVERSE_DOMAIN}", |
| 189 | + True, |
| 190 | + "foo.mtls.googleapis.com", |
| 191 | + ), |
| 192 | + ( |
| 193 | + None, |
| 194 | + "googleapis.com", |
| 195 | + "googleapis.com", |
| 196 | + "foo.mtls.googleapis.com", |
| 197 | + "foo.{UNIVERSE_DOMAIN}", |
| 198 | + False, |
| 199 | + "foo.googleapis.com", |
| 200 | + ), |
| 201 | + ( |
| 202 | + None, |
| 203 | + "bar.com", |
| 204 | + "googleapis.com", |
| 205 | + "foo.mtls.googleapis.com", |
| 206 | + "foo.{UNIVERSE_DOMAIN}", |
| 207 | + True, |
| 208 | + MutualTLSChannelError, |
| 209 | + ), |
| 210 | + ( |
| 211 | + None, |
| 212 | + "googleapis.com", |
| 213 | + "googleapis.com", |
| 214 | + None, |
| 215 | + "foo.{UNIVERSE_DOMAIN}", |
| 216 | + True, |
| 217 | + ValueError, |
| 218 | + ), |
| 219 | + ], |
| 220 | +) |
| 221 | +def test_get_api_endpoint( |
| 222 | + api_override, |
| 223 | + universe_domain, |
| 224 | + default_universe, |
| 225 | + default_mtls_endpoint, |
| 226 | + default_endpoint_template, |
| 227 | + use_mtls, |
| 228 | + expected, |
| 229 | +): |
| 230 | + if isinstance(expected, type) and issubclass(expected, Exception): |
| 231 | + with pytest.raises(expected): |
| 232 | + universe.get_api_endpoint( |
| 233 | + api_override, |
| 234 | + universe_domain, |
| 235 | + default_universe, |
| 236 | + default_mtls_endpoint, |
| 237 | + default_endpoint_template, |
| 238 | + use_mtls, |
| 239 | + ) |
| 240 | + else: |
| 241 | + assert ( |
| 242 | + universe.get_api_endpoint( |
| 243 | + api_override, |
| 244 | + universe_domain, |
| 245 | + default_universe, |
| 246 | + default_mtls_endpoint, |
| 247 | + default_endpoint_template, |
| 248 | + use_mtls, |
| 249 | + ) |
| 250 | + == expected |
| 251 | + ) |
| 252 | + |
0 commit comments