Skip to content

Commit

Permalink
feat: add support for no_proxy environment variable
Browse files Browse the repository at this point in the history
Refs #69
  • Loading branch information
bcoe committed Nov 5, 2020
1 parent 9048be4 commit 9b60efd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ let HttpsProxyAgent: any;

// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function loadProxy() {
function loadProxy(url: string) {
const noProxy = process.env.no_proxy ?? process.env.NO_PROXY;
if (noProxy && url === noProxy) {
return;
}
const proxy =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
Expand All @@ -55,7 +59,6 @@ function loadProxy() {
}
return proxy;
}
loadProxy();

export class Gaxios {
private agentCache = new Map<
Expand Down Expand Up @@ -219,7 +222,7 @@ export class Gaxios {
}
opts.method = opts.method || 'GET';

const proxy = loadProxy();
const proxy = loadProxy(opts.url);
if (proxy) {
if (this.agentCache.has(proxy)) {
opts.agent = this.agentCache.get(proxy);
Expand Down
30 changes: 30 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,36 @@ describe('🥁 configuration options', () => {
assert.ok(res.config.agent instanceof HttpsProxyAgent);
});

describe('no_proxy', () => {
it('should not proxy when url matches no_proxy', async () => {
const url = 'https://example.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'https://example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});

it('should still proxy if url does not match no_proxy', async () => {
const url = 'https://example2.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'https://example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
});
});

it('should load the proxy from the cache', async () => {
sandbox.stub(process, 'env').value({HTTPS_PROXY: 'https://fake.proxy'});
const body = {hello: '🌎'};
Expand Down

0 comments on commit 9b60efd

Please sign in to comment.