-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.test.js
166 lines (152 loc) · 4.97 KB
/
proxy.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var tap = require('tap');
var test = tap.test;
var url = require('url');
var http = require('http');
var nock = require('nock');
var request = require('../src/lib/request');
var proxyPort = 4242;
var httpRequestHost = 'http://localhost:8000';
var httpsRequestHost = 'https://snyk.io:443';
var requestPath = '/api/v1/verify/token';
/**
* Verify support for http(s) proxy from environments variables
* (http_proxy, https_proxy, no_proxy)
* see https://www.gnu.org/software/wget/manual/html_node/Proxies.html
*/
test('request respects proxy environment variables', function(t) {
t.plan(6);
t.test('direct http access', function(t) {
var nockClient = nock(httpRequestHost)
.post(requestPath)
.reply(200, {});
return request({ method: 'post', url: httpRequestHost + requestPath })
.then(function() {
t.ok(nockClient.isDone(), 'direct call without a proxy');
nock.cleanAll();
})
.catch((err) => t.fail(err.message));
});
t.test('direct https access', function(t) {
var nockClient = nock(httpsRequestHost)
.post(requestPath)
.reply(200, {});
return request({ method: 'post', url: httpsRequestHost + requestPath })
.then(function() {
t.ok(nockClient.isDone(), 'direct call without a proxy');
nock.cleanAll();
})
.catch((err) => t.fail(err.message));
});
t.test('http_proxy', function(t) {
// NO_PROXY is set in CircleCI and brakes test purpose
const tmpNoProxy = process.env.NO_PROXY;
delete process.env.NO_PROXY;
// Restore env variables
t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.http_proxy;
});
process.env.http_proxy = `http://localhost:${proxyPort}`;
var proxy = http.createServer(function(req, res) {
t.equal(req.url, httpRequestHost + requestPath, 'http_proxy url ok');
res.end();
});
proxy.listen(proxyPort);
// http is only supported for localhost
return request({ method: 'post', url: httpRequestHost + requestPath })
.catch((err) => t.fail(err.message))
.then(() => {
proxy.close();
});
});
t.test('HTTP_PROXY', function(t) {
// NO_PROXY is set in CircleCI and brakes test purpose
const tmpNoProxy = process.env.NO_PROXY;
delete process.env.NO_PROXY;
// Restore env variables
t.teardown(() => {
process.env.NO_PROXY = tmpNoProxy;
delete process.env.HTTP_PROXY;
});
process.env.HTTP_PROXY = `http://localhost:${proxyPort}`;
var proxy = http.createServer(function(req, res) {
t.equal(req.url, httpRequestHost + requestPath, 'HTTP_PROXY url ok');
res.end();
});
proxy.listen(proxyPort);
// http is only supported for localhost
return request({ method: 'post', url: httpRequestHost + requestPath })
.catch((err) => t.fail(err.message))
.then(() => {
proxy.close();
});
});
t.test('https_proxy', function(t) {
process.env.https_proxy = `http://localhost:${proxyPort}`;
var proxy = http.createServer();
proxy.setTimeout(1000);
proxy.on('connect', (req, cltSocket, head) => {
const proxiedUrl = url.parse(`https://${req.url}`);
t.equal(
proxiedUrl.hostname,
url.parse(httpsRequestHost).hostname,
'https_proxy url hostname ok',
);
t.equal(
proxiedUrl.port,
url.parse(httpsRequestHost).port,
'https_proxy url port ok',
);
cltSocket.write(
'HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'Connection: close\r\n' +
'\r\n',
function() {
cltSocket.end();
},
);
});
proxy.listen(proxyPort);
return request({ method: 'post', url: httpsRequestHost + requestPath })
.catch(() => {}) // client socket being closed generates an error here
.then(() => {
proxy.close();
delete process.env.https_proxy;
});
});
t.test('HTTPS_PROXY', function(t) {
process.env.HTTPS_PROXY = `http://localhost:${proxyPort}`;
var proxy = http.createServer();
proxy.setTimeout(1000);
proxy.on('connect', (req, cltSocket, head) => {
const proxiedUrl = url.parse(`https://${req.url}`);
t.equal(
proxiedUrl.hostname,
url.parse(httpsRequestHost).hostname,
'HTTPS_PROXY url hostname ok',
);
t.equal(
proxiedUrl.port,
url.parse(httpsRequestHost).port,
'HTTPS_PROXY url port ok',
);
cltSocket.write(
'HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'Connection: close\r\n' +
'\r\n',
function() {
cltSocket.end();
},
);
});
proxy.listen(proxyPort);
return request({ method: 'post', url: httpsRequestHost + requestPath })
.catch(() => {}) // client socket being closed generates an error here
.then(() => {
proxy.close();
delete process.env.HTTPS_PROXY;
});
});
});