JSONResponse
JSONResponse
Section titled “JSONResponse”Extends Helper
This helper allows performing assertions on JSON responses paired with following helpers:
- REST
- GraphQL
- Playwright
It can check status codes, response data, response structure.
Configuration
Section titled “Configuration”requestHelper- a helper which will perform requests.RESTby default, alsoPlaywrightorGraphQLcan be used. Custom helpers must haveonResponsehook in their config, which will be executed when request is performed.
Examples
Section titled “Examples”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', } }Access From Helpers
Section titled “Access From Helpers”If you plan to add custom assertions it is recommended to create a helper that will retrieve response object from JSONResponse:
// inside custom helperconst response = this.helpers.JSONResponse.response;Methods
Section titled “Methods”Parameters
Section titled “Parameters”config
dontSeeResponseCodeIs
Section titled “dontSeeResponseCodeIs”Checks that response code is not equal to the provided one
I.dontSeeResponseCodeIs(500);Parameters
Section titled “Parameters”codenumber
dontSeeResponseContainsJson
Section titled “dontSeeResponseContainsJson”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 });Parameters
Section titled “Parameters”jsonobject
seeResponseCodeIs
Section titled “seeResponseCodeIs”Checks that response code is equal to the provided one
I.seeResponseCodeIs(200);Parameters
Section titled “Parameters”codenumber
seeResponseCodeIsClientError
Section titled “seeResponseCodeIsClientError”Checks that the response code is 4xx
seeResponseCodeIsRedirection
Section titled “seeResponseCodeIsRedirection”Checks that the response code is 3xx
seeResponseCodeIsServerError
Section titled “seeResponseCodeIsServerError”Checks that the response code is 5xx
seeResponseCodeIsSuccessful
Section titled “seeResponseCodeIsSuccessful”Checks that the response code is 2xx Use it instead of seeResponseCodeIs(200) if server can return 204 instead.
I.seeResponseCodeIsSuccessful();seeResponseContainsJson
Section titled “seeResponseContainsJson”Checks for deep inclusion of a provided json in a response data.
// response.data == { user: { name: 'jon', email: 'jon@doe.com' } }
I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } });If an array is received, checks that at least one element contains JSON
// response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }]
I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } });Parameters
Section titled “Parameters”jsonobject
seeResponseContainsKeys
Section titled “seeResponseContainsKeys”Checks for deep inclusion of a provided json in a response data.
// response.data == { user: { name: 'jon', email: 'jon@doe.com' } }
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']);Parameters
Section titled “Parameters”keysarray
seeResponseEquals
Section titled “seeResponseEquals”Checks that response data equals to expected:
// response.data is { error: 'Not allowed' }
I.seeResponseEquals({ error: 'Not allowed' })Parameters
Section titled “Parameters”respobject
seeResponseMatchesJsonSchema
Section titled “seeResponseMatchesJsonSchema”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 schemaconst joi = require('joi');
I.seeResponseMatchesJsonSchema(joi.object({ name: joi.string(), id: joi.number();});Parameters
Section titled “Parameters”fnOrSchemaany
seeResponseValidByCallback
Section titled “seeResponseValidByCallback”Executes a callback function passing in response object and chai assertions with expect
Use it to perform custom checks of response data
I.seeResponseValidByCallback(({ data, status, expect }) => { expect(status).to.eql(200); expect(data).keys.to.include(['user', 'company']);});Parameters
Section titled “Parameters”fnfunction