HttpClientFactory { CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration, final HttpClientConfiguration httpClientConfiguration) throws SmartlingApiException { HttpClientBuilder httpClientBuilder = getHttpClientBuilder(); if (hasActiveProxyConfiguration(proxyConfiguration)) { HttpHost proxyHost = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort()); httpClientBuilder.setProxy(proxyHost); if (proxyAuthenticationRequired(proxyConfiguration)) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()), new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } httpClientBuilder.setSSLContext(getSSLConfig()); if (httpConfigurationRequired(httpClientConfiguration)) { final RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(httpClientConfiguration.getSocketTimeout()) .setConnectTimeout(httpClientConfiguration.getConnectionTimeout()) .setConnectionRequestTimeout(httpClientConfiguration.getConnectionRequestTimeout()) .build(); httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig); } return httpClientBuilder.build(); } HttpClientFactory();   }
@Test public void testGetHttpClientNoProxy() throws SmartlingApiException { CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); PowerMockito.when(httpClientBuilder.build()).thenReturn(closeableHttpClient); assertNotNull(httpClientFactory.getHttpClient(null, null)); verify(httpClientBuilder, never()).setProxy(any(HttpHost.class)); verify(httpClientBuilder, never()).setDefaultCredentialsProvider(any(CredentialsProvider.class)); verify(httpClientBuilder).build(); verify(httpClientBuilder).setSSLContext(context); }
@Test public void testGetHttpClientNoProxyConfig() throws SmartlingApiException { CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); PowerMockito.when(httpClientBuilder.build()).thenReturn(closeableHttpClient); ProxyConfiguration proxyConfiguration = new ProxyConfiguration(); assertNotNull(httpClientFactory.getHttpClient(proxyConfiguration, null)); verify(httpClientBuilder, never()).setProxy(any(HttpHost.class)); verify(httpClientBuilder, never()).setDefaultCredentialsProvider(any(CredentialsProvider.class)); verify(httpClientBuilder).build(); verify(httpClientBuilder).setSSLContext(context); }
@Test public void testGetHttpClientWithProxyConfig() throws Exception { ArgumentCaptor<CredentialsProvider> credentialsProviderCaptor = ArgumentCaptor.forClass(CredentialsProvider.class); ArgumentCaptor<HttpHost> httpHostArgumentCaptor = ArgumentCaptor.forClass(HttpHost.class); PowerMockito.when(httpClientBuilder.setProxy(httpHostArgumentCaptor.capture())).thenReturn(httpClientBuilder); PowerMockito.when(httpClientBuilder.setDefaultCredentialsProvider(credentialsProviderCaptor.capture())).thenReturn(httpClientBuilder); assertNotNull(httpClientFactory.getHttpClient(fillProxyConfiguration(), null)); HttpHost httpHost = httpHostArgumentCaptor.getValue(); Credentials credentials = credentialsProviderCaptor.getValue().getCredentials(new AuthScope(HOST, PORT)); assertEquals(HOST, httpHost.getHostName()); assertEquals(PORT, httpHost.getPort()); assertEquals(USERNAME, credentials.getUserPrincipal().getName()); assertEquals(PASSWORD, credentials.getPassword()); verify(httpClientBuilder).build(); }
@Test public void shouldNotConfigureAuthenticationIfUsernameIsMissing() throws Exception { ProxyConfiguration proxyConfiguration = fillProxyConfiguration(); proxyConfiguration.setUsername(null); CloseableHttpClient httpClient = httpClientFactory.getHttpClient(proxyConfiguration, null); assertNotNull(httpClient); verify(httpClientBuilder).setProxy(any(HttpHost.class)); verify(httpClientBuilder, never()).setDefaultCredentialsProvider(any(CredentialsProvider.class)); }
@Test public void shouldNotConfigureAuthenticationIfPasswordIsMissing() throws Exception { ProxyConfiguration proxyConfiguration = fillProxyConfiguration(); proxyConfiguration.setPassword(null); CloseableHttpClient httpClient = httpClientFactory.getHttpClient(proxyConfiguration, null); assertNotNull(httpClient); verify(httpClientBuilder).setProxy(any(HttpHost.class)); verify(httpClientBuilder, never()).setDefaultCredentialsProvider(any(CredentialsProvider.class)); }
@Test public void shouldConfigureHttpTimeouts() throws SmartlingApiException { ArgumentCaptor<RequestConfig> requestConfigArgumentCaptor = ArgumentCaptor.forClass(RequestConfig.class); HttpClientConfiguration httpClientConfiguration = httpClientConfiguration(); when(httpClientBuilder.setDefaultRequestConfig(any(RequestConfig.class))).thenReturn(httpClientBuilder); CloseableHttpClient httpClient = httpClientFactory.getHttpClient(null, httpClientConfiguration); assertNotNull(httpClient); verify(httpClientBuilder).setDefaultRequestConfig(requestConfigArgumentCaptor.capture()); verify(httpClientBuilder).build(); RequestConfig requestConfig = requestConfigArgumentCaptor.getValue(); assertEquals(httpClientConfiguration.getConnectionRequestTimeout(), requestConfig.getConnectionRequestTimeout()); assertEquals(httpClientConfiguration.getConnectionTimeout(), requestConfig.getConnectTimeout()); assertEquals(httpClientConfiguration.getSocketTimeout(), requestConfig.getSocketTimeout()); }
@Test public void shouldSkipSettingTimeoutsIfHttpClientConfigurationIsNull() throws SmartlingApiException { CloseableHttpClient httpClient = httpClientFactory.getHttpClient(null, null); assertNotNull(httpClient); verify(httpClientBuilder, never()).setDefaultRequestConfig(any(RequestConfig.class)); }