HttpClient: Prevent NPE when no path is specified

Original commit: elastic/x-pack-elasticsearch@47f26a5850
This commit is contained in:
Alexander Reelsen 2016-02-01 10:37:44 +01:00
parent 6287a1300a
commit 0fe7716459
2 changed files with 10 additions and 5 deletions

View File

@ -142,7 +142,7 @@ public class HttpClient extends AbstractLifecycleComponent<HttpClient> {
queryString = builder.toString();
}
String path = request.path;
String path = Strings.hasLength(request.path) ? request.path : "";
if (Strings.hasLength(queryString)) {
path += "?" + queryString;
}

View File

@ -24,10 +24,7 @@ import org.junit.Before;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.BindException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.UnrecoverableKeyException;
@ -148,6 +145,14 @@ public class HttpClientTests extends ESTestCase {
assertThat(recordedRequest.getHeader("Authorization"), equalTo("Basic dXNlcjpwYXNz"));
}
public void testNoPathSpecified() throws Exception {
webServer.enqueue(new MockResponse().setResponseCode(200).setBody("doesntmatter"));
HttpRequest.Builder request = HttpRequest.builder("localhost", webPort).method(HttpMethod.GET);
httpClient.execute(request.build());
RecordedRequest recordedRequest = webServer.takeRequest();
assertThat(recordedRequest.getPath(), equalTo("/"));
}
public void testHttps() throws Exception {
Path resource = getDataPath("/org/elasticsearch/shield/keystore/truststore-testnode-only.jks");