Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(REST): support httpAgent conf #4328

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/helpers/REST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
29 changes: 28 additions & 1 deletion lib/helper/REST.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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.
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand Down
Loading