Reputation: 165
Refer to the below response body as an example, the only valid parameters of the response body are StatusCode and Message. We can add assertions and easily check if the fields are present with the correct value or not.
Now, I want to check if any unwanted parameters are being populated in the response and make the correction in this case. Is there any way to check this in postman using test assertions?
{
"statusCode": 200,
"message": "Here is the detail message",
"data": "Unwanted parameter"
"UnwantedLink": "www.unwantedlink.com"
}
Upvotes: 0
Views: 712
Reputation: 19929
use json schema validation :
you can use https://www.jsonschema.net/home to generate schema , click setting and settings and set options like:
schema = {
"type": "object",
"properties": {
"statusCode": { "type": "integer" },
"message": { "type": "string" }
},
"additionalProperties": false
};
pm.response.to.have.jsonSchema(schema)
Upvotes: 1
Reputation: 5917
You can check keys using chaijs
const res = pm.response.json();
pm.test("check keys in response", () => {
pm.expect(res).have.all.keys("statusCode", "message");
})
Example 2 cases below
Upvotes: 1