Skip to content

Instantly share code, notes, and snippets.

@ZhouMeichen
Created June 29, 2019 13:16
Show Gist options
  • Save ZhouMeichen/93c930f34200151edfdec9f2f33de717 to your computer and use it in GitHub Desktop.
Save ZhouMeichen/93c930f34200151edfdec9f2f33de717 to your computer and use it in GitHub Desktop.
API test using Postman (JSON Schema, Status code, Data value, Header, Response time)
//GET: https://reqres.in/api/users?page=2
var schema = {
"properties": {
"page": {
"type": "integer"
},
"per_page": {
"type": "integer",
"enum": [
1,
2,
3,
4
]
},
"total": {
"type": "integer"
},
"total_pages": {
"type": "integer"
},
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"email": {
"type": "string",
"pattern": "^([a-zA-Z0-9._-]+?@[a-zA-Z0-9]+?.[a-zA-Z]+?)$"
},
"first_name": {
"type": [
"string",
"null"
]
},
"last_name": {
"type": [
"string",
"null"
]
},
"avatar": {
"type": [
"string",
"null"
],
"pattern": "^((//|https?://).+|)$"
}
},
"additionalProperties": false,
"required": [
"id",
"email",
"first_name",
"last_name",
"avatar"
]
}
}
},
"additionalProperties": false,
"required": [
"page",
"per_page",
"total",
"total_pages",
"data"
]
};
pm.test('Schema is valid', function() {
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Total page is right", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.total_pages).to.eql(4);
});
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
//Response example:
/*
{
"page": 2,
"per_page": 3,
"total": 12,
"total_pages": 4,
"data": [
{
"id": 4,
"email": "[email protected]",
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
},
{
"id": 5,
"email": "[email protected]",
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
},
{
"id": 6,
"email": "[email protected]",
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
}
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment