-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(proxy): acceptance test for Proxy envvar settings
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); | ||
}); | ||
}); |