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);  }
@Test public void testValidJson() { User user = new User("Joe", 25); String json = codec.toJson(user); JsonResponse<User> response = handler.handle(null, mockResponse().contentType(JSON_UTF_8).body(json).build()); assertTrue(response.hasValue()); assertEquals(response.getJsonBytes(), json.getBytes(StandardCharsets.UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getValue().getName(), user.getName()); assertEquals(response.getValue().getAge(), user.getAge()); assertNotSame(response.getJson(), response.getJson()); assertNotSame(response.getJsonBytes(), response.getJsonBytes()); assertNotSame(response.getResponseBytes(), response.getResponseBytes()); assertNotSame(response.getResponseBody(), response.getResponseBody()); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testInvalidJson() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse().contentType(JSON_UTF_8).body(json).build()); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), format("Unable to create %s from JSON response:\n[%s]", User.class, json)); assertInstanceOf(response.getException().getCause(), IllegalArgumentException.class); assertEquals(response.getException().getCause().getMessage(), "Invalid JSON bytes for [simple type, class com.proofpoint.http.client.TestFullJsonResponseHandler$User]"); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testInvalidJsonGetValue() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse().contentType(JSON_UTF_8).body(json).build()); try { response.getValue(); fail("expected exception"); } catch (IllegalStateException e) { assertEquals(e.getMessage(), "Response does not contain a JSON value"); assertEquals(e.getCause(), response.getException()); assertEquals(response.getJsonBytes(), json.getBytes(StandardCharsets.UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); } }
@Test public void testNonJsonResponse() { JsonResponse<User> response = handler.handle(null, mockResponse().contentType(PLAIN_TEXT_UTF_8).body("hello").build()); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(StandardCharsets.UTF_8)); assertEquals(response.getResponseBody(), "hello"); }
@Test public void testMissingContentType() { JsonResponse<User> response = handler.handle(null, mockResponse().body("hello").build()); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(StandardCharsets.UTF_8)); assertEquals(response.getResponseBody(), "hello"); assertTrue(response.getHeaders().isEmpty()); }
@Test public void testJsonErrorResponse() { String json = "{\"error\": true}"; JsonResponse<User> response = handler.handle(null, mockResponse().status(INTERNAL_SERVER_ERROR).contentType(JSON_UTF_8).body(json).build()); assertTrue(response.hasValue()); assertEquals(response.getJson(), json); assertEquals(response.getJsonBytes(), json.getBytes(StandardCharsets.UTF_8)); assertNull(response.getValue().getName()); assertEquals(response.getValue().getAge(), 0); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@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); } }