Skip to content

Commit

Permalink
fix: Add http agent to axios to work with proxy (twilio#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Elise Shanholtz <eshanholtz@twilio.com>
Co-authored-by: Jennifer Mah <42650198+JenniferMah@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 28, 2021
1 parent 00dda63 commit 6762db4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Formatters to take a JSON array and write to the stdout. Current formatters incl

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

### Usage with proxy
- `HTTP_PROXY`: If using Twilio CLI behind a proxy, set the URL of the proxy in an environment variable called `HTTP_PROXY`.

### Config

Manages the CLI configuration options, such as Twilio profiles and credentials.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"chalk": "^4.1.0",
"columnify": "^1.5.4",
"fs-extra": "^9.0.1",
"https-proxy-agent": "^5.0.0",
"inquirer": "^7.3.0",
"qs": "^6.9.4",
"semver": "^7.3.2",
Expand Down
9 changes: 9 additions & 0 deletions src/services/cli-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const http_ = require('http');
const https = require('https');
const os = require('os');

const HttpsProxyAgent = require('https-proxy-agent');
const qs = require('qs');

const pkg = require('../../package.json');
Expand All @@ -17,6 +18,14 @@ class CliRequestClient {
this.commandName = commandName;
this.logger = logger;
this.http = http || require('axios');
if (process.env.HTTP_PROXY) {
/*
* If environment variable HTTP_PROXY is set,
* add an appropriate httpsAgent to axios.
*/
this.http.defaults.proxy = false;
this.http.defaults.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/services/cli-http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('services', () => {
expect(response.body).to.equal('foo');
});

test.it('should add the correct http agent for proxy', async () => {
process.env.HTTP_PROXY = 'http://someproxy.com:8080';
const client = new CliRequestClient('blah', logger, { defaults: {} });
const httpAgent = client.http.defaults.httpsAgent;
expect(httpAgent.proxy.host).to.equal('someproxy.com');
expect(httpAgent.proxy.port).to.equal(8080);
});

test
.nock('https://foo.com', (api) => {
api.get('/bar').delay(100).reply(200);
Expand Down

0 comments on commit 6762db4

Please sign in to comment.