Skip to content

Commit

Permalink
fix: also add HTTP_PROXY environment variable
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Schäfer <101886095+PeterSchafer@users.noreply.github.com>

chore: skip tests that cannot be satisfied with cliv2

Signed-off-by: Peter Schäfer <101886095+PeterSchafer@users.noreply.github.com>
Signed-off-by: Peter Schäfer <101886095+PeterSchafer@users.noreply.github.com>
Signed-off-by: Peter Schäfer <101886095+PeterSchafer@users.noreply.github.com>
  • Loading branch information
PeterSchafer committed Jun 15, 2022
1 parent e1386b7 commit b90e91e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func PrepareV1Command(cmd string, args []string, proxyPort int, caCertLocation s

snykCmd = exec.Command(cmd, args...)
snykCmd.Env = append(os.Environ(),
fmt.Sprintf("HTTP_PROXY=http://127.0.0.1:%d", proxyPort),
fmt.Sprintf("HTTPS_PROXY=http://127.0.0.1:%d", proxyPort),
fmt.Sprintf("NODE_EXTRA_CA_CERTS=%s", caCertLocation),
)
Expand Down
65 changes: 33 additions & 32 deletions test/jest/acceptance/proxy-behavior.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const SNYK_API_HTTP = 'http://snyk.io/api/v1';
const FAKE_HTTP_PROXY = `http://localhost:${fakeServerPort}`;

jest.setTimeout(1000 * 60 * 1);

describe('Proxy configuration behavior', () => {
describe('*_PROXY against HTTPS host', () => {
it('tries to connect to the HTTPS_PROXY when HTTPS_PROXY is set', async () => {
Expand Down Expand Up @@ -56,41 +55,43 @@ describe('Proxy configuration behavior', () => {
});

describe('*_PROXY against HTTP host', () => {
it('tries to connect to the HTTP_PROXY when HTTP_PROXY is set', async () => {
const { code, stderr } = await runSnykCLI(`woof -d`, {
env: {
...process.env,
HTTP_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
});
if (!isCLIV2()) {
it('tries to connect to the HTTP_PROXY when HTTP_PROXY is set', async () => {
const { code, stderr } = await runSnykCLI(`woof -d`, {
env: {
...process.env,
HTTP_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
});

expect(code).toBe(0);
expect(code).toBe(0);

// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
expect(stderr).toContain(
`Error: connect ECONNREFUSED 127.0.0.1:${fakeServerPort}`,
);
});

it('does not try to connect to the HTTPS_PROXY when it is set', async () => {
const { code, stderr } = await runSnykCLI(`woof -d`, {
env: {
...process.env,
HTTPS_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
expect(stderr).toContain(
`Error: connect ECONNREFUSED 127.0.0.1:${fakeServerPort}`,
);
});

expect(code).toBe(2);
it('does not try to connect to the HTTPS_PROXY when it is set', async () => {
const { code, stderr } = await runSnykCLI(`woof -d`, {
env: {
...process.env,
HTTPS_PROXY: FAKE_HTTP_PROXY,
SNYK_API: SNYK_API_HTTP,
SNYK_HTTP_PROTOCOL_UPGRADE: '0',
},
});

// 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:"',
);
});
expect(code).toBe(2);

// 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:"',
);
});
}
});
});

0 comments on commit b90e91e

Please sign in to comment.