WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory { @Override public HttpClient getCommonHttpClient() { initialize(); logger.debug("shared http client requested"); return commonHttpClient; } @Activate  WebClientFactoryImpl(final @Reference ExtensibleTrustManager extensibleTrustManager); @Override HttpClient createHttpClient(String consumerName); @Override WebSocketClient createWebSocketClient(String consumerName); @Override HttpClient getCommonHttpClient(); @Override WebSocketClient getCommonWebSocketClient();  }
@Disabled("connecting to the outside world makes this test flaky") @Test public void testCommonClientUsesExtensibleTrustManager() throws Exception { ArgumentCaptor<X509Certificate[]> certificateChainCaptor = ArgumentCaptor.forClass(X509Certificate[].class); ArgumentCaptor<SSLEngine> sslEngineCaptor = ArgumentCaptor.forClass(SSLEngine.class); HttpClient httpClient = webClientFactory.getCommonHttpClient(); ContentResponse response = httpClient.GET(TEST_URL); if (response.getStatus() != 200) { fail("Statuscode != 200"); } verify(extensibleTrustManager).checkServerTrusted(certificateChainCaptor.capture(), anyString(), sslEngineCaptor.capture()); verifyNoMoreInteractions(extensibleTrustManager); assertThat(sslEngineCaptor.getValue().getPeerHost(), is("www.eclipse.org")); assertThat(sslEngineCaptor.getValue().getPeerPort(), is(443)); assertThat(certificateChainCaptor.getValue()[0].getSubjectX500Principal().getName(), containsString("eclipse.org")); }
@Disabled("connecting to the outside world makes this test flaky") @Test public void testCommonClientUsesExtensibleTrustManagerFailure() throws Throwable { doThrow(new CertificateException()).when(extensibleTrustManager).checkServerTrusted( ArgumentMatchers.any(X509Certificate[].class), anyString(), ArgumentMatchers.any(SSLEngine.class)); HttpClient httpClient = webClientFactory.getCommonHttpClient(); assertThrows(SSLHandshakeException.class, () -> { try { httpClient.GET(TEST_URL); } catch (ExecutionException e) { throw e.getCause(); } }); }
@Disabled("only for manual test") @Test public void testMultiThreadedShared() throws Exception { ThreadPoolExecutor workers = new ThreadPoolExecutor(20, 80, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(50 * 50)); final List<HttpClient> clients = new ArrayList<>(); final int maxClients = 2; final int maxRequests = 2; for (int i = 0; i < maxClients; i++) { HttpClient httpClient = webClientFactory.getCommonHttpClient(); clients.add(httpClient); } final List<String> failures = new ArrayList<>(); for (int i = 0; i < maxRequests; i++) { for (final HttpClient client : clients) { workers.execute(new Runnable() { @Override public void run() { try { ContentResponse response = client.GET(TEST_URL); if (response.getStatus() != 200) { failures.add("Statuscode != 200"); } } catch (InterruptedException | ExecutionException | TimeoutException e) { failures.add("Unexpected exception:" + e.getMessage()); } } }); } } workers.shutdown(); workers.awaitTermination(120, TimeUnit.SECONDS); if (!failures.isEmpty()) { fail(failures.toString()); } }