HttpDriver implements Closeable { public String doGet(final String url) throws IOException { LOGGER.debug("Requesting api for {}", url); HttpGet httpGet = new HttpGet(url); for (int i = 0; i < clientMaxRetries; i++) { try (CloseableHttpResponse response = client.execute(httpGet)) { if (response != null) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { String data = EntityUtils.toString(response.getEntity()); LOGGER.debug("Data received: {}, from: {}", data, url); return data; } LOGGER.error("Received bad status: {} from: {}", statusCode, url); response.getEntity().getContent().close(); return ""; } } catch (IOException ex) { LOGGER.error("Failed to get response from server {} retry: {}/{}, exception: ", url, i, clientMaxRetries, ex); try { TimeUnit.MILLISECONDS.sleep(clientRetryIntervalMs); } catch (InterruptedException ignored) { } } } throw new IOException("Failed to get response from server: " + url); } HttpDriver(Builder builder); void setHttpClient(CloseableHttpClient httpClient); static SSLContext createSSLContext(String trustorePath, char[] trustorePassword, String certPath,
                                          String keyPath); @Override void close(); String doGet(final String url); String doPost(HttpPost httpPost); String doPost(final String url, final List<NameValuePair> fields);  }
@Test public void testDoGet() throws IOException { CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class); HttpEntity entity = Mockito.mock(HttpEntity.class); String data = "Sample Server Response"; Mockito.when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK" )); Mockito.when(entity.getContent()).thenReturn(new ByteArrayInputStream(data.getBytes())); Mockito.when(httpResponse.getEntity()).thenReturn(entity); Mockito.when(httpClient.execute(Mockito.any(HttpGet.class))).thenReturn(httpResponse); HttpDriver httpDriver = new HttpDriver.Builder("", null, "asdf".toCharArray(), null, null) .build(); httpDriver.setHttpClient(httpClient); String url = "https: String out = httpDriver.doGet(url); Assert.assertEquals(out, data); }