/
test.ts
403 lines (343 loc) · 10.6 KB
/
test.ts
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import * as fs from 'fs';
import * as net from 'net';
import * as http from 'http';
import * as https from 'https';
import assert from 'assert';
import { once } from 'events';
import { listen } from 'async-listen';
import { json, req } from 'agent-base';
import { createProxy, ProxyServer } from 'proxy';
import { HttpsProxyAgent } from '../src';
const sslOptions = {
key: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.key`),
cert: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.pem`),
};
const testIf = (condition: boolean, ...args: Parameters<typeof test>) =>
condition ? test(...args) : test.skip(...args);
describe('HttpsProxyAgent', () => {
let server: http.Server;
let serverUrl: URL;
let sslServer: https.Server;
let sslServerUrl: URL;
let proxy: ProxyServer;
let proxyUrl: URL;
let sslProxy: ProxyServer;
let sslProxyUrl: URL;
beforeAll(async () => {
// setup target HTTP server
server = http.createServer();
serverUrl = await listen(server);
});
beforeAll(async () => {
// setup HTTP proxy server
proxy = createProxy();
proxyUrl = await listen(proxy);
});
beforeAll(async () => {
// setup target HTTPS server
sslServer = https.createServer(sslOptions);
sslServerUrl = await listen(sslServer);
});
beforeAll(async () => {
// setup SSL HTTP proxy server
sslProxy = createProxy(https.createServer(sslOptions));
sslProxyUrl = await listen(sslProxy);
});
beforeEach(() => {
server.removeAllListeners('request');
sslServer.removeAllListeners('request');
});
// shut down the test HTTP servers
afterAll(() => {
server.close();
proxy.close();
sslServer.close();
sslProxy.close();
});
describe('constructor', () => {
it('should throw an Error if no "proxy" argument is given', () => {
assert.throws(() => {
new HttpsProxyAgent('');
});
});
it('should accept a "string" proxy argument', () => {
const agent = new HttpsProxyAgent(proxyUrl.href);
assert.equal(proxyUrl.hostname, agent.proxy.hostname);
assert.equal(proxyUrl.port, agent.proxy.port);
});
it('should accept a `URL` instance proxy argument', () => {
const agent = new HttpsProxyAgent(proxyUrl);
assert.equal(proxyUrl.hostname, agent.proxy.hostname);
assert.equal(proxyUrl.port, agent.proxy.port);
});
});
describe('"http" module', () => {
beforeEach(() => {
delete proxy.authenticate;
});
it('should work over an HTTP proxy', async () => {
server.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(proxyUrl);
const r = req(serverUrl, { agent });
const [connect] = await once(r, 'proxyConnect');
expect(connect.statusCode).toEqual(200);
expect(connect.statusText).toEqual('Connection established');
expect('date' in connect.headers).toBe(true);
const res = await r;
const body = await json(res);
assert.equal(serverUrl.host, body.host);
});
it('should work over an HTTPS proxy', async () => {
server.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(sslProxyUrl, {
rejectUnauthorized: false,
});
const r = req(serverUrl, { agent });
const [connect] = await once(agent, 'proxyConnect');
expect(connect.statusCode).toEqual(200);
expect(connect.statusText).toEqual('Connection established');
const res = await r;
const body = await json(res);
assert.equal(serverUrl.host, body.host);
});
it('should receive the 407 authorization code on the `http.ClientResponse`', async () => {
// reject all auth requests
proxy.authenticate = () => false;
const agent = new HttpsProxyAgent(proxyUrl);
const res = await req('http://example.com', { agent });
assert.equal(407, res.statusCode);
assert('proxy-authenticate' in res.headers);
});
it('should not error if the proxy responds with 407 and the request is aborted', async () => {
// reject all auth requests
proxy.authenticate = () => false;
const req = http.get(
{
agent: new HttpsProxyAgent(proxyUrl),
},
(res) => {
assert.equal(407, res.statusCode);
req.abort();
}
);
await once(req, 'abort');
});
it('should emit an "end" event on the `http.IncomingMessage` if the proxy responds with non-200 status code', async () => {
proxy.authenticate = () => false;
const agent = new HttpsProxyAgent(proxyUrl);
const res = await req('http://example.com', {
agent,
});
assert.equal(407, res.statusCode);
res.resume();
await once(res, 'end');
});
it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', async () => {
// port 4 is a reserved, but "unassigned" port
const agent = new HttpsProxyAgent('http://localhost:4');
let err: NodeJS.ErrnoException | undefined;
try {
await req('http://nodejs.org', { agent });
} catch (_err) {
err = _err as NodeJS.ErrnoException;
}
assert(err);
assert.equal('ECONNREFUSED', err.code);
});
it('should allow custom proxy "headers" object', async () => {
const agent = new HttpsProxyAgent(serverUrl, {
headers: {
Foo: 'bar',
},
});
const connectPromise = once(server, 'connect');
http.get({ agent }).on('error', () => {
// "error" happens because agent didn't receive proper
// CONNECT response before the socket was closed.
// We can safely ignore that.
});
const [req, socket] = await connectPromise;
assert.equal('CONNECT', req.method);
assert.equal('bar', req.headers.foo);
socket.destroy();
});
it('should allow custom proxy "headers" function', async () => {
let count = 1;
const agent = new HttpsProxyAgent(serverUrl, {
headers: () => ({ number: count++ }),
});
const connectPromise = once(server, 'connect');
http.get({ agent }).on('error', () => {
// "error" happens because agent didn't receive proper
// CONNECT response before the socket was closed.
// We can safely ignore that.
});
const [req, socket] = await connectPromise;
assert.equal('CONNECT', req.method);
assert.equal('1', req.headers.number);
socket.destroy();
const connectPromise2 = once(server, 'connect');
http.get({ agent }).on('error', () => {
// "error" happens because agent didn't receive proper
// CONNECT response before the socket was closed.
// We can safely ignore that.
});
const [req2, socket2] = await connectPromise2;
assert.equal('CONNECT', req2.method);
assert.equal('2', req2.headers.number);
socket2.destroy();
});
it('should work with `keepAlive: true`', async () => {
server.on('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(proxyUrl, { keepAlive: true });
try {
const res = await req(serverUrl, { agent });
expect(res.headers.connection).toEqual('keep-alive');
res.resume();
const s1 = res.socket;
await once(s1, 'free');
const res2 = await req(serverUrl, { agent });
expect(res2.headers.connection).toEqual('keep-alive');
res2.resume();
const s2 = res2.socket;
assert(s1 === s2);
await once(s2, 'free');
} finally {
agent.destroy();
}
});
it('should emit "error" on request if proxy has invalid header', async () => {
const badProxy = net.createServer((socket) => {
socket.write(
'HTTP/1.1 200 Connection established\r\nbadheader\r\n\r\n'
);
});
const addr = await listen(badProxy);
let err: Error | undefined;
try {
const agent = new HttpsProxyAgent(
addr.href.replace('tcp', 'http')
);
await req('http://example.com', { agent });
} catch (_err) {
err = _err as Error;
} finally {
badProxy.close();
}
assert(err);
expect(err.message).toEqual(
'Invalid header from proxy CONNECT response: "badheader"'
);
});
});
describe('"https" module', () => {
// Skipping these tests on older Node, since it fails with a strange error.
// Possibly related to the `proxy` testing module.
// The module does seem to work fine on an actual proxy though.
const nodeVersion = parseFloat(process.versions.node);
it('should throw an "error" when `rejectUnauthorized: false` is missing', async () => {
const agent = new HttpsProxyAgent(sslProxyUrl, {
rejectUnauthorized: false,
});
let err: Error | undefined;
try {
await req(sslServerUrl, {
agent,
});
} catch (_err) {
err = _err as Error;
}
assert(err);
expect(err.message).toMatch(/self( |-)signed certificate/);
});
testIf(
nodeVersion >= 18,
'should work over an HTTP proxy',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(proxyUrl);
const res = await req(sslServerUrl, {
rejectUnauthorized: false,
agent,
});
const body = await json(res);
assert.equal(sslServerUrl.host, body.host);
}
);
testIf(
nodeVersion >= 18,
'should work over an HTTPS proxy',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(sslProxyUrl, {
rejectUnauthorized: false,
});
const res = await req(sslServerUrl, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal(sslServerUrl.host, body.host);
}
);
testIf(
nodeVersion >= 18,
'should not send a port number for the default port',
async () => {
sslServer.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(sslProxyUrl, {
rejectUnauthorized: false,
});
agent.defaultPort = parseInt(sslServerUrl.port, 10);
const res = await req(sslServerUrl, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert.equal(sslServerUrl.hostname, body.host);
}
);
it('should work with `keepAlive: true`', async () => {
sslServer.on('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
const agent = new HttpsProxyAgent(sslProxyUrl, {
keepAlive: true,
rejectUnauthorized: false,
});
try {
const res = await req(sslServerUrl, {
agent,
rejectUnauthorized: false,
});
expect(res.headers.connection).toEqual('keep-alive');
res.resume();
const s1 = res.socket;
await once(s1, 'free');
const res2 = await req(sslServerUrl, {
agent,
rejectUnauthorized: false,
});
expect(res2.headers.connection).toEqual('keep-alive');
res2.resume();
const s2 = res2.socket;
assert(s1 === s2);
await once(s2, 'free');
} finally {
agent.destroy();
}
});
});
});