Skip to content

Commit 487db45

Browse files
committed
refactor(api-core): update routing tests to match generator's test template precisely
1 parent da2cf50 commit 487db45

1 file changed

Lines changed: 123 additions & 109 deletions

File tree

packages/google-api-core/tests/unit/gapic/test_routing.py

Lines changed: 123 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
)
2727

2828

29+
class MockClient:
30+
_DEFAULT_UNIVERSE = "googleapis.com"
31+
DEFAULT_MTLS_ENDPOINT = "foo.mtls.googleapis.com"
32+
_DEFAULT_ENDPOINT_TEMPLATE = "foo.{UNIVERSE_DOMAIN}"
33+
34+
2935
def test_get_default_mtls_endpoint():
3036
# Test valid API endpoints
3137
assert get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com"
@@ -46,130 +52,138 @@ def test_get_default_mtls_endpoint():
4652
assert get_default_mtls_endpoint(None) is None
4753

4854

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}",
55+
def test__get_api_endpoint():
56+
api_override = "foo.com"
57+
mock_client_cert_source = mock.Mock()
58+
default_universe = MockClient._DEFAULT_UNIVERSE
59+
default_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format(
60+
UNIVERSE_DOMAIN=default_universe
6061
)
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}",
62+
mock_universe = "bar.com"
63+
mock_endpoint = MockClient._DEFAULT_ENDPOINT_TEMPLATE.format(
64+
UNIVERSE_DOMAIN=mock_universe
7465
)
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}",
66+
67+
assert (
68+
get_api_endpoint(
69+
api_override,
70+
mock_client_cert_source,
71+
default_universe,
72+
"always",
73+
MockClient._DEFAULT_UNIVERSE,
74+
MockClient.DEFAULT_MTLS_ENDPOINT,
75+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
76+
)
77+
== api_override
8878
)
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}",
79+
assert (
80+
get_api_endpoint(
81+
None,
82+
mock_client_cert_source,
83+
default_universe,
84+
"auto",
85+
MockClient._DEFAULT_UNIVERSE,
86+
MockClient.DEFAULT_MTLS_ENDPOINT,
87+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
88+
)
89+
== MockClient.DEFAULT_MTLS_ENDPOINT
10290
)
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"):
91+
assert (
10992
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}",
93+
None,
94+
None,
95+
default_universe,
96+
"auto",
97+
MockClient._DEFAULT_UNIVERSE,
98+
MockClient.DEFAULT_MTLS_ENDPOINT,
99+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
117100
)
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}",
101+
== default_endpoint
130102
)
131-
assert endpoint == "foo.mtls.googleapis.com"
132-
133-
134-
def test_get_universe_domain():
135-
# client_universe_domain takes precedence
136103
assert (
137-
get_universe_domain("client.com", "env.com", "default.com") # noqa: E501
138-
== "client.com"
104+
get_api_endpoint(
105+
None,
106+
None,
107+
default_universe,
108+
"always",
109+
MockClient._DEFAULT_UNIVERSE,
110+
MockClient.DEFAULT_MTLS_ENDPOINT,
111+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
112+
)
113+
== MockClient.DEFAULT_MTLS_ENDPOINT
139114
)
140-
141-
# env takes precedence over default
142115
assert (
143-
get_universe_domain(None, "env.com", "default.com") == "env.com" # noqa: E501
116+
get_api_endpoint(
117+
None,
118+
mock_client_cert_source,
119+
default_universe,
120+
"always",
121+
MockClient._DEFAULT_UNIVERSE,
122+
MockClient.DEFAULT_MTLS_ENDPOINT,
123+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
124+
)
125+
== MockClient.DEFAULT_MTLS_ENDPOINT
126+
)
127+
assert (
128+
get_api_endpoint(
129+
None,
130+
None,
131+
mock_universe,
132+
"never",
133+
MockClient._DEFAULT_UNIVERSE,
134+
MockClient.DEFAULT_MTLS_ENDPOINT,
135+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
136+
)
137+
== mock_endpoint
144138
)
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
152139
assert (
153-
get_universe_domain(" client.com ", "env.com", "default.com") == "client.com"
140+
get_api_endpoint(
141+
None,
142+
None,
143+
default_universe,
144+
"never",
145+
MockClient._DEFAULT_UNIVERSE,
146+
MockClient.DEFAULT_MTLS_ENDPOINT,
147+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
148+
)
149+
== default_endpoint
154150
)
155-
assert get_universe_domain(None, " env.com ", "default.com") == "env.com"
156151

152+
with pytest.raises(MutualTLSChannelError) as excinfo:
153+
get_api_endpoint(
154+
None,
155+
mock_client_cert_source,
156+
mock_universe,
157+
"auto",
158+
MockClient._DEFAULT_UNIVERSE,
159+
MockClient.DEFAULT_MTLS_ENDPOINT,
160+
MockClient._DEFAULT_ENDPOINT_TEMPLATE,
161+
)
162+
assert (
163+
str(excinfo.value)
164+
== "mTLS is not supported in any universe other than googleapis.com."
165+
)
157166

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")
163167

168+
def test__get_universe_domain():
169+
client_universe_domain = "foo.com"
170+
universe_domain_env = "bar.com"
164171

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,
172+
assert (
173+
get_universe_domain(
174+
client_universe_domain, universe_domain_env, MockClient._DEFAULT_UNIVERSE
175+
)
176+
== client_universe_domain
174177
)
175-
assert endpoint is None
178+
assert (
179+
get_universe_domain(None, universe_domain_env, MockClient._DEFAULT_UNIVERSE)
180+
== universe_domain_env
181+
)
182+
assert (
183+
get_universe_domain(None, None, MockClient._DEFAULT_UNIVERSE)
184+
== MockClient._DEFAULT_UNIVERSE
185+
)
186+
187+
with pytest.raises(ValueError) as excinfo:
188+
get_universe_domain("", None, MockClient._DEFAULT_UNIVERSE)
189+
assert str(excinfo.value) == "Universe Domain cannot be an empty string."

0 commit comments

Comments
 (0)