Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: XML whitespace trims while pretty print #2415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions karate-core/src/main/java/com/intuit/karate/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ public static String toString(Node node) {
}

public static String toString(Node node, boolean pretty) {
Node nodeToSerialize = node;
// In case of pretty string, we clone the node so that we don't modify the original node while trimming whitespaces
if (pretty) {
trimWhiteSpace(node);
nodeToSerialize = node.cloneNode(true);
trimWhiteSpace(nodeToSerialize);
}
DOMSource domSource = new DOMSource(node);
DOMSource domSource = new DOMSource(nodeToSerialize);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Expand Down
17 changes: 17 additions & 0 deletions karate-core/src/test/java/com/intuit/karate/XmlUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ void testPrettyPrint() {
assertEquals(temp, expected);
}

@Test
void testPrettyPrintWithWhiteSpace() {
String xml = "<foo><bar>baz</bar><ban><goo>moo </goo></ban></foo>";
Document doc = XmlUtils.toXmlDoc(xml);
String temp = XmlUtils.toString(doc, true);
String expected
= "<foo>\n"
+ " <bar>baz</bar>\n"
+ " <ban>\n"
+ " <goo>moo</goo>\n"
+ " </ban>\n"
+ "</foo>\n";

expected = expected.replace("\n", System.lineSeparator());
assertEquals(temp, expected);
}

@Test
void testCreatingNewDocumentFromSomeChildNode() {
String xml = "<root><foo><bar>baz</bar></foo></root>";
Expand Down
26 changes: 25 additions & 1 deletion karate-core/src/test/java/com/intuit/karate/core/xml/xml.feature
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,28 @@ Scenario: xml matching involving karate-schema substitutions
<a>x</a>
</root>
"""
* match test2 == schema
* match test2 == schema

Scenario: Xml whitespace trim after print bug fix
* def setForecastRequestXml =
"""
<myroot>
<myelement>T </myelement>
</myroot>
"""
* match setForecastRequestXml //myelement == 'T '
* print setForecastRequestXml
* match setForecastRequestXml //myelement == 'T '
* match setForecastRequestXml //myelement != 'T'

Scenario: Xml whitespace trim after karate.log bug fix
* def setForecastRequestXml =
"""
<myroot>
<myelement> T </myelement>
</myroot>
"""
* match setForecastRequestXml //myelement == ' T '
* karate.log(setForecastRequestXml)
* match setForecastRequestXml //myelement == ' T '
* match setForecastRequestXml //myelement != 'T'