REST
Extends Helper
REST helper allows to send additional requests to the REST API during acceptance tests. Axios library is used to perform requests.
Configuration
Section titled “Configuration”Type: object
Properties
Section titled “Properties”endpointstring? API base URLprettyPrintJsonboolean? pretty print json for response/request on console logs.printCurlboolean? print cURL request on console logs. False by default.timeoutnumber? timeout for requests in milliseconds. 10000ms by default.defaultHeadersobject? a list of default headers.httpAgentobject? create an agent with SSL certificateonRequestfunction? an async function which can update request object.onResponsefunction? an async function which can update response object.maxUploadFileSizenumber? set the max content file size in MB when performing api calls.
Example
Section titled “Example”{ helpers: { REST: { endpoint: 'http://site.com/api', prettyPrintJson: true, onRequest: (request) => { request.headers.auth = '123'; } } }}With httpAgent
{ helpers: { REST: { endpoint: 'http://site.com/api', prettyPrintJson: true, httpAgent: { key: fs.readFileSync(__dirname + '/path/to/keyfile.key'), cert: fs.readFileSync(__dirname + '/path/to/certfile.cert'), rejectUnauthorized: false, keepAlive: true } } }}{ helpers: { REST: { endpoint: 'http://site.com/api', prettyPrintJson: true, httpAgent: { ca: fs.readFileSync(__dirname + '/path/to/ca.pem'), rejectUnauthorized: false, keepAlive: true } } }}Access From Helpers
Section titled “Access From Helpers”Send REST requests by accessing _executeRequest method:
this.helpers['REST']._executeRequest({ url, data,});Methods
Section titled “Methods”Parameters
Section titled “Parameters”config
_executeRequest
Section titled “_executeRequest”Executes axios request
Parameters
Section titled “Parameters”requestany
Returns Promise
Generates url based on format sent (takes endpoint + url if latter lacks ‘http’)
Parameters
Section titled “Parameters”urlany
amBearerAuthenticated
Section titled “amBearerAuthenticated”Adds a header for Bearer authentication
// we use secret function to hide token from logsI.amBearerAuthenticated(secret('heregoestoken'))Parameters
Section titled “Parameters”accessToken(string | CodeceptJS.Secret) Bearer access token
haveRequestHeaders
Section titled “haveRequestHeaders”Sets request headers for all requests of this test
Parameters
Section titled “Parameters”headersobject headers list
sendDeleteRequest
Section titled “sendDeleteRequest”Sends DELETE request to API.
I.sendDeleteRequest('/api/users/1');Parameters
Section titled “Parameters”urlanyheadersobject the headers object to be sent. By default, it is sent as an empty object
Returns Promise
sendDeleteRequestWithPayload
Section titled “sendDeleteRequestWithPayload”Sends DELETE request to API with payload.
I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });Parameters
Section titled “Parameters”urlanypayloadany the payload to be sent. By default it is sent as an empty objectheadersobject the headers object to be sent. By default, it is sent as an empty object
Returns Promise
sendGetRequest
Section titled “sendGetRequest”Send GET request to REST API
I.sendGetRequest('/api/users.json');Parameters
Section titled “Parameters”urlanyheadersobject the headers object to be sent. By default, it is sent as an empty object
Returns Promise
sendPatchRequest
Section titled “sendPatchRequest”Sends PATCH request to API.
I.sendPatchRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logsI.sendPatchRequest('/api/users.json', secret({ "email": "user@user.com" }));Parameters
Section titled “Parameters”urlstringpayloadany the payload to be sent. By default it is sent as an empty objectheadersobject the headers object to be sent. By default it is sent as an empty object
Returns Promise
sendPostRequest
Section titled “sendPostRequest”Sends POST request to API.
I.sendPostRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logsI.sendPostRequest('/api/users.json', secret({ "email": "user@user.com" }));Parameters
Section titled “Parameters”urlanypayloadany the payload to be sent. By default, it is sent as an empty objectheadersobject the headers object to be sent. By default, it is sent as an empty object
Returns Promise
sendPutRequest
Section titled “sendPutRequest”Sends PUT request to API.
I.sendPutRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logsI.sendPutRequest('/api/users.json', secret({ "email": "user@user.com" }));Parameters
Section titled “Parameters”urlstringpayloadany the payload to be sent. By default it is sent as an empty objectheadersobject the headers object to be sent. By default it is sent as an empty object
Returns Promise
setRequestTimeout
Section titled “setRequestTimeout”Set timeout for the request
I.setRequestTimeout(10000); // In millisecondsParameters
Section titled “Parameters”newTimeoutnumber timeout in milliseconds