ConsulReader { <T> Optional<T> parseHttpResponse(HttpResponse httpResponse, Function<HttpEntity, Optional<T>> method) throws ClientProtocolException { return parseHttpResponse(httpResponse, method, false); } ConsulReader(ObjectMapper mapper);   }
@Test public void should_throw_exception_when_status_out_of_range_200_299() { for (int status = 100; status < 600; status++) { if (status >= 200 && status < 300) continue; HttpResponse httpResponse = mock(HttpResponse.class); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("http1.1", 1, 1), status, "")); BasicHttpEntity givenHttpEntity = new BasicHttpEntity(); givenHttpEntity.setContent(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8))); when(httpResponse.getEntity()).thenReturn(givenHttpEntity); try { new ConsulReader(null).parseHttpResponse(httpResponse, this::getHttpEntity); fail("can't reach this point for status " + status); } catch (ClientProtocolException e) { assertThat(e.getMessage()).contains(String.valueOf(status)); } } }
@Test public void should_throw_exception_with_no_entity_when_status_out_of_range_200_299() { for (int status = 100; status < 600; status++) { if (status >= 200 && status < 300) continue; HttpResponse httpResponse = mock(HttpResponse.class); when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("http1.1", 1, 1), status, "")); try { new ConsulReader(null).parseHttpResponse(httpResponse, this::getHttpEntity); fail("can't reach this point for status " + status); } catch (ClientProtocolException e) { assertThat(e.getMessage()).contains(String.valueOf(status)); assertThat(e.getMessage()).contains("no content"); } } }