woocommerce/packages/js/api-core-tests/utils/request.js

71 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-08-23 17:39:03 +00:00
require('dotenv').config();
2021-09-07 22:20:44 +00:00
const { USER_KEY, USER_SECRET } = process.env;
2021-08-23 17:39:03 +00:00
const request = require('supertest')( API_PATH );
/**
* Make a GET request.
*
* @param {string} requestPath The path of the request.
* @param {object} queryString Optional. An object of one or more `key: value` query string parameters.
* @returns {Response}
*/
const getRequest = async ( requestPath, queryString = {} ) => {
const response = await request
.get( requestPath )
.set( 'Accept', 'application/json' )
.query( queryString )
2021-09-07 22:20:44 +00:00
.auth( USER_KEY, USER_SECRET );
2021-08-23 17:39:03 +00:00
return response;
2021-09-07 22:20:44 +00:00
};
2021-08-23 17:39:03 +00:00
/**
* Make a POST request.
*
* @param {string} requestPath The path of the request.
* @param {object} requestBody The body of the request to submit.
* @returns {Response}
*/
const postRequest = async ( requestPath, requestBody ) => {
const response = await request
.post( requestPath )
.send( requestBody )
.set( 'Accept', 'application/json' )
2021-09-07 22:20:44 +00:00
.auth( USER_KEY, USER_SECRET );
2021-08-23 17:39:03 +00:00
return response;
2021-09-07 22:20:44 +00:00
};
2021-08-23 17:39:03 +00:00
/**
* Make a PUT request.
*
* @param {string} requestPath The path of the request.
* @param {object} requestBody The body of the request to submit.
* @returns {Request}
*/
const putRequest = async ( requestPath, requestBody ) => {
const response = await request
.put( requestPath )
.send( requestBody )
.set( 'Accept', 'application/json' )
2021-09-07 22:20:44 +00:00
.auth( USER_KEY, USER_SECRET );
2021-08-23 17:39:03 +00:00
return response;
2021-09-07 22:20:44 +00:00
};
2021-08-23 17:39:03 +00:00
/**
* Make a DELETE request, optionally deleting the resource permanently.
*
* @param {string} requestPath The path of the request.
* @param {boolean} deletePermanently Flag to permanently delete the resource.
* @returns {Response}
*/
const deleteRequest = async ( requestPath, deletePermanently = false ) => {
const requestBody = deletePermanently ? { force: true } : {}
const response = await request
.delete( requestPath )
.set( 'Accept', 'application/json' )
.send( requestBody )
2021-09-07 22:20:44 +00:00
.auth( USER_KEY, USER_SECRET );
2021-08-23 17:39:03 +00:00
return response;
2021-09-07 22:20:44 +00:00
};
2021-08-23 17:39:03 +00:00
module.exports = { getRequest, postRequest, putRequest, deleteRequest }