From f7e7de490f90b593efe81160ee398e4310a518d6 Mon Sep 17 00:00:00 2001 From: Serhii Kobeza Date: Fri, 25 Jul 2025 23:12:27 +0200 Subject: [PATCH] update buildBaseUrl function and create tests --- .../guardian/sdk/RichConsentsAPIClient.java | 17 +++++- .../sdk/RichConsentsAPIClientTest.java | 60 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/guardian/src/main/java/com/auth0/android/guardian/sdk/RichConsentsAPIClient.java b/guardian/src/main/java/com/auth0/android/guardian/sdk/RichConsentsAPIClient.java index 4ccefae..b2404aa 100644 --- a/guardian/src/main/java/com/auth0/android/guardian/sdk/RichConsentsAPIClient.java +++ b/guardian/src/main/java/com/auth0/android/guardian/sdk/RichConsentsAPIClient.java @@ -34,6 +34,8 @@ public class RichConsentsAPIClient { private final HttpUrl baseUrl; private final ClientInfo clientInfo; + private static final String CONSENT_PATH = "rich-consents"; + RichConsentsAPIClient(RequestFactory requestFactory, Uri url, ClientInfo clientInfo) { this.requestFactory = requestFactory; @@ -120,10 +122,21 @@ public GuardianAPIRequest fetch(@NonNull String consentId, @NonNull .setHeader("MFA-DPoP", dpopAssertion); } - private HttpUrl buildBaseUrl(Uri url) { + public static HttpUrl buildBaseUrl(Uri url) { HttpUrl httpUrl = HttpUrl.parse(url.toString()); + if (httpUrl == null) { + throw new NullPointerException("Base uri cannot be null"); + } + + String host = httpUrl.host(); + + if (host.endsWith("auth0.com")) { + host = host.replace(".guardian", ""); + } + return httpUrl.newBuilder() - .addPathSegments("rich-consents") + .host(host) + .addPathSegments(CONSENT_PATH) .build(); } } diff --git a/guardian/src/test/java/com/auth0/android/guardian/sdk/RichConsentsAPIClientTest.java b/guardian/src/test/java/com/auth0/android/guardian/sdk/RichConsentsAPIClientTest.java index f638c18..f515707 100644 --- a/guardian/src/test/java/com/auth0/android/guardian/sdk/RichConsentsAPIClientTest.java +++ b/guardian/src/test/java/com/auth0/android/guardian/sdk/RichConsentsAPIClientTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -32,6 +33,7 @@ import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAKey; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -156,4 +158,62 @@ private void verifyDPoPAssertion(String assertion) { .build() .verify(assertion); } + + static class TestCase { + final String description; + final Uri input; + final HttpUrl expected; + + TestCase(String description, String inputUrl, String expectedUrl) { + this.description = description; + this.input = Uri.parse(inputUrl); + this.expected = HttpUrl.parse(expectedUrl); + } + } + + @Test + public void testBuildBaseUrl() { + List testCases = Arrays.asList( + new TestCase( + "should handle legacy url with region", + "https://samples.guardian.eu.auth0.com", + "https://samples.eu.auth0.com/rich-consents" + ), + new TestCase( + "should handle legacy url without region", + "https://samples.guardian.auth0.com", + "https://samples.auth0.com/rich-consents" + ), + new TestCase( + "should handle url with additional path", + "https://samples.guardian.eu.auth0.com/path/", + "https://samples.eu.auth0.com/path/rich-consents" + ), + new TestCase( + "should handle canonical tenant url with region", + "https://samples.eu.auth0.com", + "https://samples.eu.auth0.com/rich-consents" + ), + new TestCase( + "should handle canonical tenant url without region", + "https://samples.auth0.com", + "https://samples.auth0.com/rich-consents" + ), + new TestCase( + "should handle custom domain url", + "https://custom-domain.com", + "https://custom-domain.com/rich-consents" + ), + new TestCase( + "should handle custom domain url with guardian subdomain", + "https://guardian.custom-domain.com", + "https://guardian.custom-domain.com/rich-consents" + ) + ); + + for (TestCase testCase : testCases) { + HttpUrl result = RichConsentsAPIClient.buildBaseUrl(testCase.input); + assertEquals(testCase.description, testCase.expected, result); + } + } } \ No newline at end of file