HttpUtils { public StringResponse executeHttpCall(final HttpRequestBase httpRequest, final ProxyConfiguration proxyConfiguration, final HttpClientConfiguration httpClientConfiguration) throws SmartlingApiException { CloseableHttpClient httpClient = null; try { requestId.remove(); responseDetails.remove(); ProxyConfiguration newProxyConfiguration = mergeSystemProxyConfiguration(proxyConfiguration); logProxyConfiguration(newProxyConfiguration); httpClient = httpClientFactory.getHttpClient(newProxyConfiguration, httpClientConfiguration); addUserAgentHeader(httpRequest); final HttpResponse response = httpClient.execute(httpRequest); final String charset = EntityUtils.getContentCharSet(response.getEntity()); int statusCode = response.getStatusLine().getStatusCode(); Header header = response.getFirstHeader(X_SL_REQUEST_ID); if (header != null) { requestId.set(header.getValue()); } ResponseDetails details = new ResponseDetails(statusCode, response.getAllHeaders()); responseDetails.set(details); return inputStreamToString(response.getEntity().getContent(), charset, statusCode); } catch (final IOException ioe) { LOGGER.error(String.format(LOG_MESSAGE_ERROR_TEMPLATE, ioe.getMessage())); throw new SmartlingApiException(ioe); } finally { try { if (null != httpClient) httpClient.close(); } catch (final IOException ioe) { LOGGER.warn(String.format(LOG_MESSAGE_ERROR_TEMPLATE, ioe.getMessage())); } } } HttpUtils(); static ThreadLocal<String> getRequestId(); static ThreadLocal<ResponseDetails> getResponseDetails(); StringResponse executeHttpCall(final HttpRequestBase httpRequest, final ProxyConfiguration proxyConfiguration,
                                          final HttpClientConfiguration httpClientConfiguration);  }
@Test public void testExecuteHttpCallWithProxy() throws SmartlingApiException, IOException { when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); setSystemProxy(HttpUtils.SCHEME_HTTP, "", "", "", ""); setSystemProxy(HttpUtils.SCHEME_HTTPS, "", "", "", ""); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClientFactory.getHttpClient(proxyConfiguration, httpClientConfiguration)).thenReturn(httpClient); when(httpClient.execute(httpRequest)).thenReturn(httpResponse); StringResponse response = httpUtils.executeHttpCall(httpRequest, proxyConfiguration, httpClientConfiguration); assertEquals(TEST_RESPONSE, response.getContents()); verify(httpRequest).addHeader(HttpHeaders.USER_AGENT, USER_AGENT); }
@Test public void testExecuteHttpCallWithProxySystemHttp() throws SmartlingApiException, IOException { when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); setSystemProxy(HttpUtils.SCHEME_HTTP, HOST, PORT, USERNAME, PASSWORD); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); ArgumentCaptor<ProxyConfiguration> proxyConfigurationCaptor = ArgumentCaptor.forClass(ProxyConfiguration.class); when(httpClientFactory.getHttpClient(proxyConfigurationCaptor.capture(), eq(httpClientConfiguration))).thenReturn(httpClient); when(httpClient.execute(httpRequest)).thenReturn(httpResponse); StringResponse response = httpUtils.executeHttpCall(httpRequest, proxyConfiguration, httpClientConfiguration); assertEquals(TEST_RESPONSE, response.getContents()); ProxyConfiguration proxyConfiguration = proxyConfigurationCaptor.getValue(); assertEquals(HOST, proxyConfiguration.getHost()); assertEquals(Integer.parseInt(PORT), proxyConfiguration.getPort()); assertEquals(USERNAME, proxyConfiguration.getUsername()); assertEquals(PASSWORD, proxyConfiguration.getPassword()); }
@Test public void testExecuteHttpCallWithProxySystemHttps() throws SmartlingApiException, IOException { when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); setSystemProxy(HttpUtils.SCHEME_HTTPS, HOST, PORT, USERNAME, PASSWORD); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); ArgumentCaptor<ProxyConfiguration> proxyConfigurationCaptor = ArgumentCaptor.forClass(ProxyConfiguration.class); when(httpClientFactory.getHttpClient(proxyConfigurationCaptor.capture(), eq(httpClientConfiguration))).thenReturn(httpClient); when(httpClient.execute(httpRequest)).thenReturn(httpResponse); StringResponse response = httpUtils.executeHttpCall(httpRequest, proxyConfiguration, httpClientConfiguration); assertEquals(TEST_RESPONSE, response.getContents()); ProxyConfiguration proxyConfiguration = proxyConfigurationCaptor.getValue(); assertEquals(HOST, proxyConfiguration.getHost()); assertEquals(Integer.parseInt(PORT), proxyConfiguration.getPort()); assertEquals(USERNAME, proxyConfiguration.getUsername()); assertEquals(PASSWORD, proxyConfiguration.getPassword()); }