Skip to content

Commit

Permalink
chore: Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Mar 11, 2024
1 parent 83612b6 commit 1cfd93a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -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. ,
Expand Down Expand Up @@ -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();
}

/**
Expand Down
Expand Up @@ -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 =
Expand All @@ -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
Expand Down

0 comments on commit 1cfd93a

Please sign in to comment.