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

71 lines
2.0 KiB
JavaScript
Raw Normal View History

require( 'dotenv' ).config();
2021-09-07 22:20:44 +00:00
const { USER_KEY, USER_SECRET } = process.env;
const request = require( 'supertest' )( API_PATH );
2021-08-23 17:39:03 +00:00
/**
* 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.
* @return {Response}
2021-08-23 17:39:03 +00:00
*/
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.
* @return {Response}
2021-08-23 17:39:03 +00:00
*/
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.
* @return {Request}
2021-08-23 17:39:03 +00:00
*/
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.
2021-08-23 17:39:03 +00:00
* @param {boolean} deletePermanently Flag to permanently delete the resource.
* @return {Response}
2021-08-23 17:39:03 +00:00
*/
const deleteRequest = async ( requestPath, deletePermanently = false ) => {
const requestBody = deletePermanently ? { force: true } : {};
2021-08-23 17:39:03 +00:00
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 };