docs/helpers/JSONResponse.md
Extends Helper
This helper allows performing assertions on JSON responses paired with following helpers:
It can check status codes, response data, response structure.
requestHelper - a helper which will perform requests. REST by default, also Playwright or GraphQL can be used. Custom helpers must have onResponse hook in their config, which will be executed when request is performed.Zero-configuration when paired with REST:
// inside codecept.conf.js
{
helpers: {
REST: {
endpoint: 'http://site.com/api',
},
JSONResponse: {}
}
}
Explicitly setting request helper if you use makeApiRequest of Playwright to perform requests and not paired REST:
// inside codecept.conf.js
// ...
helpers: {
Playwright: {
url: 'https://localhost',
browser: 'chromium',
},
JSONResponse: {
requestHelper: 'Playwright',
}
}
If you plan to add custom assertions it is recommended to create a helper that will retrieve response object from JSONResponse:
// inside custom helper
const response = this.helpers.JSONResponse.response;
configChecks that response code is not equal to the provided one
I.dontSeeResponseCodeIs(500);
code number Checks for deep inclusion of a provided json in a response data.
// response.data == { data: { user: 1 } }
I.dontSeeResponseContainsJson({ user: 2 });
If an array is received, checks that no element of array contains json:
// response.data == [{ user: 1 }, { user: 3 }]
I.dontSeeResponseContainsJson({ user: 2 });
json objectChecks that response code is equal to the provided one
I.seeResponseCodeIs(200);
code number Checks that the response code is 4xx
Checks that the response code is 3xx
Checks that the response code is 5xx
Checks that the response code is 2xx Use it instead of seeResponseCodeIs(200) if server can return 204 instead.
I.seeResponseCodeIsSuccessful();
Checks for deep inclusion of a provided json in a response data.
// response.data == { user: { name: 'jon', email: '[email protected]' } }
I.seeResponseContainsJson({ user: { email: '[email protected]' } });
If an array is received, checks that at least one element contains JSON
// response.data == [{ user: { name: 'jon', email: '[email protected]' } }]
I.seeResponseContainsJson({ user: { email: '[email protected]' } });
json objectChecks for deep inclusion of a provided json in a response data.
// response.data == { user: { name: 'jon', email: '[email protected]' } }
I.seeResponseContainsKeys(['user']);
If an array is received, check is performed for each element of array:
// response.data == [{ user: 'jon' }, { user: 'matt'}]
I.seeResponseContainsKeys(['user']);
keys arrayChecks that response data equals to expected:
// response.data is { error: 'Not allowed' }
I.seeResponseEquals({ error: 'Not allowed' })
resp object Validates JSON structure of response using joi library. See joi API for complete reference on usage.
Use pre-initialized joi instance by passing function callback:
// response.data is { name: 'jon', id: 1 }
I.seeResponseMatchesJsonSchema(joi => {
return joi.object({
name: joi.string(),
id: joi.number()
})
});
// or pass a valid schema
const joi = require('joi');
I.seeResponseMatchesJsonSchema(joi.object({
name: joi.string(),
id: joi.number();
});
fnOrSchema any Executes a callback function passing in response object and assert
Use it to perform custom checks of response data
I.seeResponseValidByCallback(({ data, status }) => {
assert.strictEqual(status, 200);
assert('user' in data);
assert('company' in data);
});
fn function