|
| 1 | +import { exec } from 'child_process'; |
| 2 | +import { sep } from 'path'; |
| 3 | +const main = './dist/cli/index.js'.replace(/\//g, sep); |
| 4 | + |
| 5 | +const SNYK_API_HTTPS = 'https://snyk.io/api/v1'; |
| 6 | +const SNYK_API_HTTP = 'http://snyk.io/api/v1'; |
| 7 | +const FAKE_HTTP_PROXY = 'http://localhost:12345'; |
| 8 | +const testTimeout = 50000; |
| 9 | + |
| 10 | +describe('Proxy configuration behavior', () => { |
| 11 | + describe('*_PROXY against HTTPS host', () => { |
| 12 | + it( |
| 13 | + 'tries to connect to the HTTPS_PROXY when HTTPS_PROXY is set', |
| 14 | + (done) => { |
| 15 | + exec( |
| 16 | + `node ${main} woof -d`, |
| 17 | + { |
| 18 | + env: { |
| 19 | + HTTPS_PROXY: FAKE_HTTP_PROXY, |
| 20 | + SNYK_API: SNYK_API_HTTPS, |
| 21 | + }, |
| 22 | + }, |
| 23 | + (err, stdout, stderr) => { |
| 24 | + if (err) { |
| 25 | + throw err; |
| 26 | + } |
| 27 | + |
| 28 | + expect(stderr).toContain( |
| 29 | + `snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${ |
| 30 | + FAKE_HTTP_PROXY.split(':')[2] |
| 31 | + }`, |
| 32 | + ); |
| 33 | + done(); |
| 34 | + }, |
| 35 | + ); |
| 36 | + }, |
| 37 | + testTimeout, |
| 38 | + ); |
| 39 | + |
| 40 | + it( |
| 41 | + 'does not try to connect to the HTTP_PROXY when it is set', |
| 42 | + (done) => { |
| 43 | + exec( |
| 44 | + `node ${main} woof -d`, |
| 45 | + { |
| 46 | + env: { |
| 47 | + HTTP_PROXY: FAKE_HTTP_PROXY, |
| 48 | + SNYK_API: SNYK_API_HTTPS, |
| 49 | + }, |
| 50 | + }, |
| 51 | + (err, stdout, stderr) => { |
| 52 | + if (err) { |
| 53 | + throw err; |
| 54 | + } |
| 55 | + |
| 56 | + expect(stderr).not.toContain('ECONNREFUSED'); |
| 57 | + done(); |
| 58 | + }, |
| 59 | + ); |
| 60 | + }, |
| 61 | + testTimeout, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('*_PROXY against HTTP host', () => { |
| 66 | + it( |
| 67 | + 'tries to connect to the HTTP_PROXY when HTTP_PROXY is set', |
| 68 | + (done) => { |
| 69 | + exec( |
| 70 | + `node ${main} woof -d`, |
| 71 | + { |
| 72 | + env: { |
| 73 | + HTTP_PROXY: FAKE_HTTP_PROXY, |
| 74 | + SNYK_API: SNYK_API_HTTP, |
| 75 | + SNYK_HTTP_PROTOCOL_UPGRADE: '0', |
| 76 | + }, |
| 77 | + }, |
| 78 | + (err, stdout, stderr) => { |
| 79 | + if (err) { |
| 80 | + throw err; |
| 81 | + } |
| 82 | + |
| 83 | + expect(stderr).toContain( |
| 84 | + `snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${ |
| 85 | + FAKE_HTTP_PROXY.split(':')[2] |
| 86 | + }`, |
| 87 | + ); |
| 88 | + done(); |
| 89 | + }, |
| 90 | + ); |
| 91 | + }, |
| 92 | + testTimeout, |
| 93 | + ); |
| 94 | + |
| 95 | + it( |
| 96 | + 'does not try to connect to the HTTPS_PROXY when it is set', |
| 97 | + (done) => { |
| 98 | + exec( |
| 99 | + `node ${main} woof -d`, |
| 100 | + { |
| 101 | + env: { |
| 102 | + HTTPS_PROXY: FAKE_HTTP_PROXY, |
| 103 | + SNYK_API: SNYK_API_HTTP, |
| 104 | + SNYK_HTTP_PROTOCOL_UPGRADE: '0', |
| 105 | + }, |
| 106 | + }, |
| 107 | + (err, stdout, stderr) => { |
| 108 | + // TODO: incorrect behavior when Needle tries to upgrade connection after 301 http->https and the Agent option is set to a strict protocol |
| 109 | + expect(stderr).toContain( |
| 110 | + 'TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"', |
| 111 | + ); |
| 112 | + done(); |
| 113 | + }, |
| 114 | + ); |
| 115 | + }, |
| 116 | + testTimeout, |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
0 commit comments