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.
This commit is contained in:
javanna 2016-07-07 19:10:21 +02:00 committed by Luca Cavanna
parent a20cf8275b
commit 134b73ab33
2 changed files with 6 additions and 2 deletions

View File

@ -156,7 +156,7 @@ public class HostsSniffer {
private final RestClient restClient; private final RestClient restClient;
private long sniffRequestTimeoutMillis = DEFAULT_SNIFF_REQUEST_TIMEOUT; private long sniffRequestTimeoutMillis = DEFAULT_SNIFF_REQUEST_TIMEOUT;
private Scheme scheme; private Scheme scheme = Scheme.HTTP;
private Builder(RestClient restClient) { private Builder(RestClient restClient) {
Objects.requireNonNull(restClient, "restClient cannot be null"); Objects.requireNonNull(restClient, "restClient cannot be null");

View File

@ -89,7 +89,11 @@ public class HostsSnifferTests extends RestClientTestCase {
public void testSniffNodes() throws IOException, URISyntaxException { public void testSniffNodes() throws IOException, URISyntaxException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()); HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) { 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 { try {
List<HttpHost> sniffedHosts = sniffer.sniffHosts(); List<HttpHost> sniffedHosts = sniffer.sniffHosts();
if (sniffResponse.isFailure) { if (sniffResponse.isFailure) {