HttpClientFactoryImpl implements HttpClientFactory { private static OkHttpClient createClient(HttpClientConfig config) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); if (config.proxySettings().isProxyUse()) { Proxy proxy = createProxy(config.proxySettings()); builder.proxy(proxy); } if (unverifiedSSLCertificate()) { builder.hostnameVerifier((s, sslSession) -> true); } return builder.build(); }  @Override HttpClient newHttpClient(HttpClientConfig config);  }
@Test public void testNewClient() { ObjectMapper mapper = mock(ObjectMapper.class); HttpClientConfig config = HttpClientConfig.basicConfig() .withObjectMapper(mapper); HttpClientImpl client = createClient(config); assertThat(client.getMapper()).isSameAs(mapper); assertThat(client.getClient().proxy()).isNull(); }
@Test public void testNewClientWithProxySettings() { String host = "localhost"; int port = 8080; ProxySettings proxySettings = ProxySettings.useProxy(host, port); HttpClientConfig config = HttpClientConfig.basicConfig() .withProxySettings(proxySettings); HttpClientImpl client = createClient(config); Proxy proxy = client.getClient().proxy(); assertThat(proxy).isNotNull(); assertThat(proxy.address()).isInstanceOf(InetSocketAddress.class); InetSocketAddress address = (InetSocketAddress) proxy.address(); assertThat(address.getHostName()).isEqualTo(host); assertThat(address.getPort()).isEqualTo(port); assertThat(proxy.type()).isEqualTo(Proxy.Type.HTTP); }
@Test public void testNewClientWithoutCertificateCheck() { try { System.setProperty(HttpClientFactoryImpl.CLIENT_ACCESS_UNVERIFIED_PROPERTY, "true"); ObjectMapper mapper = mock(ObjectMapper.class); HttpClientConfig config = HttpClientConfig.basicConfig() .withObjectMapper(mapper); HttpClientImpl client = createClient(config); assertThat(client.getClient().hostnameVerifier().verify("", null)).isTrue(); } finally { System.clearProperty(HttpClientFactoryImpl.CLIENT_ACCESS_UNVERIFIED_PROPERTY); } }