11const axios = require ( 'axios' ) . default ;
22const Helper = require ( '@codeceptjs/helper' ) ;
3+ const { Agent } = require ( 'https' ) ;
34const Secret = require ( '../secret' ) ;
45
56const { beautify } = require ( '../utils' ) ;
@@ -13,6 +14,7 @@ const { beautify } = require('../utils');
1314 * @prop {boolean } [prettyPrintJson=false] - pretty print json for response/request on console logs
1415 * @prop {number } [timeout=1000] - timeout for requests in milliseconds. 10000ms by default
1516 * @prop {object } [defaultHeaders] - a list of default headers
17+ * @prop {object } [httpAgent] - create an agent with SSL certificate
1618 * @prop {function } [onRequest] - a async function which can update request object.
1719 * @prop {function } [onResponse] - a async function which can update response object.
1820 * @prop {number } [maxUploadFileSize] - set the max content file size in MB when performing api calls.
@@ -40,6 +42,24 @@ const config = {};
4042 * }
4143 *}
4244 * ```
45+ * With httpAgent
46+ *
47+ * ```js
48+ * {
49+ * helpers: {
50+ * REST: {
51+ * endpoint: 'http://site.com/api',
52+ * prettyPrintJson: true,
53+ * httpAgent: {
54+ * key: fs.readFileSync(__dirname + '/path/to/keyfile.key'),
55+ * cert: fs.readFileSync(__dirname + '/path/to/certfile.cert'),
56+ * rejectUnauthorized: false,
57+ * keepAlive: true
58+ * }
59+ * }
60+ * }
61+ * }
62+ * ```
4363 *
4464 * ## Access From Helpers
4565 *
@@ -76,7 +96,14 @@ class REST extends Helper {
7696 this . _setConfig ( config ) ;
7797
7898 this . headers = { ...this . options . defaultHeaders } ;
79- this . axios = axios . create ( ) ;
99+
100+ // Create an agent with SSL certificate
101+ if ( this . options . httpAgent ) {
102+ if ( ! this . options . httpAgent . key || ! this . options . httpAgent . cert ) throw Error ( 'Please recheck your httpAgent config!' ) ;
103+ this . httpsAgent = new Agent ( this . options . httpAgent ) ;
104+ }
105+
106+ this . axios = this . httpsAgent ? axios . create ( { httpsAgent : this . httpsAgent } ) : axios . create ( ) ;
80107 // @ts -ignore
81108 this . axios . defaults . headers = this . options . defaultHeaders ;
82109 }
0 commit comments