@Test public void getTextFromUrl() throws IOException { when(requestFactory.createRequest(URI.create(URL), HttpMethod.GET)).thenReturn(request); when(request.execute()).thenReturn(response); when(response.getStatusCode()).thenReturn(HttpStatus.OK); when(response.getBody()).then(new Answer<InputStream>() { @Override public InputStream answer(InvocationOnMock invocation) { byte[] data = TEXT.getBytes(Charsets.UTF_8); return new ByteArrayInputStream(data); } }); String result = downloader.getTextFromUrl(URL, Charsets.UTF_8); assertEquals(TEXT, result); ArgumentCaptor<Integer> connectTimeoutCaptor = ArgumentCaptor.forClass(Integer.class); verify(requestFactory).setConnectTimeout(connectTimeoutCaptor.capture()); assertTrue(connectTimeoutCaptor.getValue() > 0); ArgumentCaptor<Integer> readTimeoutCaptor = ArgumentCaptor.forClass(Integer.class); verify(requestFactory).setReadTimeout(readTimeoutCaptor.capture()); assertTrue(readTimeoutCaptor.getValue() > 0); }
String getTextFromUrl(String url, Charset encoding) throws IOException { ClientHttpResponse response = null; try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout((int) TIMEOUT); requestFactory.setReadTimeout((int) TIMEOUT); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), HttpMethod.GET); response = request.execute(); HttpStatus status = response.getStatusCode(); if (status.series() == Series.SUCCESSFUL) { return IOUtils.toString(response.getBody(), encoding); } } finally { if (response != null) { response.close(); } } return null; }
Downloader { String getTextFromUrl(String url, Charset encoding) throws IOException { ClientHttpResponse response = null; try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout((int) TIMEOUT); requestFactory.setReadTimeout((int) TIMEOUT); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), HttpMethod.GET); response = request.execute(); HttpStatus status = response.getStatusCode(); if (status.series() == Series.SUCCESSFUL) { return IOUtils.toString(response.getBody(), encoding); } } finally { if (response != null) { response.close(); } } return null; } }
Downloader { String getTextFromUrl(String url, Charset encoding) throws IOException { ClientHttpResponse response = null; try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout((int) TIMEOUT); requestFactory.setReadTimeout((int) TIMEOUT); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), HttpMethod.GET); response = request.execute(); HttpStatus status = response.getStatusCode(); if (status.series() == Series.SUCCESSFUL) { return IOUtils.toString(response.getBody(), encoding); } } finally { if (response != null) { response.close(); } } return null; }  }
Downloader { String getTextFromUrl(String url, Charset encoding) throws IOException { ClientHttpResponse response = null; try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout((int) TIMEOUT); requestFactory.setReadTimeout((int) TIMEOUT); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), HttpMethod.GET); response = request.execute(); HttpStatus status = response.getStatusCode(); if (status.series() == Series.SUCCESSFUL) { return IOUtils.toString(response.getBody(), encoding); } } finally { if (response != null) { response.close(); } } return null; }   }
Downloader { String getTextFromUrl(String url, Charset encoding) throws IOException { ClientHttpResponse response = null; try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout((int) TIMEOUT); requestFactory.setReadTimeout((int) TIMEOUT); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), HttpMethod.GET); response = request.execute(); HttpStatus status = response.getStatusCode(); if (status.series() == Series.SUCCESSFUL) { return IOUtils.toString(response.getBody(), encoding); } } finally { if (response != null) { response.close(); } } return null; }    }