Watcher: Prevent question mark in HttpClient with empty params (elastic/x-pack-elasticsearch#4206)

The HTTPClient in watcher always appended a question mark at the end of
an URL, regardless if parameters were used or not. This commit adds a
check to only pass valid parameters to the URI construction.

Original commit: elastic/x-pack-elasticsearch@184f8f441c
This commit is contained in:
Alexander Reelsen 2018-03-23 12:16:34 +01:00 committed by GitHub
parent 264c88f445
commit f6d318a782
2 changed files with 13 additions and 1 deletions

View File

@ -241,8 +241,9 @@ public class HttpClient extends AbstractComponent {
try {
List<NameValuePair> qparams = new ArrayList<>(request.params.size());
request.params.forEach((k, v) -> qparams.add(new BasicNameValuePair(k, v)));
String format = URLEncodedUtils.format(qparams, "UTF-8");
URI uri = URIUtils.createURI(request.scheme.scheme(), request.host, request.port, request.path,
URLEncodedUtils.format(qparams, "UTF-8"), null);
Strings.isNullOrEmpty(format) ? null : format, null);
return uri;
} catch (URISyntaxException e) {

View File

@ -595,4 +595,15 @@ public class HttpClientTests extends ESTestCase {
assertThat(webServer.requests().get(0).getHeader(HttpHeaders.CONTENT_TYPE), is(XContentType.JSON.mediaType()));
assertThat(webServer.requests().get(0).getBody(), is(body));
}
public void testThatUrlDoesNotContainQuestionMarkAtTheEnd() throws Exception {
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever"));
HttpRequest request = HttpRequest.builder("localhost", webServer.getPort())
.path("foo")
.build();
httpClient.execute(request);
assertThat(webServer.requests(), hasSize(1));
assertThat(webServer.requests().get(0).getUri().getRawPath(), is("/foo"));
}
}