Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) {
private RuntimeException requestFailedException(String task, Request request, JsonResponse<QueryResults> response) { gone.set(true); if (!response.hasValue()) { return new RuntimeException( format("Error %s at %s returned an invalid response: %s [Error: %s]", task, request.getUri(), response, response.getResponseBody()), response.getException()); } return new RuntimeException(format("Error %s at %s returned %s: %s", task, request.getUri(), response.getStatusCode(), response.getStatusMessage())); }
@Test public void testNonJsonResponse() { JsonResponse<User> response = handler.handle(null, mockResponse(OK, PLAIN_TEXT_UTF_8, "hello")); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); }
Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) {
Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) {
Exception cause = response.getException(); if (cause == null) { if (response.getStatusCode() == HttpStatus.OK.code()) {
@Test public void testInvalidJsonGetValue() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); 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(UTF_8)); 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(OK, JSON_UTF_8, json)); 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(UTF_8)); assertEquals(response.getJson(), json); 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(OK, JSON_UTF_8, json)); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), "Unable to create " + User.class + " from JSON response:\n" + json); assertTrue(response.getException().getCause() instanceof IllegalArgumentException); assertEquals(response.getException().getCause().getMessage(), "Invalid [simple type, class io.airlift.http.client.TestFullJsonResponseHandler$User] json bytes"); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); 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(OK, JSON_UTF_8, json)); 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(UTF_8)); assertEquals(response.getJson(), json); 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(OK, JSON_UTF_8, json)); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), format("Unable to create %s from JSON response:\n[%s]", User.class, json)); assertTrue(response.getException().getCause() instanceof IllegalArgumentException); assertEquals(response.getException().getCause().getMessage(), "Invalid JSON bytes for [simple type, class io.airlift.http.client.TestFullJsonResponseHandler$User]"); assertEquals(response.getJsonBytes(), json.getBytes(UTF_8)); assertEquals(response.getJson(), json); assertEquals(response.getResponseBytes(), response.getJsonBytes()); assertEquals(response.getResponseBody(), response.getJson()); }
@Test public void testMissingContentType() { JsonResponse<User> response = handler.handle(null, new TestingResponse(OK, ImmutableListMultimap.<String, String>of(), "hello".getBytes(UTF_8))); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); assertTrue(response.getHeaders().isEmpty()); }
@Test public void testMissingContentType() { JsonResponse<User> response = handler.handle(null, new TestingResponse(OK, ImmutableListMultimap.<String, String>of(), "hello".getBytes(UTF_8))); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); assertTrue(response.getHeaders().isEmpty()); }
@Test public void testMissingContentType() { JsonResponse<User> response = handler.handle(null, new TestingResponse(OK, ImmutableListMultimap.<String, String>of(), "hello".getBytes(UTF_8))); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); assertTrue(response.getHeaders().isEmpty()); }
@Test public void testInvalidJson() { String json = "{\"age\": \"foo\"}"; JsonResponse<User> response = handler.handle(null, mockResponse(OK, JSON_UTF_8, json)); assertFalse(response.hasValue()); assertEquals(response.getException().getMessage(), format("Unable to create %s from JSON response:\n[%s]", User.class, json)); assertTrue(response.getException().getCause() instanceof IllegalArgumentException); assertEquals(response.getException().getCause().getMessage(), "Invalid JSON bytes for [simple type, class io.airlift.http.client.TestFullJsonResponseHandler$User]"); assertEquals(response.getJsonBytes(), json.getBytes(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(OK, PLAIN_TEXT_UTF_8, "hello")); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); }
@Test public void testNonJsonResponse() { JsonResponse<User> response = handler.handle(null, mockResponse(OK, PLAIN_TEXT_UTF_8, "hello")); assertFalse(response.hasValue()); assertNull(response.getException()); assertNull(response.getJson()); assertNull(response.getJsonBytes()); assertEquals(response.getResponseBytes(), "hello".getBytes(UTF_8)); assertEquals(response.getResponseBody(), "hello"); }