From 98c32c7846a6f96268b6569aec580b40cfc5db8e Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 30 Aug 2019 14:02:24 +0200 Subject: [PATCH] Fix wrong URL encoding in watcher HTTP client (#45894) The test assumption was calling the wrong method resulting in a URL encoding before returning the data. Closes #44970 --- .../xpack/watcher/common/http/HttpClient.java | 7 ++++--- .../xpack/watcher/common/http/HttpClientTests.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java index 99945c09375..49e27f51ea7 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java @@ -334,12 +334,13 @@ public class HttpClient implements Closeable { String part = pathParts[i]; boolean isLast = i == pathParts.length - 1; if (Strings.isEmpty(part) == false) { - String appendPart = part; + unescapedPathParts.add(URLDecoder.decode(part, StandardCharsets.UTF_8.name())); + // if the passed URL ends with a slash, adding an empty string to the + // unescaped paths will ensure the slash will be added back boolean appendSlash = isPathEndsWithSlash && isLast; if (appendSlash) { - appendPart += "/"; + unescapedPathParts.add(""); } - unescapedPathParts.add(URLDecoder.decode(appendPart, StandardCharsets.UTF_8.name())); } } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java index 1f8ec6bb5d8..75e551b58e5 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java @@ -751,7 +751,7 @@ public class HttpClientTests extends ESTestCase { private void assertCreateUri(String uri, String expectedPath) { final HttpRequest request = HttpRequest.builder().fromUrl(uri).build(); final Tuple tuple = HttpClient.createURI(request); - assertThat(tuple.v2().getPath(), is(expectedPath)); + assertThat(tuple.v2().getRawPath(), is(expectedPath)); } public static ClusterService mockClusterService() {