Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/main/java/com/auth0/client/mgmt/ManagementApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public ManagementApiBuilder url(String url) {
* .build();
* }</pre>
*
* @param domain The Auth0 domain (e.g., "your-tenant.auth0.com")
* @param domain The Auth0 domain (e.g., "your-tenant.auth0.com"). A leading {@code https://} and a trailing
* slash are accepted.
* @return This builder for method chaining
*/
public ManagementApiBuilder domain(String domain) {
Expand Down Expand Up @@ -241,7 +242,7 @@ protected ClientOptions buildClientOptions() {
*/
protected void setEnvironment(ClientOptions.Builder builder) {
if (this.domain != null) {
String url = "https://" + this.domain + "/api/v2";
String url = getBaseUrl() + "/api/v2";
builder.environment(Environment.custom(url));
} else {
builder.environment(this.environment);
Expand All @@ -256,7 +257,11 @@ protected void setEnvironment(ClientOptions.Builder builder) {
*/
protected String getBaseUrl() {
if (this.domain != null) {
return "https://" + this.domain;
String url = this.domain.replaceAll("/+$", "");
if (!url.startsWith("https://") && !url.startsWith("http://")) {
url = "https://" + url;
}
return url;
}
// Extract base URL from environment URL by removing /api/v2 suffix
String envUrl = this.environment.getUrl();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.auth0.client.mgmt;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

/**
* Tests for ManagementApiBuilder domain-based initialization.
Expand Down Expand Up @@ -43,22 +46,24 @@ public void testAudienceMethodExists() {
/**
* Test that build() fails when no authentication is provided.
*/
@Test(expected = RuntimeException.class)
@Test
public void testBuildFailsWithoutAuth() {
ManagementApi.builder().domain("my-tenant.auth0.com").build();
assertThrows(
RuntimeException.class,
() -> ManagementApi.builder().domain("my-tenant.auth0.com").build());
}

/**
* Test that build() fails when only clientId is provided without clientSecret.
*/
@Test(expected = RuntimeException.class)
@Test
public void testBuildFailsWithPartialCredentials() {
// This tests an edge case where someone might call clientCredentials incorrectly
ManagementApiBuilder builder = ManagementApi.builder();
builder.domain("my-tenant.auth0.com");
// Manually set only clientId to simulate partial configuration
// This should fail during build
builder.build();
assertThrows(RuntimeException.class, builder::build);
}

/**
Expand Down Expand Up @@ -138,4 +143,30 @@ public void testFluentChaining() {
.build();
assert client != null : "Fluent chaining should work";
}

/**
* Test that domain() builds the expected URLs from a bare domain.
*/
@Test
public void testDomainUrls() {
ManagementApiBuilder builder =
ManagementApi.builder().domain("my-tenant.auth0.com").token("test-token");
assertEquals("https://my-tenant.auth0.com", builder.getBaseUrl());
assertEquals(
"https://my-tenant.auth0.com/api/v2",
builder.buildClientOptions().environment().getUrl());
}

/**
* Test that domain() accepts a domain that includes the scheme and a trailing slash.
*/
@Test
public void testDomainWithSchemeAndTrailingSlash() {
ManagementApiBuilder builder =
ManagementApi.builder().domain("https://my-tenant.auth0.com/").token("test-token");
assertEquals("https://my-tenant.auth0.com", builder.getBaseUrl());
assertEquals(
"https://my-tenant.auth0.com/api/v2",
builder.buildClientOptions().environment().getUrl());
}
}