Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,10 +122,21 @@ public GuardianAPIRequest<RichConsent> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<TestCase> 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);
}
}
}