From 81b7e3cf729d5ce8be3618cc76191b57847a0baf Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Mon, 29 Apr 2024 14:55:33 +0200 Subject: [PATCH] feat(REST): support httpAgent conf --- docs/helpers/REST.md | 20 ++++++++++++++++++++ lib/helper/REST.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/helpers/REST.md b/docs/helpers/REST.md index a23760624..cd96d5e0f 100644 --- a/docs/helpers/REST.md +++ b/docs/helpers/REST.md @@ -26,6 +26,7 @@ Type: [object][4] - `prettyPrintJson` **[boolean][6]?** pretty print json for response/request on console logs - `timeout` **[number][5]?** timeout for requests in milliseconds. 10000ms by default - `defaultHeaders` **[object][4]?** a list of default headers +- `httpAgent` **[object][4]?** create an agent with SSL certificate - `onRequest` **[function][7]?** a async function which can update request object. - `onResponse` **[function][7]?** a async function which can update response object. - `maxUploadFileSize` **[number][5]?** set the max content file size in MB when performing api calls. @@ -46,6 +47,25 @@ Type: [object][4] } } } +``` + + With httpAgent + +```js +{ + 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 + } + } + } +} ``` ## Access From Helpers diff --git a/lib/helper/REST.js b/lib/helper/REST.js index 88c214fb6..0af333464 100644 --- a/lib/helper/REST.js +++ b/lib/helper/REST.js @@ -1,5 +1,6 @@ const axios = require('axios').default; const Helper = require('@codeceptjs/helper'); +const { Agent } = require('https'); const Secret = require('../secret'); const { beautify } = require('../utils'); @@ -13,6 +14,7 @@ const { beautify } = require('../utils'); * @prop {boolean} [prettyPrintJson=false] - pretty print json for response/request on console logs * @prop {number} [timeout=1000] - timeout for requests in milliseconds. 10000ms by default * @prop {object} [defaultHeaders] - a list of default headers + * @prop {object} [httpAgent] - create an agent with SSL certificate * @prop {function} [onRequest] - a async function which can update request object. * @prop {function} [onResponse] - a async function which can update response object. * @prop {number} [maxUploadFileSize] - set the max content file size in MB when performing api calls. @@ -40,6 +42,24 @@ const config = {}; * } *} * ``` + * With httpAgent + * + * ```js + * { + * 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 + * } + * } + * } + * } + * ``` * * ## Access From Helpers * @@ -76,7 +96,14 @@ class REST extends Helper { this._setConfig(config); this.headers = { ...this.options.defaultHeaders }; - this.axios = axios.create(); + + // Create an agent with SSL certificate + if (this.options.httpAgent) { + if (!this.options.httpAgent.key || !this.options.httpAgent.cert) throw Error('Please recheck your httpAgent config!'); + this.httpsAgent = new Agent(this.options.httpAgent); + } + + this.axios = this.httpsAgent ? axios.create({ httpsAgent: this.httpsAgent }) : axios.create(); // @ts-ignore this.axios.defaults.headers = this.options.defaultHeaders; }