Skip to content

Commit

Permalink
resolve xml namespace weirdness ref #1587
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Jan 29, 2022
1 parent e14a379 commit 51bc8fd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,7 @@ You can adjust configuration settings for the HTTP client used by Karate using t
`driver` | JSON | See [UI Automation](karate-core)
`driverTarget` | JSON / Java Object | See [`configure driverTarget`](karate-core#configure-drivertarget)
`pauseIfNotPerf` | boolean | defaults to `false`, relevant only for performance-testing, see [`karate.pause()`](#karate-pause) and [`karate-gatling`](karate-gatling#think-time)
`xmlNamespaceAware` | boolean | defaults to `false`, to handle XML namespaces in [some special circumstances](https://github.com/karatelabs/karate/issues/1587)

Examples:
```cucumber
Expand Down
13 changes: 9 additions & 4 deletions karate-core/src/main/java/com/intuit/karate/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,14 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
}

}

public static Document toXmlDoc(String xml) {
return toXmlDoc(xml, false);
}

public static Document toXmlDoc(String xml, boolean namespaceAware) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaceAware);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
DtdEntityResolver dtdEntityResolver = new DtdEntityResolver();
Expand All @@ -130,7 +135,7 @@ public static Document toXmlDoc(String xml) {
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));
return toXmlDoc(toString(doc, false), namespaceAware);
} else {
return doc;
}
Expand Down Expand Up @@ -461,12 +466,12 @@ public static Document toNewDocument(Node in) {
return doc;
}

public static Document toXmlDoc(Object o) {
public static Document fromJavaObject(Object o) {
return fromObject("root", Json.of(o).value()); // keep it simple for people to write generic xpath starting with /root
}

public static String toXml(Object o) {
return toString(toXmlDoc(o));
return toString(fromJavaObject(o));
}

public static boolean isXml(String s) {
Expand Down
9 changes: 9 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class Config {
private List<String> nonProxyHosts;
private String localAddress;
private int responseDelay;
private boolean xmlNamespaceAware = false;
private boolean lowerCaseResponseHeaders = false;
private boolean corsEnabled = false;
private boolean logPrettyRequest;
Expand Down Expand Up @@ -164,6 +165,9 @@ public boolean configure(String key, Variable value) { // TODO use enum
case "responseDelay":
responseDelay = value.isNull() ? 0 : value.getAsInt();
return false;
case "xmlNamespaceAware":
xmlNamespaceAware = value.isTrue();
return false;
case "lowerCaseResponseHeaders":
lowerCaseResponseHeaders = value.isTrue();
return false;
Expand Down Expand Up @@ -344,6 +348,7 @@ public Config(Config parent) {
nonProxyHosts = parent.nonProxyHosts;
localAddress = parent.localAddress;
responseDelay = parent.responseDelay;
xmlNamespaceAware = parent.xmlNamespaceAware;
lowerCaseResponseHeaders = parent.lowerCaseResponseHeaders;
corsEnabled = parent.corsEnabled;
logPrettyRequest = parent.logPrettyRequest;
Expand Down Expand Up @@ -462,6 +467,10 @@ public int getResponseDelay() {
return responseDelay;
}

public boolean isXmlNamespaceAware() {
return xmlNamespaceAware;
}

public boolean isLowerCaseResponseHeaders() {
return lowerCaseResponseHeaders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ public Variable evalKarateExpression(String text, boolean forMatch) {
Json json = Json.of(text);
return evalEmbeddedExpressions(new Variable(json.value()), forMatch);
} else if (isXml(text)) {
Document doc = XmlUtils.toXmlDoc(text);
Document doc = XmlUtils.toXmlDoc(text, config.isXmlNamespaceAware());
return evalEmbeddedExpressions(new Variable(doc), forMatch);
} else if (isXmlPath(text)) {
return evalXmlPathOnVariableByName(RESPONSE, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public Node getAsXml() {
String xml = getAsString();
return XmlUtils.toXmlDoc(xml);
case OTHER: // POJO
return XmlUtils.toXmlDoc(value);
return XmlUtils.fromJavaObject(value);
default:
throw new RuntimeException("cannot convert to xml:" + this);
}
Expand Down

0 comments on commit 51bc8fd

Please sign in to comment.