Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test: update proxy tests for the new proxy global-agent
  • Loading branch information
JackuB committed Mar 30, 2021
1 parent 0d0c76a commit 8045ceb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
10 changes: 7 additions & 3 deletions test/jest/acceptance/proxy-behavior.spec.ts
Expand Up @@ -25,8 +25,9 @@ describe('Proxy configuration behavior', () => {
throw err;
}

// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
expect(stderr).toContain(
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
`Error: connect ECONNREFUSED 127.0.0.1:${
FAKE_HTTP_PROXY.split(':')[2]
}`,
);
Expand All @@ -53,6 +54,7 @@ describe('Proxy configuration behavior', () => {
throw err;
}

// It will *not attempt* to connect to a proxy and /analytics call won't fail
expect(stderr).not.toContain('ECONNREFUSED');
done();
},
Expand Down Expand Up @@ -80,8 +82,9 @@ describe('Proxy configuration behavior', () => {
throw err;
}

// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
expect(stderr).toContain(
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
`Error: connect ECONNREFUSED 127.0.0.1:${
FAKE_HTTP_PROXY.split(':')[2]
}`,
);
Expand All @@ -105,7 +108,8 @@ describe('Proxy configuration behavior', () => {
},
},
(err, stdout, stderr) => {
// TODO: incorrect behavior when Needle tries to upgrade connection after 301 http->https and the Agent option is set to a strict protocol
// TODO: incorrect behavior when Needle tries to upgrade connection after 301 http->https and the Agent option is set to a strict http/s protocol
// See lines with `keepAlive` in request.ts for more details
expect(stderr).toContain(
'TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"',
);
Expand Down
57 changes: 41 additions & 16 deletions test/proxy.test.js
@@ -1,14 +1,14 @@
var tap = require('tap');
var test = tap.test;
var url = require('url');
var http = require('http');
var nock = require('nock');
var request = require('../src/lib/request');

var proxyPort = 4242;
var httpRequestHost = 'http://localhost:8000';
var httpsRequestHost = 'https://snyk.io:443';
var requestPath = '/api/v1/verify/token';
const tap = require('tap');
const test = tap.test;
const url = require('url');
const http = require('http');
const nock = require('nock');
const request = require('../src/lib/request');

const proxyPort = 4242;
const httpRequestHost = 'http://localhost:8000';
const httpsRequestHost = 'https://snyk.io:443';
const requestPath = '/api/v1/verify/token';

/**
* Verify support for http(s) proxy from environments variables
Expand Down Expand Up @@ -51,10 +51,12 @@ test('request respects proxy environment variables', function(t) {
t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.http_proxy;
delete process.env.HTTP_PROXY;
delete global.GLOBAL_AGENT;
});

process.env.http_proxy = `http://localhost:${proxyPort}`;
var proxy = http.createServer(function(req, res) {
const proxy = http.createServer(function(req, res) {
t.equal(req.url, httpRequestHost + requestPath, 'http_proxy url ok');
res.end();
});
Expand All @@ -63,6 +65,7 @@ test('request respects proxy environment variables', function(t) {
return request({ method: 'post', url: httpRequestHost + requestPath })
.catch((err) => t.fail(err.message))
.then(() => {
t.equal(process.env.http_proxy, process.env.HTTP_PROXY);
proxy.close();
});
});
Expand All @@ -76,6 +79,7 @@ test('request respects proxy environment variables', function(t) {
t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.HTTP_PROXY;
delete global.GLOBAL_AGENT;
});

process.env.HTTP_PROXY = `http://localhost:${proxyPort}`;
Expand All @@ -93,8 +97,20 @@ test('request respects proxy environment variables', function(t) {
});

t.test('https_proxy', function(t) {
// NO_PROXY is set in CircleCI and brakes test purpose
const tmpNoProxy = process.env.NO_PROXY;
delete process.env.NO_PROXY;

t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.https_proxy;
delete process.env.HTTPS_PROXY;
delete global.GLOBAL_AGENT;
});

// eslint-disable-next-line @typescript-eslint/camelcase
process.env.https_proxy = `http://localhost:${proxyPort}`;
var proxy = http.createServer();
const proxy = http.createServer();
proxy.setTimeout(1000);
proxy.on('connect', (req, cltSocket, head) => {
const proxiedUrl = url.parse(`https://${req.url}`);
Expand Down Expand Up @@ -123,14 +139,24 @@ test('request respects proxy environment variables', function(t) {
return request({ method: 'post', url: httpsRequestHost + requestPath })
.catch(() => {}) // client socket being closed generates an error here
.then(() => {
t.equal(process.env.https_proxy, process.env.HTTPS_PROXY);
proxy.close();
delete process.env.https_proxy;
});
});

t.test('HTTPS_PROXY', function(t) {
// NO_PROXY is set in CircleCI and brakes test purpose
const tmpNoProxy = process.env.NO_PROXY;
delete process.env.NO_PROXY;

t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.HTTPS_PROXY;
delete global.GLOBAL_AGENT;
});

process.env.HTTPS_PROXY = `http://localhost:${proxyPort}`;
var proxy = http.createServer();
const proxy = http.createServer();
proxy.setTimeout(1000);
proxy.on('connect', (req, cltSocket, head) => {
const proxiedUrl = url.parse(`https://${req.url}`);
Expand Down Expand Up @@ -160,7 +186,6 @@ test('request respects proxy environment variables', function(t) {
.catch(() => {}) // client socket being closed generates an error here
.then(() => {
proxy.close();
delete process.env.HTTPS_PROXY;
});
});
});

0 comments on commit 8045ceb

Please sign in to comment.