Reputation: 21
I am trying to validate response body including errors in postman. How can I validate the response and text below?
{
"responseHeader": {
"publisherId": "12345",
"responseId": "abbcb15d79d54f5dbc473e502e2242c4abbcb15d79d54f5dbc473e502e224264",
"errors": [
{
"errorCode": "1004",
"errorMessage": "XXXX Not Found"
}
]
}
}
These are my tests which are failing:
tests['response json contains responseHeader'] = _.has(responseJSON, 'responseHeader');
tests['response json contains errors'] = _.has(responseJSON, 'responseHeader.publisherId');
tests["Response has publisher id"] = responseJSON.publisherId === 10003;
Upvotes: 0
Views: 17833
Reputation: 3664
You can use the Postman's expect
methods for assertion and Ajv
library to validate the response schema.
Below is the example that you need to put in postman request's Scripts->Post-Response
script.
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Response Schema",
"type": "object",
"properties": {
"responseHeader": {
"type": "object",
"properties": {
"publisherId": {"type": "string", "pattern": "^[0-9]+$"},
"responseId": {"type": "string", "pattern": "^[a-f0-9]+$"},
"errors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"errorCode": {"type": "string", "pattern": "^[0-9]+$"},
"errorMessage": {"type": "string"}
},
"required": ["errorCode", "errorMessage"]
}
}
},
"required": ["publisherId", "responseId", "errors"]
}
},
"required": ["responseHeader"]
};
const validate = ajv.compile(schema);
const valid = validate(pm.response.json());
if (!valid) {
console.log(validate.errors);
pm.test.fail('JSON is invalid');
} else {
console.log('JSON is valid');
// Validate each property
const responseHeader = pm.response.json().responseHeader;
pm.expect(responseHeader.publisherId).to.be.a('string');
pm.expect(responseHeader.publisherId).to.match(/^[0-9]+$/);
pm.expect(responseHeader.publisherId).to.be.equal("12345");
pm.expect(responseHeader.responseId).to.be.a('string');
pm.expect(responseHeader.responseId).to.match(/^[a-f0-9]+$/);
pm.expect(responseHeader.errors).to.be.an('array');
pm.expect(responseHeader.errors).to.have.lengthOf(1);
pm.expect(responseHeader.errors[0].errorCode).to.be.a('string');
pm.expect(responseHeader.errors[0].errorCode).to.match(/^[0-9]+$/);
pm.expect(responseHeader.errors[0].errorMessage).to.be.a('string');
}
This script will validate the response against the schema and fail the test if the response is invalid. It will also validate each property of the response and fail the test if any of the properties are invalid.
Upvotes: 0
Reputation: 9150
In the "Test" tab, parse your response body into an object, then use JavaScript to perform your tests.
var data = JSON.parse(responseBody);
tests["publisherId is 12345"] = data.responseHeader.publisherId === "12345";
Take a look at the test examples at the Postman site:
https://www.getpostman.com/docs/postman/scripts/test_scripts
https://www.getpostman.com/docs/postman/scripts/test_examples
Upvotes: 3