Skip to content

Commit

Permalink
#156 add basic tests for the node http requester
Browse files Browse the repository at this point in the history
  • Loading branch information
ffendt committed May 26, 2021
1 parent 4041960 commit 75b61ab
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 20 deletions.
104 changes: 84 additions & 20 deletions javascript/lib/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javascript/lib/node/package.json
Expand Up @@ -26,6 +26,7 @@
"@types/ws": "^6.0.1",
"barrelsby": "^2.1.1",
"jest": "^25.4.0",
"nock": "^13.0.11",
"ts-jest": "^25.5.1",
"tslint": "^5.18.x",
"tslint-config-airbnb": "^5.11.1",
Expand Down
2 changes: 2 additions & 0 deletions javascript/lib/node/src/proxy-settings.ts
Expand Up @@ -38,7 +38,9 @@ class ProxyAgentOptionsBuilder {
}

parseUrlFromEnvironment(environmentProxy: string | undefined, options: ProxyOptions | undefined): ProxyAgentOptionsBuilder {
// tslint:disable-next-line:strict-boolean-expressions
const shouldIgnoreProxyFromEnv = options !== undefined && options.ignoreProxyFromEnv;
// tslint:disable-next-line:strict-boolean-expressions
if (environmentProxy !== undefined && !shouldIgnoreProxyFromEnv) {
this.proxyUrl = parse(environmentProxy);
}
Expand Down
110 changes: 110 additions & 0 deletions javascript/lib/node/tests/node-http.spec.ts
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

import { ProxyAgent } from '../src/proxy-settings';
import { NodeRequester } from '../src/node-http';
import nock = require('nock');

describe('NodeHttp', () => {

afterAll(nock.restore);

afterEach(nock.cleanAll);

beforeEach(() => {
delete process.env.HTTPS_PROXY;
delete process.env.https_proxy;
delete process.env.HTTP_PROXY;
delete process.env.http_proxy;
});

it('sends requests', () => {
const payload = 'hello';
const expectedResponsePayload = { foo: 'bar' };
const proxyAgent = new ProxyAgent();

nock('http://localhost:8080')
.get('/get', payload)
.reply(200, function () {
if ((this.req as any).options.agent) {
return '"Request was using a proxy agent where it shouldn\'t"';
}
return expectedResponsePayload;
});

const underTest = new NodeRequester(proxyAgent);

const request = underTest.doRequest('GET', 'http://localhost:8080/get', new Map(), payload);
return request
.then(response => {
expect(response.body).toEqual(expectedResponsePayload);
}, rejected => {
fail(rejected);
});
});

it('uses the http proxy agent for http requests', () => {
const payload = 'hello';
const expectedResponsePayload = { foo: 'bar' };

process.env.HTTP_PROXY = 'http://http-proxy';
const proxyAgent = new ProxyAgent();

nock('http://localhost:8080')
.get('/get', payload)
.reply(200, function () {
if ((this.req as any).options.agent === proxyAgent.httpProxyAgent) {
return expectedResponsePayload;
}
return '"Request wasn\'t using the expected http proxy agent"';
});

const underTest = new NodeRequester(proxyAgent);

const request = underTest.doRequest('GET', 'http://localhost:8080/get', new Map(), payload);
return request
.then(response => {
expect(response.body).toEqual(expectedResponsePayload);
}, rejected => {
fail(rejected);
});
});

it('uses the https proxy agent for https requests', () => {
const payload = 'hello';
const expectedResponsePayload = { foo: 'bar' };

process.env.HTTPS_PROXY = 'http://https-proxy';
const proxyAgent = new ProxyAgent();

nock('https://localhost:8080')
.get('/get', payload)
.reply(200, function () {
if ((this.req as any).options.agent === proxyAgent.proxyAgent) {
return expectedResponsePayload;
}
return '"Request wasn\'t using the expected https proxy agent"';
});

const underTest = new NodeRequester(proxyAgent);

const request = underTest.doRequest('GET', 'https://localhost:8080/get', new Map(), payload);
return request
.then(response => {
expect(response.body).toEqual(expectedResponsePayload);
}, rejected => {
fail(rejected);
});
});

});

0 comments on commit 75b61ab

Please sign in to comment.