Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test(proxy): acceptance test for Proxy envvar settings
  • Loading branch information
JackuB committed Mar 30, 2021
1 parent 6d67579 commit e597846
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions test/jest/acceptance/proxy-behavior.spec.ts
@@ -0,0 +1,119 @@
import { exec } from 'child_process';
import { sep } from 'path';
const main = './dist/cli/index.js'.replace(/\//g, sep);

const SNYK_API_HTTPS = 'https://snyk.io/api/v1';
const SNYK_API_HTTP = 'http://snyk.io/api/v1';
const FAKE_HTTP_PROXY = 'http://localhost:12345';
const testTimeout = 50000;

describe('Proxy configuration behavior', () => {
describe('*_PROXY against HTTPS host', () => {
it(
'tries to connect to the HTTPS_PROXY when HTTPS_PROXY is set',
(done) => {
exec(
`node ${main} woof -d`,
{
env: {
HTTPS_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTPS,
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}

expect(stderr).toContain(
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
FAKE_HTTP_PROXY.split(':')[2]
}`,
);
done();
},
);
},
testTimeout,
);

it(
'does not try to connect to the HTTP_PROXY when it is set',
(done) => {
exec(
`node ${main} woof -d`,
{
env: {
HTTP_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTPS,
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}

expect(stderr).not.toContain('ECONNREFUSED');
done();
},
);
},
testTimeout,
);
});

describe('*_PROXY against HTTP host', () => {
it(
'tries to connect to the HTTP_PROXY when HTTP_PROXY is set',
(done) => {
exec(
`node ${main} woof -d`,
{
env: {
HTTP_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
},
(err, stdout, stderr) => {
if (err) {
throw err;
}

expect(stderr).toContain(
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
FAKE_HTTP_PROXY.split(':')[2]
}`,
);
done();
},
);
},
testTimeout,
);

it(
'does not try to connect to the HTTPS_PROXY when it is set',
(done) => {
exec(
`node ${main} woof -d`,
{
env: {
HTTPS_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
},
(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
expect(stderr).toContain(
'TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"',
);
done();
},
);
},
testTimeout,
);
});
});

0 comments on commit e597846

Please sign in to comment.