Skip to content

Commit

Permalink
Don't try to parse non XML value in XPath matcher (#1049)
Browse files Browse the repository at this point in the history
For better performance, don't try to parse non-XML values in the XPath matcher
  • Loading branch information
philippe-granet authored and tomakehurst committed Jan 13, 2019
1 parent 520fb86 commit 076244b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String getMatchesJsonPath() {
return expectedValue;
}

protected MatchResult isSimpleJsonPathMatch(String value) {
protected MatchResult isSimpleMatch(String value) {
try {
Object obj = JsonPath.read(value, expectedValue);

Expand Down Expand Up @@ -76,7 +76,7 @@ protected MatchResult isSimpleJsonPathMatch(String value) {

}

protected MatchResult isAdvancedJsonPathMatch(String value) {
protected MatchResult isAdvancedMatch(String value) {
Object obj = null;
try {
obj = JsonPath.read(value, expectedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.github.tomakehurst.wiremock.common.SilentErrorHandler;
import com.github.tomakehurst.wiremock.common.Xml;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import org.custommonkey.xmlunit.NamespaceContext;
import org.custommonkey.xmlunit.SimpleNamespaceContext;
import org.custommonkey.xmlunit.XMLUnit;
Expand Down Expand Up @@ -86,22 +85,14 @@ public Map<String, String> getXPathNamespaces() {
}

@Override
protected MatchResult isSimpleJsonPathMatch(String value) {
if (value == null) {
return MatchResult.noMatch();
}

protected MatchResult isSimpleMatch(String value) {
NodeList nodeList = findXmlNodesMatching(value);

return MatchResult.of(nodeList != null && nodeList.getLength() > 0);
}

@Override
protected MatchResult isAdvancedJsonPathMatch(String value) {
if (value == null) {
return MatchResult.noMatch();
}

protected MatchResult isAdvancedMatch(String value) {
NodeList nodeList = findXmlNodesMatching(value);
if (nodeList == null || nodeList.getLength() == 0) {
return MatchResult.noMatch();
Expand All @@ -118,6 +109,12 @@ protected MatchResult isAdvancedJsonPathMatch(String value) {
}

private NodeList findXmlNodesMatching(String value) {
// For performance reason, don't try to parse non XML value
if (value == null || !value.trim().startsWith("<")) {
notifier().info(String.format(
"Warning: failed to parse the XML document\nXML: %s", value));
return null;
}
try {
DocumentBuilder documentBuilder = Xml.newDocumentBuilderFactory().newDocumentBuilder();
documentBuilder.setErrorHandler(new SilentErrorHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public boolean isSimple() {
@Override
public MatchResult match(String value) {
if (isSimple()) {
return isSimpleJsonPathMatch(value);
return isSimpleMatch(value);
}

return isAdvancedJsonPathMatch(value);
return isAdvancedMatch(value);
}

protected abstract MatchResult isSimpleJsonPathMatch(String value);
protected abstract MatchResult isAdvancedJsonPathMatch(String value);
protected abstract MatchResult isSimpleMatch(String value);
protected abstract MatchResult isAdvancedMatch(String value);
}

0 comments on commit 076244b

Please sign in to comment.