Skip to content

Commit 9f1ed3a

Browse files
Copilotchernser
andcommitted
Refactor tests and improve documentation
- Extracted common URL building logic into helper method - Improved code maintainability by reducing duplication - Clarified documentation for customURLPath parameter Co-authored-by: chernser <[email protected]>
1 parent 8266a69 commit 9f1ed3a

File tree

2 files changed

+31
-80
lines changed

2 files changed

+31
-80
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,9 @@ public Builder sslSocketSNI(String sni) {
10521052
* Sets a custom URL path to be appended to the base URL for routing requests.
10531053
* This is useful when multiple database instances are behind a load balancer and routing
10541054
* is configured by path. For example: "/sales/db" or "/app/db".
1055+
* The path will be appended to the base endpoint URL as-is.
10551056
*
1056-
* @param path - custom URL path (should start with "/")
1057+
* @param path - custom URL path (e.g., "/sales/db")
10571058
* @return this builder instance
10581059
*/
10591060
public Builder customURLPath(String path) {

client-v2/src/test/java/com/clickhouse/client/api/CustomURLPathTest.java

Lines changed: 29 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,35 @@
1717
*/
1818
public class CustomURLPathTest {
1919

20-
@Test(groups = {"unit"})
21-
public void testCustomURLPathConfiguration() {
22-
String customPath = "/sales/db";
23-
24-
// Simulate the URL building logic from HttpAPIClientHelper
25-
String baseURL = "http://localhost:8123";
20+
/**
21+
* Helper method to build URI with custom path, simulating the logic from HttpAPIClientHelper.
22+
*/
23+
private URI buildURIWithCustomPath(String baseURL, String customPath) throws URISyntaxException {
2624
Map<String, Object> requestConfig = new HashMap<>();
27-
requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath);
25+
if (customPath != null) {
26+
requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath);
27+
}
2828

29-
try {
30-
URIBuilder uriBuilder = new URIBuilder(baseURL);
31-
32-
// Add custom URL path if configured
33-
String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey());
34-
if (configuredPath != null && !configuredPath.isEmpty()) {
35-
String existingPath = uriBuilder.getPath();
36-
if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) {
37-
uriBuilder.setPath(configuredPath);
38-
} else {
39-
uriBuilder.setPath(existingPath + configuredPath);
40-
}
29+
URIBuilder uriBuilder = new URIBuilder(baseURL);
30+
31+
// Add custom URL path if configured
32+
String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey());
33+
if (configuredPath != null && !configuredPath.isEmpty()) {
34+
String existingPath = uriBuilder.getPath();
35+
if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) {
36+
uriBuilder.setPath(configuredPath);
37+
} else {
38+
uriBuilder.setPath(existingPath + configuredPath);
4139
}
42-
43-
URI uri = uriBuilder.normalizeSyntax().build();
40+
}
41+
42+
return uriBuilder.normalizeSyntax().build();
43+
}
44+
45+
@Test(groups = {"unit"})
46+
public void testCustomURLPathConfiguration() {
47+
try {
48+
URI uri = buildURIWithCustomPath("http://localhost:8123", "/sales/db");
4449

4550
assertEquals(uri.toString(), "http://localhost:8123/sales/db");
4651
assertEquals(uri.getPath(), "/sales/db");
@@ -51,28 +56,8 @@ public void testCustomURLPathConfiguration() {
5156

5257
@Test(groups = {"unit"})
5358
public void testCustomURLPathWithExistingPath() {
54-
String customPath = "/app/db";
55-
56-
// Test with base URL that already has a path
57-
String baseURL = "http://localhost:8123/api";
58-
Map<String, Object> requestConfig = new HashMap<>();
59-
requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), customPath);
60-
6159
try {
62-
URIBuilder uriBuilder = new URIBuilder(baseURL);
63-
64-
// Add custom URL path if configured
65-
String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey());
66-
if (configuredPath != null && !configuredPath.isEmpty()) {
67-
String existingPath = uriBuilder.getPath();
68-
if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) {
69-
uriBuilder.setPath(configuredPath);
70-
} else {
71-
uriBuilder.setPath(existingPath + configuredPath);
72-
}
73-
}
74-
75-
URI uri = uriBuilder.normalizeSyntax().build();
60+
URI uri = buildURIWithCustomPath("http://localhost:8123/api", "/app/db");
7661

7762
assertEquals(uri.toString(), "http://localhost:8123/api/app/db");
7863
assertEquals(uri.getPath(), "/api/app/db");
@@ -83,26 +68,8 @@ public void testCustomURLPathWithExistingPath() {
8368

8469
@Test(groups = {"unit"})
8570
public void testEmptyCustomURLPath() {
86-
// Test with empty custom path
87-
String baseURL = "http://localhost:8123";
88-
Map<String, Object> requestConfig = new HashMap<>();
89-
requestConfig.put(ClientConfigProperties.CUSTOM_URL_PATH.getKey(), "");
90-
9171
try {
92-
URIBuilder uriBuilder = new URIBuilder(baseURL);
93-
94-
// Add custom URL path if configured
95-
String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey());
96-
if (configuredPath != null && !configuredPath.isEmpty()) {
97-
String existingPath = uriBuilder.getPath();
98-
if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) {
99-
uriBuilder.setPath(configuredPath);
100-
} else {
101-
uriBuilder.setPath(existingPath + configuredPath);
102-
}
103-
}
104-
105-
URI uri = uriBuilder.normalizeSyntax().build();
72+
URI uri = buildURIWithCustomPath("http://localhost:8123", "");
10673

10774
// Empty path should not modify the URL
10875
assertEquals(uri.toString(), "http://localhost:8123");
@@ -113,25 +80,8 @@ public void testEmptyCustomURLPath() {
11380

11481
@Test(groups = {"unit"})
11582
public void testNoCustomURLPath() {
116-
// Test without custom path configured
117-
String baseURL = "http://localhost:8123";
118-
Map<String, Object> requestConfig = new HashMap<>();
119-
12083
try {
121-
URIBuilder uriBuilder = new URIBuilder(baseURL);
122-
123-
// Add custom URL path if configured
124-
String configuredPath = (String) requestConfig.get(ClientConfigProperties.CUSTOM_URL_PATH.getKey());
125-
if (configuredPath != null && !configuredPath.isEmpty()) {
126-
String existingPath = uriBuilder.getPath();
127-
if (existingPath == null || existingPath.isEmpty() || existingPath.equals("/")) {
128-
uriBuilder.setPath(configuredPath);
129-
} else {
130-
uriBuilder.setPath(existingPath + configuredPath);
131-
}
132-
}
133-
134-
URI uri = uriBuilder.normalizeSyntax().build();
84+
URI uri = buildURIWithCustomPath("http://localhost:8123", null);
13585

13686
// No custom path should keep URL unchanged
13787
assertEquals(uri.toString(), "http://localhost:8123");

0 commit comments

Comments
 (0)