@Test public void testJsonReadException() throws IOException { InputStream inputStream = mock(InputStream.class); IOException expectedException = new IOException("test exception"); when(inputStream.read()).thenThrow(expectedException); when(inputStream.read(any(byte[].class))).thenThrow(expectedException); when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException); try { handler.handle(null, mockResponse() .contentType(JSON_UTF_8) .body(inputStream) .build()); fail("expected exception"); } catch (RuntimeException e) { assertEquals(e.getMessage(), "Error reading response from server"); assertSame(e.getCause(), expectedException); } }
@Override public JsonResponse<T> handle(Request request, Response response) { byte[] bytes = readResponseBytes(response); String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes); } return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes); }
FullJsonResponseHandler implements ResponseHandler<JsonResponse<T>, RuntimeException> { @Override public JsonResponse<T> handle(Request request, Response response) { byte[] bytes = readResponseBytes(response); String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes); } return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes); } }
FullJsonResponseHandler implements ResponseHandler<JsonResponse<T>, RuntimeException> { @Override public JsonResponse<T> handle(Request request, Response response) { byte[] bytes = readResponseBytes(response); String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes); } return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes); } private  FullJsonResponseHandler(JsonCodec<T> jsonCodec); }
FullJsonResponseHandler implements ResponseHandler<JsonResponse<T>, RuntimeException> { @Override public JsonResponse<T> handle(Request request, Response response) { byte[] bytes = readResponseBytes(response); String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes); } return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes); } private  FullJsonResponseHandler(JsonCodec<T> jsonCodec); static FullJsonResponseHandler<T> createFullJsonResponseHandler(JsonCodec<T> jsonCodec); @Override JsonResponse<T> handleException(Request request, Exception exception); @Override JsonResponse<T> handle(Request request, Response response); }
FullJsonResponseHandler implements ResponseHandler<JsonResponse<T>, RuntimeException> { @Override public JsonResponse<T> handle(Request request, Response response) { byte[] bytes = readResponseBytes(response); String contentType = response.getHeader(CONTENT_TYPE); if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes); } return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes); } private  FullJsonResponseHandler(JsonCodec<T> jsonCodec); static FullJsonResponseHandler<T> createFullJsonResponseHandler(JsonCodec<T> jsonCodec); @Override JsonResponse<T> handleException(Request request, Exception exception); @Override JsonResponse<T> handle(Request request, Response response);  }