In this post, we will see, how we can use variables in XML request body to make mock server more useful for our needs. Mock server request body matching works in the same way as we compare response body by using Default Validator for vREST Test cases.
We will now cover various scenarios to match XML request body:
Scenario 1: When we want to compare static XML request body in the incoming mock request
If we want to return some API mock response based on static XML request body then in that case, simply define the static XML request body in API Mock >> Request tab >> Raw Body sub tab.
Now, in the incoming mock request, if the request URL and method matches and request body of the incoming mock request matches exactly with the XML body specified in the above image then mock server will return the response specified in the response tab of this API mock.
Scenario 2: When we want to ignore values of some of the fields of the XML request body in the incoming mock request
Now, suppose in scenario 1, we don't want to compare the value of name and email value in the XML request body then we may write the raw body like this by using special variable __STAR_VAR__:
Scenario 3: When we want to only compare some specific data of the XML request body in the incoming mock request
Now, suppose we want to validate the existence of name tag and want to compare the value of email tag and we want to ignore rest of the properties in the XML request body, then we may write the raw body like this:
Scenario 4: When we want to use some part of the incoming XML request body in constructing the response of this mock API.
Now suppose we want to extract the name and email property from the incoming mock request and we want to use these properties in constructing the response of our Mock API. First we will see, how we may extract values from incoming XML request body:
To extract the name and email property from the request XML body, simply declare them as variables in the XML request body as shown below.
Now, you may use these extracted variables in the API mock response like this: