Skip to content

Commit 51c0dd4

Browse files
feat: add server URL normalization hook
Normalize server URLs by prepending https:// when no scheme is present and stripping trailing slashes, ensuring consistent base URL formatting across SDK configurations.
1 parent 98060a5 commit 51c0dd4

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

src/main/java/com/glean/api_client/glean_api_client/hooks/SDKHooks.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ private SDKHooks() {
1313
}
1414

1515
public static void initialize(com.glean.api_client.glean_api_client.utils.Hooks hooks) {
16+
hooks.registerSdkInit(ServerURLNormalizerHook.createSyncHook());
17+
1618
hooks.registerAfterError(AgentFileUploadErrorHook.createSyncHook());
1719

1820
// Register the X-Glean header hook for experimental features and deprecation testing
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.glean.api_client.glean_api_client.hooks;
2+
3+
import com.glean.api_client.glean_api_client.utils.Hook;
4+
5+
import java.util.regex.Pattern;
6+
7+
public final class ServerURLNormalizerHook {
8+
9+
private static final Pattern SCHEME_PATTERN = Pattern.compile("(?i)^https?://");
10+
11+
private ServerURLNormalizerHook() {
12+
// prevent instantiation
13+
}
14+
15+
static String normalize(String url) {
16+
String normalized = url;
17+
if (!SCHEME_PATTERN.matcher(normalized).find()) {
18+
normalized = "https://" + normalized;
19+
}
20+
normalized = normalized.replaceAll("/+$", "");
21+
return normalized;
22+
}
23+
24+
public static Hook.SdkInit createSyncHook() {
25+
return data -> new Hook.SdkInitData(normalize(data.baseUrl()), data.client());
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.glean.api_client.glean_api_client.hooks;
2+
3+
import com.glean.api_client.glean_api_client.utils.Hook;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class ServerURLNormalizerHookTest {
11+
12+
@ParameterizedTest
13+
@CsvSource({
14+
"example.glean.com, https://example.glean.com",
15+
"https://example.glean.com, https://example.glean.com",
16+
"http://localhost:8080, http://localhost:8080",
17+
"https://example.glean.com///, https://example.glean.com",
18+
"https://example.glean.com/api/v1, https://example.glean.com/api/v1",
19+
"example.glean.com/, https://example.glean.com",
20+
"HTTP://EXAMPLE.COM, HTTP://EXAMPLE.COM",
21+
"HTTPS://EXAMPLE.COM, HTTPS://EXAMPLE.COM",
22+
})
23+
void normalize(String input, String expected) {
24+
assertEquals(expected, ServerURLNormalizerHook.normalize(input));
25+
}
26+
27+
@ParameterizedTest
28+
@CsvSource({
29+
"example.glean.com, https://example.glean.com",
30+
"https://example.glean.com///, https://example.glean.com",
31+
"http://localhost:8080, http://localhost:8080",
32+
})
33+
void createSyncHook(String input, String expected) {
34+
Hook.SdkInit hook = ServerURLNormalizerHook.createSyncHook();
35+
Hook.SdkInitData data = new Hook.SdkInitData(input, null);
36+
Hook.SdkInitData result = hook.sdkInit(data);
37+
assertEquals(expected, result.baseUrl());
38+
assertNull(result.client());
39+
}
40+
}

0 commit comments

Comments
 (0)