FailureExposingHttpClient extends AbstractHttpClientDecorator { public HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx) throws IOException, ClientProtocolException { HttpResponse resp = backend.execute(host, req, ctx); if (assessor.isFailure(resp)) { throw new UnsuccessfulResponseException(resp); } return resp; } FailureExposingHttpClient(HttpClient backend);  FailureExposingHttpClient(HttpClient backend, ResponseFailureAssessor assessor); HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx);  }
@Test public void returns1XXResponseAsIs() throws Exception { for(int i = 100; i <= 199; i++) { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, i, "1XX Thingy"); backend.setResponse(resp); assertSame(resp, impl.execute(host, req, ctx)); } }
@Test public void returns2XXResponseAsIs() throws Exception { for(int i = 200; i <= 299; i++) { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, i, "Success"); backend.setResponse(resp); assertSame(resp, impl.execute(host, req, ctx)); } }
@Test public void returns3XXResponseAsIs() throws Exception { for(int i = 300; i <= 399; i++) { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, i, "3XX Thingy"); backend.setResponse(resp); assertSame(resp, impl.execute(host, req, ctx)); } }
@Test public void generatesFailureOn4XXResponse() throws Exception { for(int i = 400; i <= 499; i++) { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, i, "Client Error"); backend.setResponse(resp); try { impl.execute(host, req, ctx); fail("should have thrown exception"); } catch (UnsuccessfulResponseException expected) { } } }
@Test public void generatesFailureOn5XXResponse() throws Exception { for(int i = 500; i <= 599; i++) { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, i, "Server Error"); backend.setResponse(resp); try { impl.execute(host, req, ctx); fail("should have thrown exception"); } catch (UnsuccessfulResponseException expected) { } } }
@Test(expected=UnsuccessfulResponseException.class) public void exposesFailureIfAssessorSaysTo() throws Exception { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); backend.setResponse(resp); impl = new FailureExposingHttpClient(backend, new ResponseFailureAssessor() { public boolean isFailure(HttpResponse response) { return true; } }); impl.execute(host, req, ctx); }
@Test public void doesNotExposeFailureIfAssessorSaysNotTo() throws Exception { resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Bork"); backend.setResponse(resp); impl = new FailureExposingHttpClient(backend, new ResponseFailureAssessor() { public boolean isFailure(HttpResponse response) { return false; } }); HttpResponse result = impl.execute(host, req, ctx); assertSame(result, resp); }