Skip to content

Commit d0706dc

Browse files
committed
[TEST] REST client request without leading '/' (#29471)
The following is the current behaviour, tested now through a specific test. The low-level REST client doesn't add a leading wildcard when not provided, unless a `pathPrefix` is configured in which case a trailing slash will be automatically added when concatenating the prefix and the provided uri. Also when configuring a pathPrefix, if it doesn't start with a '/' it will be modified by adding the missing leading '/'.
1 parent 6cfe16f commit d0706dc

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import static org.junit.Assert.assertEquals;
5959
import static org.junit.Assert.assertThat;
6060
import static org.junit.Assert.assertTrue;
61+
import static org.junit.Assert.fail;
6162

6263
/**
6364
* Integration test to check interaction between {@link RestClient} and {@link org.apache.http.client.HttpClient}.
@@ -135,8 +136,7 @@ private static RestClient createRestClient(final boolean useAuth, final boolean
135136
final RestClientBuilder restClientBuilder = RestClient.builder(
136137
new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())).setDefaultHeaders(defaultHeaders);
137138
if (pathPrefix.length() > 0) {
138-
// sometimes cut off the leading slash
139-
restClientBuilder.setPathPrefix(randomBoolean() ? pathPrefix.substring(1) : pathPrefix);
139+
restClientBuilder.setPathPrefix(pathPrefix);
140140
}
141141

142142
if (useAuth) {
@@ -281,6 +281,33 @@ public void testPreemptiveAuthDisabled() throws IOException {
281281
}
282282
}
283283

284+
public void testUrlWithoutLeadingSlash() throws Exception {
285+
if (pathPrefix.length() == 0) {
286+
try {
287+
restClient.performRequest("GET", "200");
288+
fail("request should have failed");
289+
} catch(ResponseException e) {
290+
assertEquals(404, e.getResponse().getStatusLine().getStatusCode());
291+
}
292+
} else {
293+
{
294+
Response response = restClient.performRequest("GET", "200");
295+
//a trailing slash gets automatically added if a pathPrefix is configured
296+
assertEquals(200, response.getStatusLine().getStatusCode());
297+
}
298+
{
299+
//pathPrefix is not required to start with '/', will be added automatically
300+
try (RestClient restClient = RestClient.builder(
301+
new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()))
302+
.setPathPrefix(pathPrefix.substring(1)).build()) {
303+
Response response = restClient.performRequest("GET", "200");
304+
//a trailing slash gets automatically added if a pathPrefix is configured
305+
assertEquals(200, response.getStatusLine().getStatusCode());
306+
}
307+
}
308+
}
309+
}
310+
284311
private Response bodyTest(final String method) throws IOException {
285312
return bodyTest(restClient, method);
286313
}

0 commit comments

Comments
 (0)