Skip to content

Commit defee8e

Browse files
authoredMay 2, 2024··
feat(REST): support httpAgent conf (#4328)
1 parent eb5cf19 commit defee8e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
 

‎docs/helpers/REST.md

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Type: [object][4]
2626
- `prettyPrintJson` **[boolean][6]?** pretty print json for response/request on console logs
2727
- `timeout` **[number][5]?** timeout for requests in milliseconds. 10000ms by default
2828
- `defaultHeaders` **[object][4]?** a list of default headers
29+
- `httpAgent` **[object][4]?** create an agent with SSL certificate
2930
- `onRequest` **[function][7]?** a async function which can update request object.
3031
- `onResponse` **[function][7]?** a async function which can update response object.
3132
- `maxUploadFileSize` **[number][5]?** set the max content file size in MB when performing api calls.
@@ -46,6 +47,25 @@ Type: [object][4]
4647
}
4748
}
4849
}
50+
```
51+
52+
With httpAgent
53+
54+
```js
55+
{
56+
helpers: {
57+
REST: {
58+
endpoint: 'http://site.com/api',
59+
prettyPrintJson: true,
60+
httpAgent: {
61+
key: fs.readFileSync(__dirname + '/path/to/keyfile.key'),
62+
cert: fs.readFileSync(__dirname + '/path/to/certfile.cert'),
63+
rejectUnauthorized: false,
64+
keepAlive: true
65+
}
66+
}
67+
}
68+
}
4969
```
5070

5171
## Access From Helpers

‎lib/helper/REST.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const axios = require('axios').default;
22
const Helper = require('@codeceptjs/helper');
3+
const { Agent } = require('https');
34
const Secret = require('../secret');
45

56
const { 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

Comments
 (0)
Please sign in to comment.