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:
parent
264c88f445
commit
f6d318a782
|
@ -241,8 +241,9 @@ public class HttpClient extends AbstractComponent {
|
||||||
try {
|
try {
|
||||||
List<NameValuePair> qparams = new ArrayList<>(request.params.size());
|
List<NameValuePair> qparams = new ArrayList<>(request.params.size());
|
||||||
request.params.forEach((k, v) -> qparams.add(new BasicNameValuePair(k, v)));
|
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,
|
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;
|
return uri;
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
|
|
|
@ -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).getHeader(HttpHeaders.CONTENT_TYPE), is(XContentType.JSON.mediaType()));
|
||||||
assertThat(webServer.requests().get(0).getBody(), is(body));
|
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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue