1
1
const axios = require ( 'axios' ) . default ;
2
2
const Helper = require ( '@codeceptjs/helper' ) ;
3
+ const { Agent } = require ( 'https' ) ;
3
4
const Secret = require ( '../secret' ) ;
4
5
5
6
const { beautify } = require ( '../utils' ) ;
@@ -13,6 +14,7 @@ const { beautify } = require('../utils');
13
14
* @prop {boolean } [prettyPrintJson=false] - pretty print json for response/request on console logs
14
15
* @prop {number } [timeout=1000] - timeout for requests in milliseconds. 10000ms by default
15
16
* @prop {object } [defaultHeaders] - a list of default headers
17
+ * @prop {object } [httpAgent] - create an agent with SSL certificate
16
18
* @prop {function } [onRequest] - a async function which can update request object.
17
19
* @prop {function } [onResponse] - a async function which can update response object.
18
20
* @prop {number } [maxUploadFileSize] - set the max content file size in MB when performing api calls.
@@ -40,6 +42,24 @@ const config = {};
40
42
* }
41
43
*}
42
44
* ```
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
+ * ```
43
63
*
44
64
* ## Access From Helpers
45
65
*
@@ -76,7 +96,14 @@ class REST extends Helper {
76
96
this . _setConfig ( config ) ;
77
97
78
98
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 ( ) ;
80
107
// @ts -ignore
81
108
this . axios . defaults . headers = this . options . defaultHeaders ;
82
109
}
0 commit comments