HttpFetcher { public HttpEntity fetch(String url, String acceptHeader) throws HttpFetchException { return fetch(url, acceptHeader, PLAIN_URI_PROVIDER, PLAIN_GET_REQUEST_PROVIDER); } HttpFetcher(CookieStore cookieStore,
	                   HttpClient client, HttpFetchConfiguration settings);  HttpFetcher(HttpFetchConfiguration settings); void ensureCookie(); void clearCookie(); HttpEntity fetch(String url, String acceptHeader); HttpEntity fetch(String identifier,
	                        String acceptHeader,
	                        UriProvider uriProvider, RequestProvider requestProvider); CookieStore getCookieStore(); HttpClient getClient(); HttpFetchConfiguration getConfiguration();  }
@Test public void testBodyShouldBeFetched() throws Exception { HttpFetchConfiguration settings = new HttpFetchConfigurationBuilder().build(); HttpFetcher httpFetcher = new HttpFetcher(settings); stubFor(get(urlEqualTo("/")).willReturn(aResponse().withBody("body"))); HttpEntity responseEntity = httpFetcher.fetch("http: String bodyContent = readStream(responseEntity.getContent()); EntityUtils.consume(responseEntity); assertEquals("body", bodyContent); }
@Test public void testRetriesRequests() throws IOException { final int retries = 3; HttpFetchConfiguration settings = new HttpFetchConfigurationBuilder().setRetries(retries).build(); HttpFetcher httpFetcher = new HttpFetcher(settings); stubFor(get(urlEqualTo("/1")).willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR))); stubFor(get(urlEqualTo("/2")).willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR))); stubFor(get(urlEqualTo("/3")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("body"))); HttpEntity responseEntity = httpFetcher.fetch("http: String bodyContent = readStream(responseEntity.getContent()); EntityUtils.consume(responseEntity); assertThat(bodyContent, equalTo("body")); }
@Test public void testRetriesRequests_for_bad_request() throws IOException { final int retries = 2; HttpFetchConfiguration settings = new HttpFetchConfigurationBuilder().setRetries(retries).build(); HttpFetcher httpFetcher = new HttpFetcher(settings); stubFor(get(urlEqualTo("/1")).willReturn(aResponse().withStatus(HttpStatus.SC_BAD_REQUEST))); stubFor(get(urlEqualTo("/2")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("body"))); HttpEntity responseEntity = httpFetcher.fetch("http: String bodyContent = readStream(responseEntity.getContent()); EntityUtils.consume(responseEntity); assertThat(bodyContent, equalTo("body")); }
@Test public void testUsesProviders() throws IOException { RequestProvider requestProvider = mock(RequestProvider.class); when(requestProvider.getRequest()).thenReturn(new HttpGet()); HttpFetchConfiguration settings = new HttpFetchConfigurationBuilder().build(); HttpFetcher httpFetcher = new HttpFetcher(settings); stubFor(get(urlEqualTo("/1")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody("body"))); HttpEntity responseEntity = httpFetcher.fetch("1", "*/*", new AppendingUriProvider(), requestProvider); String bodyContent = readStream(responseEntity.getContent()); EntityUtils.consume(responseEntity); assertThat(bodyContent, equalTo("body")); verify(requestProvider).getRequest(); }