Skip to content

Commit 8045ceb

Browse files
committedMar 30, 2021
test: update proxy tests for the new proxy global-agent
1 parent 0d0c76a commit 8045ceb

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed
 

‎test/jest/acceptance/proxy-behavior.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ describe('Proxy configuration behavior', () => {
2525
throw err;
2626
}
2727

28+
// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
2829
expect(stderr).toContain(
29-
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
30+
`Error: connect ECONNREFUSED 127.0.0.1:${
3031
FAKE_HTTP_PROXY.split(':')[2]
3132
}`,
3233
);
@@ -53,6 +54,7 @@ describe('Proxy configuration behavior', () => {
5354
throw err;
5455
}
5556

57+
// It will *not attempt* to connect to a proxy and /analytics call won't fail
5658
expect(stderr).not.toContain('ECONNREFUSED');
5759
done();
5860
},
@@ -80,8 +82,9 @@ describe('Proxy configuration behavior', () => {
8082
throw err;
8183
}
8284

85+
// It will *attempt* to connect to a FAKE_HTTP_PROXY (and fails, because it's not a real proxy server)
8386
expect(stderr).toContain(
84-
`snyk analytics Error: connect ECONNREFUSED 127.0.0.1:${
87+
`Error: connect ECONNREFUSED 127.0.0.1:${
8588
FAKE_HTTP_PROXY.split(':')[2]
8689
}`,
8790
);
@@ -105,7 +108,8 @@ describe('Proxy configuration behavior', () => {
105108
},
106109
},
107110
(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
111+
// 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
112+
// See lines with `keepAlive` in request.ts for more details
109113
expect(stderr).toContain(
110114
'TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"',
111115
);

‎test/proxy.test.js

+41-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
var tap = require('tap');
2-
var test = tap.test;
3-
var url = require('url');
4-
var http = require('http');
5-
var nock = require('nock');
6-
var request = require('../src/lib/request');
7-
8-
var proxyPort = 4242;
9-
var httpRequestHost = 'http://localhost:8000';
10-
var httpsRequestHost = 'https://snyk.io:443';
11-
var requestPath = '/api/v1/verify/token';
1+
const tap = require('tap');
2+
const test = tap.test;
3+
const url = require('url');
4+
const http = require('http');
5+
const nock = require('nock');
6+
const request = require('../src/lib/request');
7+
8+
const proxyPort = 4242;
9+
const httpRequestHost = 'http://localhost:8000';
10+
const httpsRequestHost = 'https://snyk.io:443';
11+
const requestPath = '/api/v1/verify/token';
1212

1313
/**
1414
* Verify support for http(s) proxy from environments variables
@@ -51,10 +51,12 @@ test('request respects proxy environment variables', function(t) {
5151
t.teardown(() => {
5252
process.env.NO_PROXY = tmpNoProxy;
5353
delete process.env.http_proxy;
54+
delete process.env.HTTP_PROXY;
55+
delete global.GLOBAL_AGENT;
5456
});
5557

5658
process.env.http_proxy = `http://localhost:${proxyPort}`;
57-
var proxy = http.createServer(function(req, res) {
59+
const proxy = http.createServer(function(req, res) {
5860
t.equal(req.url, httpRequestHost + requestPath, 'http_proxy url ok');
5961
res.end();
6062
});
@@ -63,6 +65,7 @@ test('request respects proxy environment variables', function(t) {
6365
return request({ method: 'post', url: httpRequestHost + requestPath })
6466
.catch((err) => t.fail(err.message))
6567
.then(() => {
68+
t.equal(process.env.http_proxy, process.env.HTTP_PROXY);
6669
proxy.close();
6770
});
6871
});
@@ -76,6 +79,7 @@ test('request respects proxy environment variables', function(t) {
7679
t.teardown(() => {
7780
process.env.NO_PROXY = tmpNoProxy;
7881
delete process.env.HTTP_PROXY;
82+
delete global.GLOBAL_AGENT;
7983
});
8084

8185
process.env.HTTP_PROXY = `http://localhost:${proxyPort}`;
@@ -93,8 +97,20 @@ test('request respects proxy environment variables', function(t) {
9397
});
9498

9599
t.test('https_proxy', function(t) {
100+
// NO_PROXY is set in CircleCI and brakes test purpose
101+
const tmpNoProxy = process.env.NO_PROXY;
102+
delete process.env.NO_PROXY;
103+
104+
t.teardown(() => {
105+
process.env.NO_PROXY = tmpNoProxy;
106+
delete process.env.https_proxy;
107+
delete process.env.HTTPS_PROXY;
108+
delete global.GLOBAL_AGENT;
109+
});
110+
111+
// eslint-disable-next-line @typescript-eslint/camelcase
96112
process.env.https_proxy = `http://localhost:${proxyPort}`;
97-
var proxy = http.createServer();
113+
const proxy = http.createServer();
98114
proxy.setTimeout(1000);
99115
proxy.on('connect', (req, cltSocket, head) => {
100116
const proxiedUrl = url.parse(`https://${req.url}`);
@@ -123,14 +139,24 @@ test('request respects proxy environment variables', function(t) {
123139
return request({ method: 'post', url: httpsRequestHost + requestPath })
124140
.catch(() => {}) // client socket being closed generates an error here
125141
.then(() => {
142+
t.equal(process.env.https_proxy, process.env.HTTPS_PROXY);
126143
proxy.close();
127-
delete process.env.https_proxy;
128144
});
129145
});
130146

131147
t.test('HTTPS_PROXY', function(t) {
148+
// NO_PROXY is set in CircleCI and brakes test purpose
149+
const tmpNoProxy = process.env.NO_PROXY;
150+
delete process.env.NO_PROXY;
151+
152+
t.teardown(() => {
153+
process.env.NO_PROXY = tmpNoProxy;
154+
delete process.env.HTTPS_PROXY;
155+
delete global.GLOBAL_AGENT;
156+
});
157+
132158
process.env.HTTPS_PROXY = `http://localhost:${proxyPort}`;
133-
var proxy = http.createServer();
159+
const proxy = http.createServer();
134160
proxy.setTimeout(1000);
135161
proxy.on('connect', (req, cltSocket, head) => {
136162
const proxiedUrl = url.parse(`https://${req.url}`);
@@ -160,7 +186,6 @@ test('request respects proxy environment variables', function(t) {
160186
.catch(() => {}) // client socket being closed generates an error here
161187
.then(() => {
162188
proxy.close();
163-
delete process.env.HTTPS_PROXY;
164189
});
165190
});
166191
});

0 commit comments

Comments
 (0)
Please sign in to comment.