# REST
Extends Helper
REST helper allows to send additional requests to the REST API during acceptance tests. Axios (opens new window) library is used to perform requests.
# Configuration
Type: object (opens new window)
# Properties
endpoint
string (opens new window)? API base URLprettyPrintJson
boolean (opens new window)? pretty print json for response/request on console logs.printCurl
boolean (opens new window)? print cURL request on console logs. False by default.timeout
number (opens new window)? timeout for requests in milliseconds. 10000ms by default.defaultHeaders
object (opens new window)? a list of default headers.httpAgent
object (opens new window)? create an agent with SSL certificateonRequest
function (opens new window)? an async function which can update request object.onResponse
function (opens new window)? an async function which can update response object.maxUploadFileSize
number (opens new window)? set the max content file size in MB when performing api calls.
# 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
Send REST requests by accessing _executeRequest
method:
this.helpers['REST']._executeRequest({
url,
data,
});
# Methods
# Parameters
config
# _executeRequest
Executes axios request
# Parameters
request
any
Returns Promise (opens new window)
# _url
Generates url based on format sent (takes endpoint + url if latter lacks 'http')
# Parameters
url
any
# amBearerAuthenticated
Adds a header for Bearer authentication
// we use secret function to hide token from logs
I.amBearerAuthenticated(secret('heregoestoken'))
# Parameters
accessToken
(string (opens new window) | CodeceptJS.Secret) Bearer access token
# haveRequestHeaders
Sets request headers for all requests of this test
# Parameters
headers
object (opens new window) headers list
# sendDeleteRequest
Sends DELETE request to API.
I.sendDeleteRequest('/api/users/1');
# Parameters
url
anyheaders
object (opens new window) the headers object to be sent. By default, it is sent as an empty object
Returns Promise (opens new window)
# sendDeleteRequestWithPayload
Sends DELETE request to API with payload.
I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });
# Parameters
url
anypayload
any the payload to be sent. By default it is sent as an empty objectheaders
object (opens new window) the headers object to be sent. By default, it is sent as an empty object
Returns Promise (opens new window)
# sendGetRequest
Send GET request to REST API
I.sendGetRequest('/api/users.json');
# Parameters
url
anyheaders
object (opens new window) the headers object to be sent. By default, it is sent as an empty object
Returns Promise (opens new window)
# sendPatchRequest
Sends PATCH request to API.
I.sendPatchRequest('/api/users.json', { "email": "[email protected]" });
// To mask the payload in logs
I.sendPatchRequest('/api/users.json', secret({ "email": "[email protected]" }));
# Parameters
url
string (opens new window)payload
any the payload to be sent. By default it is sent as an empty objectheaders
object (opens new window) the headers object to be sent. By default it is sent as an empty object
Returns Promise (opens new window)
# sendPostRequest
Sends POST request to API.
I.sendPostRequest('/api/users.json', { "email": "[email protected]" });
// To mask the payload in logs
I.sendPostRequest('/api/users.json', secret({ "email": "[email protected]" }));
# Parameters
url
anypayload
any the payload to be sent. By default, it is sent as an empty objectheaders
object (opens new window) the headers object to be sent. By default, it is sent as an empty object
Returns Promise (opens new window)
# sendPutRequest
Sends PUT request to API.
I.sendPutRequest('/api/users.json', { "email": "[email protected]" });
// To mask the payload in logs
I.sendPutRequest('/api/users.json', secret({ "email": "[email protected]" }));
# Parameters
url
string (opens new window)payload
any the payload to be sent. By default it is sent as an empty objectheaders
object (opens new window) the headers object to be sent. By default it is sent as an empty object
Returns Promise (opens new window)
# setRequestTimeout
Set timeout for the request
I.setRequestTimeout(10000); // In milliseconds
# Parameters
newTimeout
number (opens new window) timeout in milliseconds