InitialEndpointSupplier implements Supplier<EndpointGroup> { @Override public EndpointGroup get() { List<EndpointGroup> endpointGroups = new ArrayList<>(); for (String hostText : hosts.split(",", 100)) { URI url; if (hostText.startsWith("http: url = URI.create(hostText); } else if (!sessionProtocol.isTls() && hostText.indexOf(':') == -1) { url = URI.create(sessionProtocol.uriText() + ": } else { url = URI.create(sessionProtocol.uriText() + ": } String host = url.getHost(); int port = getPort(url); if (port == 9300) { LOGGER.warn("Native transport no longer supported. Changing {} to http port 9200", host); port = 9200; } if (isIpAddress(host) || host.equals("localhost")) { endpointGroups.add(EndpointGroup.of(Endpoint.of(host, port))); } else { endpointGroups.add(DnsAddressEndpointGroup.builder(host).port(port).build()); } } return EndpointGroup.of(endpointGroups); } InitialEndpointSupplier(SessionProtocol sessionProtocol, @Nullable String hosts); @Override EndpointGroup get(); @Override String toString();  }
@Test void defaultIsLocalhost9200RegardlessOfSessionProtocol() { assertThat(new InitialEndpointSupplier(HTTP, null).get()) .isEqualTo(Endpoint.of("localhost", 9200)) .isEqualTo(new InitialEndpointSupplier(HTTPS, null).get()); }
@Test void usesNaturalHttpPortsWhenUrls() { assertThat(new InitialEndpointSupplier(HTTP, "http: .isEqualTo(Endpoint.of("localhost", 80)); assertThat(new InitialEndpointSupplier(HTTPS, "https: .isEqualTo(Endpoint.of("localhost", 443)); }
@Test void defaultsPlainHostsToPort9200() { assertThat(new InitialEndpointSupplier(HTTP, "localhost").get()) .isEqualTo(Endpoint.of("localhost", 9200)); assertThat(new InitialEndpointSupplier(HTTPS, "localhost").get()) .isEqualTo(Endpoint.of("localhost", 443)); }
@Test public void coersesPort9300To9200() { assertThat(new InitialEndpointSupplier(HTTP, "localhost:9300").get()) .isEqualTo(Endpoint.of("localhost", 9200)); }
@Test void parsesListOfLocalhosts() { String hostList = "localhost:9201,localhost:9202"; assertThat(new InitialEndpointSupplier(HTTP, hostList).get().endpoints()) .containsExactly(Endpoint.of("localhost", 9201), Endpoint.of("localhost", 9202)) .containsExactlyElementsOf(new InitialEndpointSupplier(HTTPS, hostList).get().endpoints()); }