Issue #6520 - Fixing ErrorHandler output of text/html

+ Updating tests to ensure that output is xml verified
+ Updating output to use `<hr>` element properly.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2021-07-16 14:01:03 -05:00
parent 34c03ddf1d
commit cb2aeb3c7a
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
2 changed files with 19 additions and 2 deletions

View File

@ -386,7 +386,7 @@ public class ErrorHandler extends AbstractHandler
writeErrorPageStacks(request, writer);
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer, "<hr>", "<hr/>\n");
.writePoweredBy(writer, "<hr/>", "<hr/>\n");
}
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)

View File

@ -13,7 +13,9 @@
package org.eclipse.jetty.server;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -22,6 +24,9 @@ import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpHeader;
@ -38,6 +43,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.w3c.dom.Document;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
@ -561,7 +567,7 @@ public class ErrorHandlerTest
}
}
private String assertContent(HttpTester.Response response)
private String assertContent(HttpTester.Response response) throws Exception
{
String contentType = response.get(HttpHeader.CONTENT_TYPE);
String content = response.getContent();
@ -572,6 +578,17 @@ public class ErrorHandlerTest
assertThat(content, not(containsString("<glossary>")));
assertThat(content, not(containsString("<!DOCTYPE>")));
assertThat(content, not(containsString("&euro;")));
// we expect that our generated output conforms to text/xhtml is well formed
DocumentBuilderFactory xmlDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
xmlDocumentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = xmlDocumentBuilderFactory.newDocumentBuilder();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))
{
// We consider this content to be XML well formed if these 2 lines do not throw an Exception
Document doc = db.parse(inputStream);
doc.getDocumentElement().normalize();
}
}
else if (contentType.contains("text/json"))
{