Skip to content

Commit 6762db4

Browse files
david-amores-anzeshanholtzJenniferMah
authored
fix: Add http agent to axios to work with proxy (twilio#109)
Co-authored-by: Elise Shanholtz <[email protected]> Co-authored-by: Jennifer Mah <[email protected]>
1 parent 00dda63 commit 6762db4

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Formatters to take a JSON array and write to the stdout. Current formatters incl
3030

3131
A custom http client for the Twilio helper library to allow us to log API requests as well as modify the User-Agent header.
3232

33+
### Usage with proxy
34+
- `HTTP_PROXY`: If using Twilio CLI behind a proxy, set the URL of the proxy in an environment variable called `HTTP_PROXY`.
35+
3336
### Config
3437

3538
Manages the CLI configuration options, such as Twilio profiles and credentials.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"chalk": "^4.1.0",
3535
"columnify": "^1.5.4",
3636
"fs-extra": "^9.0.1",
37+
"https-proxy-agent": "^5.0.0",
3738
"inquirer": "^7.3.0",
3839
"qs": "^6.9.4",
3940
"semver": "^7.3.2",

src/services/cli-http-client.js

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const http_ = require('http');
22
const https = require('https');
33
const os = require('os');
44

5+
const HttpsProxyAgent = require('https-proxy-agent');
56
const qs = require('qs');
67

78
const pkg = require('../../package.json');
@@ -17,6 +18,14 @@ class CliRequestClient {
1718
this.commandName = commandName;
1819
this.logger = logger;
1920
this.http = http || require('axios');
21+
if (process.env.HTTP_PROXY) {
22+
/*
23+
* If environment variable HTTP_PROXY is set,
24+
* add an appropriate httpsAgent to axios.
25+
*/
26+
this.http.defaults.proxy = false;
27+
this.http.defaults.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);
28+
}
2029
}
2130

2231
/**

test/services/cli-http-client.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('services', () => {
3030
expect(response.body).to.equal('foo');
3131
});
3232

33+
test.it('should add the correct http agent for proxy', async () => {
34+
process.env.HTTP_PROXY = 'http://someproxy.com:8080';
35+
const client = new CliRequestClient('blah', logger, { defaults: {} });
36+
const httpAgent = client.http.defaults.httpsAgent;
37+
expect(httpAgent.proxy.host).to.equal('someproxy.com');
38+
expect(httpAgent.proxy.port).to.equal(8080);
39+
});
40+
3341
test
3442
.nock('https://foo.com', (api) => {
3543
api.get('/bar').delay(100).reply(200);

0 commit comments

Comments
 (0)