Special Variables
*
}}" for JSON and __STAR_VAR__ for XML responses.In this post, we will cover special variable only for the JSON responses.
For XML responses, please visit XML response validation scenarios.
Lets understand the concept of this variable by taking an example: Suppose we have an API which creates resource on server and returns the following JSON response:
{ "id": "54a79b704cba8d5328d087f5", "resource_name": "testcase", "resource_url": "http://vrest.io/i/demo/m/RVD/create_resource", "resource_description": "This API creates a resource on the server", "meta": { "created_at": "2015-01-03T07:41:21.000Z" } }
Here "id" and "created_at" are dynamic fields, which we do not know the values in advance. Now question comes, how we can test these type of responses. There are various approaches to this problem.
First approach, we define our own custom validator by duplicating the "Default Validator" in which we provide special checks for these two properties and rest of the properties will be checked according to "Default Validator". In this scenario, we need to define this custom validator for our test case.
- Second approach, we use "Default Schema Validator" and define only expected schema of the test case. In this scenario, we are only checking the schema of the response, not the values.
If we do not want to take hassle on creating our own custom validator and we don't want to rely on only schema validation and we are ok with ignoring the dynamic properties of the test case, then this variable can be very helpful in handling such scenarios.
Scenario 1: Ignore some of the JSON values while response validation
If you want to ignore some JSON values while response validation then you can specify this special variable against those JSON keys in the expected body for a test case. vREST simply replaces this special variable with the actual values received while test case execution in the expected body before sending the expected body to the validator for response validation. So, we can write our expected body like this:
{ "id": "{{*}}", "resource_name": "testcase", "resource_url": "http://vrest.io/i/demo/m/RVD/create_resource", "resource_description": "This API creates a resource on the server", "meta": { "created_at": "{{*}}" } }
vREST will simply replace this special variable with the actual values received before response validation. You can simply use the built-in "Default Validator" for the test case and you don't need to define any custom validator for the test case.
Scenario 2: Partial Response Validation
This special variable is also helpful in verifying partial JSON response. Suppose, in the above response, you want to validate only the resource_name and resource_url properties in the response, then you can write your expected body like this:
{ "{{*}}": "{{*}}", "resource_name": "testcase", "resource_url": "http://vrest.io/i/demo/m/RVD/create_resource" }
vREST will simply replace the other properties except "resouce_name" and "resource_url" in expected body from the actual response received before response validation.
Note: Please use this variable with caution because later on if you add a new JSON key to the response, that new JSON key will also get ignored and your test case will always pass.
Scenario 3: Ignore nested objects in the response
This special variable is also helpful in ignoring nested objects. Suppose, in the above response, you want to ignore the whole "meta" object, then you can write your expected body like this:
{ "id": "{{*}}", "resource_name": "testcase", "resource_url": "http://vrest.io/i/demo/m/RVD/create_resource", "resource_description": "This API creates a resource on the server", "meta": "{{*}}" }
vREST will simply replace the whole meta object and id property in expected body from the actual response received before response validation.
Â
Â