【发布时间】:2019-11-03 12:42:48
【问题描述】:
我正在尝试运行在 URL 上调用 GET 的空手道测试,但我发现当网站以小写形式返回其 <!doctype 声明时(在“正常”HTML 中完全可以接受),我认为空手道XML 解析器抛出致命错误和警告。在我看来,空手道使用 XML 解析器,所以严格来说,这可能是正确的行为,因为小写 doctype 会中断。但是,对于有效的 HTML,我找不到解决此问题的方法。我玩过不同的标题等,但似乎无法超越这一点。
我已经包含了一个小测试,幸运的是 google.com 也返回了小写声明:
示例测试
Given url 'http://www.google.com'
When method GET
Then status 200
错误
[Fatal Error] :1:3: The markup in the document preceding the root element must be well-formed.
15:19:45.267 [main] WARN com.intuit.karate.FileUtils - parsing failed: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 3; The markup in the document preceding the root element must be well-formed.
<!doctype html><html .... blah
我下载了空手道源码,发现报的警告:
FileUtils.java
public static String toPrettyString(String raw) {
raw = StringUtils.trimToEmpty(raw);
try {
if (Script.isJson(raw)) {
return JsonUtils.toPrettyJsonString(JsonUtils.toJsonDoc(raw));
} else if (Script.isXml(raw)) {
return XmlUtils.toString(XmlUtils.toXmlDoc(raw), true);
}
} catch (Exception e) {
logger.warn("parsing failed: {}", e.getMessage());
}
return raw;
}
通过检查返回文档的第一个字符,检查似乎是在 JSON 或 XML 之间:
Script.java
public static final boolean isXml(String text) {
return text.startsWith("<");
}
XmlUtils.java
然后我认为 builder.parse 失败了,因为它不是有效的 XHTML,因为下面的注释暗示 <!doctype 将在递归调用中被删除。
public static Document toXmlDoc(String xml) {
...
Document doc = builder.parse(is);
if (dtdEntityResolver.dtdPresent) { // DOCTYPE present
// the XML was not parsed, but I think it hangs at the root as a text node
// so conversion to string and back has the effect of discarding the DOCTYPE !
return toXmlDoc(toString(doc, false));
是否可以将此流程转移到有效的 HTML 中?
【问题讨论】:
标签: java html xhtml sax karate