From 7bcb8797ea02246a6efbf7fda5cf92689d3acc28 Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 11 Apr 2018 14:55:38 +0200 Subject: [PATCH] [TEST] REST client request without leading '/' 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 '/'. --- .../RestClientSingleHostIntegTests.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java index 3d282a642e0da..59aa2baab9672 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java @@ -58,6 +58,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * 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 final RestClientBuilder restClientBuilder = RestClient.builder( new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())).setDefaultHeaders(defaultHeaders); if (pathPrefix.length() > 0) { - // sometimes cut off the leading slash - restClientBuilder.setPathPrefix(randomBoolean() ? pathPrefix.substring(1) : pathPrefix); + restClientBuilder.setPathPrefix(pathPrefix); } if (useAuth) { @@ -281,6 +281,33 @@ public void testPreemptiveAuthDisabled() throws IOException { } } + public void testUrlWithoutLeadingSlash() throws Exception { + if (pathPrefix.length() == 0) { + try { + restClient.performRequest("GET", "200"); + fail("request should have failed"); + } catch(ResponseException e) { + assertEquals(404, e.getResponse().getStatusLine().getStatusCode()); + } + } else { + { + Response response = restClient.performRequest("GET", "200"); + //a trailing slash gets automatically added if a pathPrefix is configured + assertEquals(200, response.getStatusLine().getStatusCode()); + } + { + //pathPrefix is not required to start with '/', will be added automatically + try (RestClient restClient = RestClient.builder( + new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())) + .setPathPrefix(pathPrefix.substring(1)).build()) { + Response response = restClient.performRequest("GET", "200"); + //a trailing slash gets automatically added if a pathPrefix is configured + assertEquals(200, response.getStatusLine().getStatusCode()); + } + } + } + } + private Response bodyTest(final String method) throws IOException { return bodyTest(restClient, method); }