ErrorResponseContentCreator { public byte[] createErrorContent(String requestUri, int statusCode, Optional<String> message) { String sanitizedString = message.map(StringUtil::sanitizeXmlString).orElse(""); String statusCodeString = Integer.toString(statusCode); writer.resetWriter(); try { writer.write("<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n<title>Error "); writer.write(statusCodeString); writer.write("</title>\n</head>\n<body>\n<h2>HTTP ERROR: "); writer.write(statusCodeString); writer.write("</h2>\n<p>Problem accessing "); writer.write(StringUtil.sanitizeXmlString(requestUri)); writer.write(". Reason:\n<pre> "); writer.write(sanitizedString); writer.write("</pre></p>\n<hr/>\n</body>\n</html>\n"); } catch (IOException e) { throw new RuntimeException(e); } return writer.getByteArray(); }  byte[] createErrorContent(String requestUri, int statusCode, Optional<String> message);  }
@Test public void response_content_matches_expected_string() { String expectedHtml = "<html>\n" + "<head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=ISO-8859-1\"/>\n" + "<title>Error 200</title>\n" + "</head>\n" + "<body>\n" + "<h2>HTTP ERROR: 200</h2>\n" + "<p>Problem accessing http: "<pre> My custom error message</pre></p>\n" + "<hr/>\n" + "</body>\n" + "</html>\n"; ErrorResponseContentCreator c = new ErrorResponseContentCreator(); byte[] rawContent = c.createErrorContent( "http: HttpServletResponse.SC_OK, Optional.of("My custom error message")); String actualHtml = new String(rawContent, StandardCharsets.ISO_8859_1); assertEquals(expectedHtml, actualHtml); }