Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid XML path keys still evaluate in Rest Assured

Tags:

rest-assured

My example web service is returning following XML.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorResponse>
<errorCode>Wrong ID</errorCode>
<errorId>2</errorId>
</errorResponse>

Following tests are passed.

response.then().body("errorResponse.errorId", Matchers.is("2"));
response.then().body("errorResponse.errorCode", Matchers.is("Wrong ID"));

response.then().body("errorResponse1.errorCode", Matchers.is("Wrong ID"));
response.then().body("errorResponse2.errorCode", Matchers.is("Wrong ID"));

I understand that first two tests are fine, what I am not getting is why the last two are getting passed?

like image 307
Pankaj Avatar asked Jul 09 '18 16:07

Pankaj


1 Answers

Rest-assured uses its xml-path library to evaluate your hamcrest matcher and that library contains the XMLAssertion class which does the actual checking.

The source can be found on GitHub: https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/main/groovy/io/restassured/assertion/XMLAssertion.groovy

At line 60 of this file you can see it removes the part of the search key before the first dot, because it recognizes we're evaluating from a root node.

Hence your key:

"errorResponse3.errorCode" 

becomes

".errorCode"

So it turns out it doesn't matter what this initial path looks like, it assumes it's the name of the root node and discards it anyway.

like image 198
Luciano van der Veekens Avatar answered Oct 13 '22 21:10

Luciano van der Veekens