Skip to content

REST

Extends Helper

REST helper allows to send additional requests to the REST API during acceptance tests. Axios library is used to perform requests.

Type: object

  • endpoint string? API base URL
  • prettyPrintJson boolean? pretty print json for response/request on console logs.
  • printCurl boolean? print cURL request on console logs. False by default.
  • timeout number? timeout for requests in milliseconds. 10000ms by default.
  • defaultHeaders object? a list of default headers.
  • httpAgent object? create an agent with SSL certificate
  • onRequest function? an async function which can update request object.
  • onResponse function? an async function which can update response object.
  • maxUploadFileSize number? set the max content file size in MB when performing api calls.
{
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
}
}
}
}

Send REST requests by accessing _executeRequest method:

this.helpers['REST']._executeRequest({
url,
data,
});
  • config

Executes axios request

  • request any

Returns Promise response

Generates url based on format sent (takes endpoint + url if latter lacks ‘http’)

  • url any

Adds a header for Bearer authentication

// we use secret function to hide token from logs
I.amBearerAuthenticated(secret('heregoestoken'))
  • accessToken (string | CodeceptJS.Secret) Bearer access token

Sets request headers for all requests of this test

Sends DELETE request to API.

I.sendDeleteRequest('/api/users/1');
  • url any
  • headers object the headers object to be sent. By default, it is sent as an empty object

Returns Promise response

Sends DELETE request to API with payload.

I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });
  • url any
  • payload any the payload to be sent. By default it is sent as an empty object
  • headers object the headers object to be sent. By default, it is sent as an empty object

Returns Promise response

Send GET request to REST API

I.sendGetRequest('/api/users.json');
  • url any
  • headers object the headers object to be sent. By default, it is sent as an empty object

Returns Promise response

Sends PATCH request to API.

I.sendPatchRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logs
I.sendPatchRequest('/api/users.json', secret({ "email": "user@user.com" }));
  • url string
  • payload any the payload to be sent. By default it is sent as an empty object
  • headers object the headers object to be sent. By default it is sent as an empty object

Returns Promise response

Sends POST request to API.

I.sendPostRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logs
I.sendPostRequest('/api/users.json', secret({ "email": "user@user.com" }));
  • url any
  • payload any the payload to be sent. By default, it is sent as an empty object
  • headers object the headers object to be sent. By default, it is sent as an empty object

Returns Promise response

Sends PUT request to API.

I.sendPutRequest('/api/users.json', { "email": "user@user.com" });
// To mask the payload in logs
I.sendPutRequest('/api/users.json', secret({ "email": "user@user.com" }));
  • url string
  • payload any the payload to be sent. By default it is sent as an empty object
  • headers object the headers object to be sent. By default it is sent as an empty object

Returns Promise response

Set timeout for the request

I.setRequestTimeout(10000); // In milliseconds
  • newTimeout number timeout in milliseconds