Skip to content

Commit dc41db1

Browse files
authored
Fix baseUrl Bug (#183)
* fixed baseUrl bug * added test and simplified logic * removed extra file * added fix * added one more test and bumped the version
1 parent 32705a5 commit dc41db1

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.descope</groupId>
55
<artifactId>java-sdk</artifactId>
66
<modelVersion>4.0.0</modelVersion>
7-
<version>1.0.37</version>
7+
<version>1.0.38</version>
88
<name>${project.groupId}:${project.artifactId}</name>
99
<description>Java library used to integrate with Descope.</description>
1010
<url>https://github.com/descope/descope-java</url>

src/main/java/com/descope/client/DescopeClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static Client getClient(Config config) {
6464
throw ClientSetupException.invalidProjectId();
6565
}
6666
final String region = projectId.substring(1, projectId.length() - 27);
67-
final String baseUrl = DEFAULT_BASE_URL.replace(REGION_PLACEHOLDER, region.length() > 0 ? region + "." : "");
67+
final String baseUrl = DEFAULT_BASE_URL.replace(REGION_PLACEHOLDER, region.length() > 3 ? region + "." : "");
6868
Client c = Client.builder()
6969
.uri(StringUtils.isBlank(config.getDescopeBaseUrl()) ? baseUrl : config.getDescopeBaseUrl())
7070
.projectId(projectId)

src/test/java/com/descope/client/DescopeClientTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,36 @@ void testEmptyProjectID() throws Exception {
8787
});
8888
}
8989

90+
@Test
91+
void testShortProjectID() throws Exception {
92+
// Project ID too short (< 28 characters)
93+
String shortProjectId = "P1234567890123456789012345";
94+
EnvironmentVariables env = new EnvironmentVariables(PROJECT_ID_ENV_VAR, shortProjectId);
95+
env.execute(() -> {
96+
Assertions.assertThatThrownBy(DescopeClient::new)
97+
.isInstanceOf(ClientSetupException.class)
98+
.hasMessage("Invalid project ID - must be over 27 characters long");
99+
});
100+
}
101+
102+
@Test
103+
void testProjectIDRegion() throws Exception {
104+
String invalidRegionProjectId = "Pus11456789012345678901234567";
105+
EnvironmentVariables env = new EnvironmentVariables(PROJECT_ID_ENV_VAR, invalidRegionProjectId);
106+
env.execute(() -> {
107+
DescopeClient descopeClient = new DescopeClient();
108+
ApiProxy apiProxy = mock(ApiProxy.class);
109+
UserResponseDetails userResponseDetails = mock(UserResponseDetails.class);
110+
when(apiProxy.get(eq(new URI("https://api.descope.com/v1/mgmt/user?loginId=test")), any()))
111+
.thenReturn(userResponseDetails);
112+
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
113+
mockedApiProxyBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
114+
UserResponseDetails u = descopeClient.getManagementServices().getUserService().load("test");
115+
Assertions.assertThat(u).isNotNull();
116+
}
117+
});
118+
}
119+
90120
@Test
91121
void testEmptyConfig() {
92122
Assertions.assertThatThrownBy(() -> new DescopeClient(null))

0 commit comments

Comments
 (0)