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

fix: if scheme is http:// use an HTTP agent #75

Merged
merged 6 commits into from
Aug 13, 2019
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"homepage": "https:/googleapis/teeny-request#readme",
"dependencies": {
"http-proxy-agent": "^2.1.0",
"https-proxy-agent": "^2.2.1",
"node-fetch": "^2.2.0",
"stream-events": "^1.0.5",
Expand Down
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

import {Agent as HTTPAgent} from 'http';
import {Agent} from 'https';

import fetch, * as f from 'node-fetch';
import {PassThrough, Readable} from 'stream';
import * as uuid from 'uuid';
Expand Down Expand Up @@ -80,9 +82,6 @@ export interface RequestCallback<T = any> {
(err: Error | null, response: Response, body?: T): void;
}

// tslint:disable-next-line variable-name
const HttpsProxyAgent = require('https-proxy-agent');

export class RequestError extends Error {
code?: number;
}
Expand Down Expand Up @@ -130,15 +129,27 @@ function requestToFetchOptions(reqOpts: Options) {
uri = uri + '?' + params;
}

const isHttp = uri.startsWith('http://');
const proxy =
reqOpts.proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy;
if (reqOpts.proxy || proxy) {
options.agent = new HttpsProxyAgent(proxy);
if (proxy) {
if (isHttp) {
// tslint:disable-next-line variable-name
const HttpProxyAgent = require('http-proxy-agent');
bcoe marked this conversation as resolved.
Show resolved Hide resolved
options.agent = new HttpProxyAgent(proxy);
} else {
// tslint:disable-next-line variable-name
const HttpsProxyAgent = require('https-proxy-agent');
options.agent = new HttpsProxyAgent(proxy);
}
} else if (reqOpts.forever) {
options.agent = new Agent({keepAlive: true});
options.agent = isHttp
? new HTTPAgent({keepAlive: true})
: new Agent({keepAlive: true});
}

return {uri, options};
Expand Down
44 changes: 38 additions & 6 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import {teenyRequest} from '../src';

import {PassThrough} from 'stream';

// tslint:disable-next-line variable-name
const HttpProxyAgent = require('http-proxy-agent');
// tslint:disable-next-line variable-name
const HttpsProxyAgent = require('https-proxy-agent');

nock.disableNetConnect();
const uri = 'http://example.com';
const uri = 'https://example.com';

function mockJson() {
return nock(uri)
Expand Down Expand Up @@ -142,21 +144,51 @@ describe('teeny', () => {
});
});

it('should respect environment variables for proxy config', () => {
const envVars = ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY'];
for (const v of envVars) {
const envVars = ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY'];
for (const v of envVars) {
it(`should respect ${v} environment variable for proxy config`, done => {
sandbox.stub(process, 'env').value({[v]: 'https://fake.proxy'});
const expectedBody = {hello: '🌎'};
const scope = nock(uri)
.get('/')
.reply(200, expectedBody);
teenyRequest({uri}, (err, res, body) => {
assert.ifError(err);
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpsProxyAgent);
bcoe marked this conversation as resolved.
Show resolved Hide resolved
return done();
});
}
});
}

it('should create http proxy if upstream scheme is http', done => {
sandbox.stub(process, 'env').value({http_proxy: 'https://fake.proxy'});
const expectedBody = {hello: '🌎'};
const scope = nock('http://example.com')
.get('/')
.reply(200, expectedBody);
teenyRequest({uri: 'http://example.com'}, (err, res, body) => {
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpProxyAgent);
return done();
});
});

it('should use proxy if set in request options', done => {
const expectedBody = {hello: '🌎'};
const scope = nock(uri)
.get('/')
.reply(200, expectedBody);
teenyRequest({uri, proxy: 'https://fake.proxy'}, (err, res, body) => {
scope.done();
assert.ifError(err);
assert.deepStrictEqual(expectedBody, body);
assert.ok(res.request.agent instanceof HttpsProxyAgent);
return done();
});
});

// see: https:/googleapis/nodejs-storage/issues/798
Expand Down