From 134b73ab3324f39b72d074b1a0a079ed6fd91ac3 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 7 Jul 2016 19:10:21 +0200 Subject: [PATCH] Rest Client: HostsSniffer to set http as default scheme The assumption is HostsSniffer is that all of the arguments have been properly provided and validated through HostsSniffer.Builder, except they weren't, as the scheme didn't have a default value and when not set would cause NPEs down the road. Improved tests to catch this also. --- .../java/org/elasticsearch/client/sniff/HostsSniffer.java | 2 +- .../org/elasticsearch/client/sniff/HostsSnifferTests.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java index 7cdb51066cb..f06436c6175 100644 --- a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java +++ b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java @@ -156,7 +156,7 @@ public class HostsSniffer { private final RestClient restClient; private long sniffRequestTimeoutMillis = DEFAULT_SNIFF_REQUEST_TIMEOUT; - private Scheme scheme; + private Scheme scheme = Scheme.HTTP; private Builder(RestClient restClient) { Objects.requireNonNull(restClient, "restClient cannot be null"); diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java index 1b38f034262..6e0c3a728d5 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferTests.java @@ -89,7 +89,11 @@ public class HostsSnifferTests extends RestClientTestCase { public void testSniffNodes() throws IOException, URISyntaxException { HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()); try (RestClient restClient = RestClient.builder(httpHost).build()) { - HostsSniffer sniffer = new HostsSniffer(restClient, sniffRequestTimeout, scheme); + HostsSniffer.Builder builder = HostsSniffer.builder(restClient).setSniffRequestTimeoutMillis(sniffRequestTimeout); + if (scheme != HostsSniffer.Scheme.HTTP || randomBoolean()) { + builder.setScheme(scheme); + } + HostsSniffer sniffer = builder.build(); try { List sniffedHosts = sniffer.sniffHosts(); if (sniffResponse.isFailure) {