HttpUtils { public static <T> ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor, Predicate<Response> successPredicate, String tag) { return response -> { if (!successPredicate.test(response)) { throw createExceptionForResponse(response, tag); } return processor.process(response); }; } private  HttpUtils(); static T waitFor(Future<? extends T> future); static Throwable unwrapCompletionException(Throwable ex); static boolean isSuccessStatus(int status); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor,
                                                         Predicate<Response> successPredicate,
                                                         String tag); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor,
                                                         Predicate<Response> successPredicate); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor, String tag); static ResponseProcessor<T> checkResponse(ResponseProcessor<T> processor); static Predicate<Response> hasStatus(int status); static FailedRequestException createExceptionForResponse(Response response, String tag); static ResponseProcessor<T> jsonResult(ObjectMapper mapper, Class<T> resultClass); static ResponseProcessor<T> jsonResult(ObjectMapper mapper, TypeReference<T> typeReference); static Consumer<RequestBuilder> get(String uri); static ResponseProcessor<T> nullProcessor(); static String urlEncode(String src); static String addQueryParameters(String url, Map<String, ?> params); static String addQueryParameters(String url, Map<String, ?> params, boolean filterUndefined); static String addQueryParameter(String url, String key, Object value); static final Predicate<Response> SUCCESS_STATUS; }
@Test public void testCheckResponseSuccess() throws IOException { Response response = mock(Response.class); ResponseProcessor<Object> processor = createProcessorMock(); Object result = new Object(); when(response.isSuccess()).thenReturn(Boolean.TRUE); when(processor.process(response)).thenReturn(result); ResponseProcessor<Object> checkProcessor = HttpUtils.checkResponse(processor); Object processorResult = checkProcessor.process(response); assertThat(processorResult).isEqualTo(result); }
@Test public void testCheckResponseFailed() throws IOException { final int status = 500; Response response = mock(Response.class); ResponseProcessor<Object> processor = createProcessorMock(); when(response.isSuccess()).thenReturn(Boolean.FALSE); when(response.statusCode()).thenReturn(status); ResponseProcessor<Object> checkProcessor = HttpUtils.checkResponse(processor); try { checkProcessor.process(response); fail("No exception thrown!"); } catch (FailedRequestException e) { assertThat(e.getMessage()).contains(String.valueOf(status)); assertThat(e.getStatusCode()).isEqualTo(status); assertThat(e.getTag()).isNull(); verifyZeroInteractions(processor); } }
@Test public void testCheckResponseFailedWithTag() throws IOException { final int status = 400; final String tag = "MyTestRequest"; Response response = mock(Response.class); ResponseProcessor<Object> processor = createProcessorMock(); when(response.isSuccess()).thenReturn(Boolean.FALSE); when(response.statusCode()).thenReturn(status); ResponseProcessor<Object> checkProcessor = HttpUtils.checkResponse(processor, tag); try { checkProcessor.process(response); fail("No exception thrown!"); } catch (FailedRequestException e) { assertThat(e.getMessage()).contains(String.valueOf(status), tag); assertThat(e.getStatusCode()).isEqualTo(status); assertThat(e.getTag()).isEqualTo(tag); verifyZeroInteractions(processor); } }
@Test public void testCheckResponseStatusFailedBodyStreamThrowsException() throws IOException { final int status = 401; final String tag = "failed_request_with_broken_response"; Response response = mock(Response.class); ResponseProcessor<Object> processor = createProcessorMock(); when(response.isSuccess()).thenReturn(Boolean.FALSE); when(response.statusCode()).thenReturn(status); when(response.bodyStream()).thenReturn(new BrokenInputStream() { @Override public void close() { } }); ResponseProcessor<Object> checkProcessor = HttpUtils.checkResponse(processor, tag); try { checkProcessor.process(response); fail("No exception thrown!"); } catch (FailedRequestException e) { assertThat(e.getMessage()).contains(String.valueOf(status), tag); assertThat(e.getStatusCode()).isEqualTo(status); assertThat(e.getTag()).isEqualTo(tag); verifyZeroInteractions(processor); } }