HttpExceptionUtils { @SuppressWarnings("unchecked") public static void validateResponse(HttpURLConnection conn, int expectedStatus) throws IOException { if (conn.getResponseCode() != expectedStatus) { Exception toThrow; InputStream es = null; try { es = conn.getErrorStream(); Map json = JsonSerialization.mapReader().readValue(es); json = (Map) json.get(ERROR_JSON); String exClass = (String) json.get(ERROR_CLASSNAME_JSON); String exMsg = (String) json.get(ERROR_MESSAGE_JSON); if (exClass != null) { try { ClassLoader cl = HttpExceptionUtils.class.getClassLoader(); Class klass = cl.loadClass(exClass); Constructor constr = klass.getConstructor(String.class); toThrow = (Exception) constr.newInstance(exMsg); } catch (Exception ex) { toThrow = new IOException(String.format( "HTTP status [%d], exception [%s], message [%s], URL [%s]", conn.getResponseCode(), exClass, exMsg, conn.getURL())); } } else { String msg = (exMsg != null) ? exMsg : conn.getResponseMessage(); toThrow = new IOException(String.format( "HTTP status [%d], message [%s], URL [%s]", conn.getResponseCode(), msg, conn.getURL())); } } catch (Exception ex) { toThrow = new IOException(String.format( "HTTP status [%d], message [%s], URL [%s], exception [%s]", conn.getResponseCode(), conn.getResponseMessage(), conn.getURL(), ex.toString()), ex); } finally { if (es != null) { try { es.close(); } catch (IOException ex) { } } } throwEx(toThrow); } }  static void createServletExceptionResponse(
      HttpServletResponse response, int status, Throwable ex); static Response createJerseyExceptionResponse(Response.Status status,
      Throwable ex); @SuppressWarnings("unchecked") static void validateResponse(HttpURLConnection conn,
      int expectedStatus); static final String ERROR_JSON; static final String ERROR_EXCEPTION_JSON; static final String ERROR_CLASSNAME_JSON; static final String ERROR_MESSAGE_JSON; }
@Test public void testValidateResponseOK() throws IOException { HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getResponseCode()).thenReturn( HttpURLConnection.HTTP_CREATED); HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); }
@Test(expected = IOException.class) public void testValidateResponseFailNoErrorMessage() throws IOException { HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getResponseCode()).thenReturn( HttpURLConnection.HTTP_BAD_REQUEST); HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); }
@Test public void testValidateResponseNonJsonErrorMessage() throws IOException { String msg = "stream"; InputStream is = new ByteArrayInputStream(msg.getBytes()); HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getErrorStream()).thenReturn(is); Mockito.when(conn.getResponseMessage()).thenReturn("msg"); Mockito.when(conn.getResponseCode()).thenReturn( HttpURLConnection.HTTP_BAD_REQUEST); try { HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); Assert.fail(); } catch (IOException ex) { Assert.assertTrue(ex.getMessage().contains("msg")); Assert.assertTrue(ex.getMessage().contains("" + HttpURLConnection.HTTP_BAD_REQUEST)); } }
@Test public void testValidateResponseJsonErrorKnownException() throws IOException { Map<String, Object> json = new HashMap<String, Object>(); json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, IllegalStateException.class.getSimpleName()); json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, IllegalStateException.class.getName()); json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX"); Map<String, Object> response = new HashMap<String, Object>(); response.put(HttpExceptionUtils.ERROR_JSON, json); ObjectMapper jsonMapper = new ObjectMapper(); String msg = jsonMapper.writeValueAsString(response); InputStream is = new ByteArrayInputStream(msg.getBytes()); HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getErrorStream()).thenReturn(is); Mockito.when(conn.getResponseMessage()).thenReturn("msg"); Mockito.when(conn.getResponseCode()).thenReturn( HttpURLConnection.HTTP_BAD_REQUEST); try { HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); Assert.fail(); } catch (IllegalStateException ex) { Assert.assertEquals("EX", ex.getMessage()); } }
@Test public void testValidateResponseJsonErrorUnknownException() throws IOException { Map<String, Object> json = new HashMap<String, Object>(); json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, "FooException"); json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, "foo.FooException"); json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX"); Map<String, Object> response = new HashMap<String, Object>(); response.put(HttpExceptionUtils.ERROR_JSON, json); ObjectMapper jsonMapper = new ObjectMapper(); String msg = jsonMapper.writeValueAsString(response); InputStream is = new ByteArrayInputStream(msg.getBytes()); HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getErrorStream()).thenReturn(is); Mockito.when(conn.getResponseMessage()).thenReturn("msg"); Mockito.when(conn.getResponseCode()).thenReturn( HttpURLConnection.HTTP_BAD_REQUEST); try { HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED); Assert.fail(); } catch (IOException ex) { Assert.assertTrue(ex.getMessage().contains("EX")); Assert.assertTrue(ex.getMessage().contains("foo.FooException")); } }