Skip to content

Commit 1cfd93a

Browse files
committed
chore: Address PR comments
1 parent 83612b6 commit 1cfd93a

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.annotations.VisibleForTesting;
2626
import java.io.IOException;
2727
import java.util.logging.Logger;
28+
import java.util.regex.Pattern;
2829

2930
/**
3031
* Abstract thread-safe Google client.
@@ -405,6 +406,13 @@ public abstract static class Builder {
405406
/** User configured Universe Domain. Defaults to `googleapis.com`. */
406407
String universeDomain;
407408

409+
/**
410+
* Regex pattern to check if the URL passed in matches the default endpoint confgured from a
411+
* discovery doc. Follows the format of `https://{serviceName}(.mtls).googleapis.com/`
412+
*/
413+
Pattern defaultEndpointRegex =
414+
Pattern.compile("https://[a-zA-Z]*(\\.mtls)?\\.googleapis.com/?");
415+
408416
/**
409417
* Whether the user has configured an endpoint via {@link #setRootUrl(String)}. This is added in
410418
* because the rootUrl is set in the Builder's constructor. ,
@@ -443,8 +451,7 @@ protected Builder(
443451
this.servicePath = normalizeServicePath(servicePath);
444452
this.httpRequestInitializer = httpRequestInitializer;
445453
this.serviceName = parseServiceName(rootUrl);
446-
this.isUserConfiguredEndpoint =
447-
!this.rootUrl.endsWith(Credentials.GOOGLE_DEFAULT_UNIVERSE + "/");
454+
this.isUserConfiguredEndpoint = !defaultEndpointRegex.matcher(this.rootUrl).matches();
448455
}
449456

450457
/**

google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ public void testInitialize() throws Exception {
289289
assertTrue(remoteRequestInitializer.isCalled);
290290
}
291291

292+
@Test
293+
public void testParseServiceName_nonMtlsRootUrl() {
294+
AbstractGoogleClient.Builder clientBuilder =
295+
new MockGoogleClient.Builder(
296+
TRANSPORT, "https://random.googleapis.com/", "", JSON_OBJECT_PARSER, null)
297+
.setApplicationName("Test Application");
298+
assertEquals(clientBuilder.getServiceName(), "random");
299+
}
300+
292301
@Test
293302
public void testParseServiceName_mtlsRootUrl() {
294303
AbstractGoogleClient.Builder clientBuilder =
@@ -299,21 +308,52 @@ public void testParseServiceName_mtlsRootUrl() {
299308
}
300309

301310
@Test
302-
public void testParseServiceName_nonMtlsRootUrl() {
311+
public void testParseServiceName_nonGDURootUrl() {
312+
AbstractGoogleClient.Builder clientBuilder =
313+
new MockGoogleClient.Builder(
314+
TRANSPORT, "https://test.random.com/", "", JSON_OBJECT_PARSER, null)
315+
.setApplicationName("Test Application");
316+
assertNull(clientBuilder.getServiceName());
317+
}
318+
319+
@Test
320+
public void testIsUserSetEndpoint_nonMtlsRootUrl() {
303321
AbstractGoogleClient.Builder clientBuilder =
304322
new MockGoogleClient.Builder(
305323
TRANSPORT, "https://random.googleapis.com/", "", JSON_OBJECT_PARSER, null)
306324
.setApplicationName("Test Application");
307-
assertEquals(clientBuilder.getServiceName(), "random");
325+
assertFalse(clientBuilder.isUserConfiguredEndpoint);
308326
}
309327

310328
@Test
311-
public void testParseServiceName_nonGDURootUrl() {
329+
public void testIsUserSetEndpoint_mtlsRootUrl() {
330+
AbstractGoogleClient.Builder clientBuilder =
331+
new MockGoogleClient.Builder(
332+
TRANSPORT, "https://test.mtls.googleapis.com/", "", JSON_OBJECT_PARSER, null)
333+
.setApplicationName("Test Application");
334+
assertFalse(clientBuilder.isUserConfiguredEndpoint);
335+
}
336+
337+
@Test
338+
public void testIsUserSetEndpoint_nonGDURootUrl() {
312339
AbstractGoogleClient.Builder clientBuilder =
313340
new MockGoogleClient.Builder(
314341
TRANSPORT, "https://test.random.com/", "", JSON_OBJECT_PARSER, null)
315342
.setApplicationName("Test Application");
316-
assertNull(clientBuilder.getServiceName());
343+
assertTrue(clientBuilder.isUserConfiguredEndpoint);
344+
}
345+
346+
@Test
347+
public void testIsUserSetEndpoint_regionalEndpoint() {
348+
AbstractGoogleClient.Builder clientBuilder =
349+
new MockGoogleClient.Builder(
350+
TRANSPORT,
351+
"https://us-east-4.coolservice.googleapis.com/",
352+
"",
353+
JSON_OBJECT_PARSER,
354+
null)
355+
.setApplicationName("Test Application");
356+
assertTrue(clientBuilder.isUserConfiguredEndpoint);
317357
}
318358

319359
@Test

0 commit comments

Comments
 (0)