From 8045cebb56119626c8f7d5a04f6fd85025496560 Mon Sep 17 00:00:00 2001 From: Jakub Mikulas Date: Tue, 30 Mar 2021 22:52:09 +0200 Subject: [PATCH] test: update proxy tests for the new proxy global-agent --- test/jest/acceptance/proxy-behavior.spec.ts | 10 ++-- test/proxy.test.js | 57 +++++++++++++++------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/test/jest/acceptance/proxy-behavior.spec.ts b/test/jest/acceptance/proxy-behavior.spec.ts index 972c8a6dc8..0a9f587b6d 100644 --- a/test/jest/acceptance/proxy-behavior.spec.ts +++ b/test/jest/acceptance/proxy-behavior.spec.ts @@ -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] }`, ); @@ -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(); }, @@ -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] }`, ); @@ -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:"', ); diff --git a/test/proxy.test.js b/test/proxy.test.js index 7f744a525a..659f15e679 100644 --- a/test/proxy.test.js +++ b/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 @@ -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(); }); @@ -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(); }); }); @@ -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}`; @@ -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}`); @@ -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}`); @@ -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; }); }); });