diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java index beb2c9260..5b4fea0ec 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java @@ -25,6 +25,7 @@ import com.google.common.annotations.VisibleForTesting; import java.io.IOException; import java.util.logging.Logger; +import java.util.regex.Pattern; /** * Abstract thread-safe Google client. @@ -405,6 +406,13 @@ public abstract static class Builder { /** User configured Universe Domain. Defaults to `googleapis.com`. */ String universeDomain; + /** + * Regex pattern to check if the URL passed in matches the default endpoint confgured from a + * discovery doc. Follows the format of `https://{serviceName}(.mtls).googleapis.com/` + */ + Pattern defaultEndpointRegex = + Pattern.compile("https://[a-zA-Z]*(\\.mtls)?\\.googleapis.com/?"); + /** * Whether the user has configured an endpoint via {@link #setRootUrl(String)}. This is added in * because the rootUrl is set in the Builder's constructor. , @@ -443,8 +451,7 @@ protected Builder( this.servicePath = normalizeServicePath(servicePath); this.httpRequestInitializer = httpRequestInitializer; this.serviceName = parseServiceName(rootUrl); - this.isUserConfiguredEndpoint = - !this.rootUrl.endsWith(Credentials.GOOGLE_DEFAULT_UNIVERSE + "/"); + this.isUserConfiguredEndpoint = !defaultEndpointRegex.matcher(this.rootUrl).matches(); } /** diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java index 8676ad98d..6145f7e69 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java @@ -289,6 +289,15 @@ public void testInitialize() throws Exception { assertTrue(remoteRequestInitializer.isCalled); } + @Test + public void testParseServiceName_nonMtlsRootUrl() { + AbstractGoogleClient.Builder clientBuilder = + new MockGoogleClient.Builder( + TRANSPORT, "https://random.googleapis.com/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application"); + assertEquals(clientBuilder.getServiceName(), "random"); + } + @Test public void testParseServiceName_mtlsRootUrl() { AbstractGoogleClient.Builder clientBuilder = @@ -299,21 +308,52 @@ public void testParseServiceName_mtlsRootUrl() { } @Test - public void testParseServiceName_nonMtlsRootUrl() { + public void testParseServiceName_nonGDURootUrl() { + AbstractGoogleClient.Builder clientBuilder = + new MockGoogleClient.Builder( + TRANSPORT, "https://test.random.com/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application"); + assertNull(clientBuilder.getServiceName()); + } + + @Test + public void testIsUserSetEndpoint_nonMtlsRootUrl() { AbstractGoogleClient.Builder clientBuilder = new MockGoogleClient.Builder( TRANSPORT, "https://random.googleapis.com/", "", JSON_OBJECT_PARSER, null) .setApplicationName("Test Application"); - assertEquals(clientBuilder.getServiceName(), "random"); + assertFalse(clientBuilder.isUserConfiguredEndpoint); } @Test - public void testParseServiceName_nonGDURootUrl() { + public void testIsUserSetEndpoint_mtlsRootUrl() { + AbstractGoogleClient.Builder clientBuilder = + new MockGoogleClient.Builder( + TRANSPORT, "https://test.mtls.googleapis.com/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application"); + assertFalse(clientBuilder.isUserConfiguredEndpoint); + } + + @Test + public void testIsUserSetEndpoint_nonGDURootUrl() { AbstractGoogleClient.Builder clientBuilder = new MockGoogleClient.Builder( TRANSPORT, "https://test.random.com/", "", JSON_OBJECT_PARSER, null) .setApplicationName("Test Application"); - assertNull(clientBuilder.getServiceName()); + assertTrue(clientBuilder.isUserConfiguredEndpoint); + } + + @Test + public void testIsUserSetEndpoint_regionalEndpoint() { + AbstractGoogleClient.Builder clientBuilder = + new MockGoogleClient.Builder( + TRANSPORT, + "https://us-east-4.coolservice.googleapis.com/", + "", + JSON_OBJECT_PARSER, + null) + .setApplicationName("Test Application"); + assertTrue(clientBuilder.isUserConfiguredEndpoint); } @Test