global-agent 4.1.2 → 4.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/Logger.d.ts +11 -0
  2. package/dist/Logger.js +56 -0
  3. package/dist/classes/Agent.d.ts +46 -0
  4. package/dist/classes/Agent.js +216 -0
  5. package/dist/classes/HttpProxyAgent.d.ts +7 -0
  6. package/dist/classes/HttpProxyAgent.js +20 -0
  7. package/dist/classes/HttpsProxyAgent.d.ts +7 -0
  8. package/dist/classes/HttpsProxyAgent.js +48 -0
  9. package/dist/classes/index.d.ts +3 -0
  10. package/dist/classes/index.js +12 -0
  11. package/dist/errors.d.ts +4 -0
  12. package/dist/errors.js +10 -0
  13. package/dist/factories/createGlobalProxyAgent.d.ts +7 -0
  14. package/dist/factories/createGlobalProxyAgent.js +131 -0
  15. package/dist/factories/createGlobalProxyAgent.test.d.ts +1 -0
  16. package/dist/factories/createGlobalProxyAgent.test.js +539 -0
  17. package/dist/factories/createProxyController.d.ts +7 -0
  18. package/dist/factories/createProxyController.js +38 -0
  19. package/dist/factories/createProxyController.test.d.ts +1 -0
  20. package/dist/factories/createProxyController.test.js +29 -0
  21. package/dist/factories/index.d.ts +2 -0
  22. package/dist/factories/index.js +10 -0
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.js +7 -0
  25. package/dist/routines/bootstrap.d.ts +3 -0
  26. package/dist/routines/bootstrap.js +20 -0
  27. package/dist/routines/index.d.ts +1 -0
  28. package/dist/routines/index.js +8 -0
  29. package/dist/types.d.ts +57 -0
  30. package/dist/types.js +2 -0
  31. package/dist/utilities/bindHttpMethod.d.ts +7 -0
  32. package/dist/utilities/bindHttpMethod.js +50 -0
  33. package/dist/utilities/index.d.ts +3 -0
  34. package/dist/utilities/index.js +12 -0
  35. package/dist/utilities/isUrlMatchingNoProxy.d.ts +2 -0
  36. package/dist/utilities/isUrlMatchingNoProxy.js +27 -0
  37. package/dist/utilities/isUrlMatchingNoProxy.test.d.ts +1 -0
  38. package/dist/utilities/isUrlMatchingNoProxy.test.js +61 -0
  39. package/dist/utilities/parseBoolean.d.ts +1 -0
  40. package/dist/utilities/parseBoolean.js +16 -0
  41. package/dist/utilities/parseProxyUrl.d.ts +6 -0
  42. package/dist/utilities/parseProxyUrl.js +31 -0
  43. package/dist/utilities/parseProxyUrl.test.d.ts +1 -0
  44. package/dist/utilities/parseProxyUrl.test.js +31 -0
  45. package/package.json +5 -2
  46. package/.babelrc +0 -23
  47. package/.editorconfig +0 -9
  48. package/.github/FUNDING.yml +0 -2
  49. package/.github/workflows/feature.yaml +0 -35
  50. package/.github/workflows/main.yaml +0 -50
  51. package/.gitignore +0 -12
  52. package/bootstrap.js +0 -1
  53. package/src/Logger.ts +0 -70
  54. package/src/classes/Agent.ts +0 -296
  55. package/src/classes/HttpProxyAgent.ts +0 -45
  56. package/src/classes/HttpsProxyAgent.ts +0 -83
  57. package/src/classes/index.ts +0 -9
  58. package/src/errors.ts +0 -9
  59. package/src/factories/createGlobalProxyAgent.test.ts +0 -761
  60. package/src/factories/createGlobalProxyAgent.ts +0 -187
  61. package/src/factories/createProxyController.test.ts +0 -38
  62. package/src/factories/createProxyController.ts +0 -51
  63. package/src/factories/index.ts +0 -6
  64. package/src/index.ts +0 -9
  65. package/src/routines/bootstrap.ts +0 -28
  66. package/src/routines/index.ts +0 -3
  67. package/src/types.ts +0 -70
  68. package/src/utilities/bindHttpMethod.ts +0 -53
  69. package/src/utilities/index.ts +0 -9
  70. package/src/utilities/isUrlMatchingNoProxy.test.ts +0 -77
  71. package/src/utilities/isUrlMatchingNoProxy.ts +0 -32
  72. package/src/utilities/parseBoolean.ts +0 -17
  73. package/src/utilities/parseProxyUrl.test.ts +0 -35
  74. package/src/utilities/parseProxyUrl.ts +0 -39
  75. package/tsconfig.json +0 -25
  76. package/vitest.config.ts +0 -11
@@ -1,761 +0,0 @@
1
- import type {
2
- IncomingMessage,
3
- IncomingHttpHeaders,
4
- } from 'http';
5
- import http from 'http';
6
- import https from 'https';
7
- import net from 'net';
8
- import {
9
- afterEach,
10
- beforeAll,
11
- beforeEach,
12
- expect,
13
- test,
14
- } from 'vitest';
15
- import axios from 'axios';
16
- import getPort from 'get-port';
17
- import got from 'got';
18
- import pem from 'pem';
19
- import makeRequest from 'request';
20
- import {
21
- stub,
22
- } from 'sinon';
23
- import createGlobalProxyAgent from './createGlobalProxyAgent';
24
-
25
- type ProxyServerType = {
26
- port: number,
27
- stop: () => void,
28
- url: string,
29
- };
30
-
31
- type HttpServerType = {
32
- stop: () => void,
33
- url: string,
34
- };
35
-
36
- type HttpsServerType = {
37
- port: number,
38
- stop: () => void,
39
- url: string,
40
- };
41
-
42
- type ProxyRules = {
43
- beforeSendRequest?: (requestDetail: {
44
- requestOptions: {
45
- headers: Record<string, string>,
46
- },
47
- }) => {
48
- response: {
49
- body: string,
50
- header: Record<string, string>,
51
- statusCode: number,
52
- },
53
- },
54
- onConnect?: (request: http.IncomingMessage) => void,
55
- };
56
-
57
- const defaultHttpAgent = http.globalAgent;
58
- const defaultHttpsAgent = https.globalAgent;
59
-
60
- // Backup original value of NODE_TLS_REJECT_UNAUTHORIZED
61
- // oxlint-disable-next-line node/no-process-env
62
- const defaultNodeTlsRejectUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
63
-
64
- let lastPort = 3_000;
65
-
66
- let localProxyServers: ProxyServerType[] = [];
67
- let localHttpServers: HttpServerType[] = [];
68
- let localHttpsServers: HttpsServerType[] = [];
69
- let generatedCerts: {
70
- cert: string,
71
- key: string,
72
- } | null = null;
73
-
74
- const getNextPort = (): Promise<number> => {
75
- return getPort({
76
- port: getPort.makeRange(lastPort++, 3_500),
77
- });
78
- };
79
-
80
- // Generate self-signed certificates for HTTPS testing
81
- const generateCertificates = (): Promise<{
82
- cert: string,
83
- key: string,
84
- }> => {
85
- return new Promise((resolve, reject) => {
86
- if (generatedCerts) {
87
- resolve(generatedCerts);
88
-
89
- return;
90
- }
91
-
92
- pem.createCertificate({days: 1, selfSigned: true}, (error, keys) => {
93
- if (error) {
94
- reject(error);
95
-
96
- return;
97
- }
98
-
99
- generatedCerts = {cert: keys.certificate, key: keys.serviceKey};
100
- resolve(generatedCerts);
101
- });
102
- });
103
- };
104
-
105
- beforeAll(async () => {
106
- // Pre-generate certificates
107
- await generateCertificates();
108
- });
109
-
110
- beforeEach(() => {
111
- http.globalAgent = defaultHttpAgent;
112
- https.globalAgent = defaultHttpsAgent;
113
- });
114
-
115
- afterEach(() => {
116
- for (const localProxyServer of localProxyServers) {
117
- localProxyServer.stop();
118
- }
119
-
120
- localProxyServers = [];
121
-
122
- for (const localHttpServer of localHttpServers) {
123
- localHttpServer.stop();
124
- }
125
-
126
- localHttpServers = [];
127
-
128
- for (const localHttpsServer of localHttpsServers) {
129
- localHttpsServer.stop();
130
- }
131
-
132
- localHttpsServers = [];
133
-
134
- // Reset NODE_TLS_REJECT_UNAUTHORIZED to original value
135
- // oxlint-disable-next-line node/no-process-env
136
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = defaultNodeTlsRejectUnauthorized;
137
- });
138
-
139
- type HttpResponseType = {
140
- body: string,
141
- headers: IncomingHttpHeaders,
142
- statusCode: number,
143
- };
144
-
145
- const createHttpResponseResolver = (resolve: (response: HttpResponseType) => void) => {
146
- return (response: IncomingMessage) => {
147
- let body = '';
148
-
149
- response.on('data', (data) => {
150
- body += data;
151
- });
152
-
153
- response.on('end', () => {
154
- if (!response.headers) {
155
- throw new Error('response.headers is not defined');
156
- }
157
-
158
- if (!response.statusCode) {
159
- throw new Error('response.statusCode is not defined');
160
- }
161
-
162
- resolve({
163
- body,
164
- headers: response.headers,
165
- statusCode: response.statusCode,
166
- });
167
- });
168
- };
169
- };
170
-
171
- // Create a local HTTPS server for CONNECT tunnel targets
172
- const createHttpsServer = async (): Promise<HttpsServerType> => {
173
- const port = await getNextPort();
174
- const certs = await generateCertificates();
175
-
176
- const localHttpsServer: HttpsServerType = await new Promise((resolve) => {
177
- const httpsServer = https.createServer({
178
- cert: certs.cert,
179
- key: certs.key,
180
- }, (request, response) => {
181
- response.writeHead(200, {'content-type': 'text/plain'});
182
- response.end('OK');
183
- });
184
-
185
- httpsServer.listen(port, '127.0.0.1', () => {
186
- resolve({
187
- port,
188
- stop: () => {
189
- httpsServer.close();
190
- },
191
- url: 'https://127.0.0.1:' + port,
192
- });
193
- });
194
- });
195
-
196
- localHttpsServers.push(localHttpsServer);
197
-
198
- return localHttpsServer;
199
- };
200
-
201
- // Create a simple HTTP proxy server that can handle both HTTP requests and HTTPS CONNECT tunneling
202
- const createProxyServer = async (rules?: ProxyRules): Promise<ProxyServerType & {
203
- httpsServer?: HttpsServerType,
204
- }> => {
205
- const port = await getNextPort();
206
-
207
- // Create an HTTPS server that the proxy will tunnel to for CONNECT requests
208
- const httpsServer = await createHttpsServer();
209
-
210
- const localProxyServer: ProxyServerType & {
211
- httpsServer?: HttpsServerType,
212
- } = await new Promise((resolve) => {
213
- const proxyServer = http.createServer((request, response) => {
214
- // Handle regular HTTP proxy requests
215
- if (rules?.beforeSendRequest) {
216
- const result = rules.beforeSendRequest({
217
- requestOptions: {
218
- headers: request.headers as Record<string, string>,
219
- },
220
- });
221
-
222
- response.writeHead(result.response.statusCode, result.response.header);
223
- response.end(result.response.body);
224
- } else {
225
- // Default response
226
- response.writeHead(200, {'content-type': 'text/plain'});
227
- response.end('OK');
228
- }
229
- });
230
-
231
- // Handle CONNECT requests for HTTPS tunneling
232
- proxyServer.on('connect', (request, clientSocket, head) => {
233
- // Call onConnect hook if provided
234
- if (rules?.onConnect) {
235
- rules.onConnect(request);
236
- }
237
-
238
- // Connect to the local HTTPS server instead of the requested host
239
- const serverSocket = net.connect(httpsServer.port, '127.0.0.1', () => {
240
- clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
241
- serverSocket.write(head);
242
- serverSocket.pipe(clientSocket);
243
- clientSocket.pipe(serverSocket);
244
- });
245
-
246
- serverSocket.on('error', () => {
247
- clientSocket.end('HTTP/1.1 500 Internal Server Error\r\n\r\n');
248
- });
249
-
250
- clientSocket.on('error', () => {
251
- serverSocket.end();
252
- });
253
- });
254
-
255
- proxyServer.listen(port, () => {
256
- resolve({
257
- httpsServer,
258
- port,
259
- stop: () => {
260
- proxyServer.close();
261
- },
262
- url: 'http://127.0.0.1:' + port,
263
- });
264
- });
265
- });
266
-
267
- localProxyServers.push(localProxyServer);
268
-
269
- return localProxyServer;
270
- };
271
-
272
- const createHttpServer = async (): Promise<HttpServerType> => {
273
- const port = await getNextPort();
274
-
275
- const localHttpServer: HttpServerType = await new Promise((resolve) => {
276
- const httpServer = http.createServer((request, response) => {
277
- response.end('DIRECT');
278
- });
279
-
280
- httpServer.listen(port, () => {
281
- resolve({
282
- stop: () => {
283
- httpServer.close();
284
- },
285
- url: 'http://127.0.0.1:' + port,
286
- });
287
- });
288
- });
289
-
290
- localHttpServers.push(localHttpServer);
291
-
292
- return localHttpServer;
293
- };
294
-
295
- test('proxies HTTP request', async () => {
296
- const globalProxyAgent = createGlobalProxyAgent();
297
-
298
- const proxyServer = await createProxyServer();
299
-
300
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
301
-
302
- const response: HttpResponseType = await new Promise((resolve) => {
303
- http.get('http://127.0.0.1', createHttpResponseResolver(resolve));
304
- });
305
-
306
- expect(response.body).toBe('OK');
307
- });
308
-
309
- test('proxies HTTP request with proxy-authorization header', async () => {
310
- const globalProxyAgent = createGlobalProxyAgent();
311
-
312
- const beforeSendRequest = stub().callsFake(() => {
313
- return {
314
- response: {
315
- body: 'OK',
316
- header: {'content-type': 'text/plain'},
317
- statusCode: 200,
318
- },
319
- };
320
- });
321
-
322
- const proxyServer = await createProxyServer({
323
- beforeSendRequest,
324
- });
325
-
326
- globalProxyAgent.HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer.port;
327
-
328
- const response: HttpResponseType = await new Promise((resolve) => {
329
- http.get('http://127.0.0.1', createHttpResponseResolver(resolve));
330
- });
331
-
332
- expect(response.body).toBe('OK');
333
-
334
- expect(beforeSendRequest.firstCall.args[0].requestOptions.headers['proxy-authorization']).toBe('Basic Zm9v');
335
- });
336
-
337
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = undefined', async () => {
338
- // oxlint-disable-next-line node/no-process-env
339
- const {NODE_TLS_REJECT_UNAUTHORIZED, ...restEnvironments} = process.env; // oxlint-disable-line @typescript-eslint/no-unused-vars
340
- // oxlint-disable-next-line node/no-process-env
341
- process.env = restEnvironments;
342
- // oxlint-disable-next-line node/no-process-env
343
- process.env.GLOBAL_AGENT_FORCE_GLOBAL_AGENT = 'true';
344
- const globalProxyAgent = createGlobalProxyAgent();
345
- const proxyServer = await createProxyServer();
346
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
347
- const globalAgent: any = https.globalAgent;
348
- expect(globalAgent.getRejectUnauthorized()).toBe(true);
349
-
350
- const response: HttpResponseType = await new Promise((resolve) => {
351
- http.get('http://127.0.0.1', createHttpResponseResolver(resolve));
352
- });
353
-
354
- expect(response.body).toBe('OK');
355
- });
356
-
357
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = null', async () => {
358
- // oxlint-disable-next-line node/no-process-env
359
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 'null';
360
- const globalProxyAgent = createGlobalProxyAgent();
361
- const proxyServer = await createProxyServer();
362
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
363
-
364
- const globalAgent: any = https.globalAgent;
365
- expect(globalAgent.getRejectUnauthorized()).toBe(false);
366
- });
367
-
368
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = 1', async () => {
369
- // @ts-expect-error it is expected as we wanted to set process variable with int
370
- // oxlint-disable-next-line node/no-process-env
371
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;
372
- const globalProxyAgent = createGlobalProxyAgent();
373
-
374
- const proxyServer = await createProxyServer();
375
-
376
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
377
-
378
- const globalAgent: any = https.globalAgent;
379
- expect(globalAgent.getRejectUnauthorized()).toBe(true);
380
- });
381
-
382
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = 0', async () => {
383
- // @ts-expect-error it is expected as we wanted to set process variable with int
384
- // oxlint-disable-next-line node/no-process-env
385
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
386
- const globalProxyAgent = createGlobalProxyAgent();
387
-
388
- const proxyServer = await createProxyServer();
389
-
390
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
391
-
392
- const globalAgent: any = https.globalAgent;
393
- expect(globalAgent.getRejectUnauthorized()).toBe(false);
394
- });
395
-
396
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = true', async () => {
397
- // @ts-expect-error it is expected as we wanted to set process variable with boolean
398
- // oxlint-disable-next-line node/no-process-env
399
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = true;
400
- const globalProxyAgent = createGlobalProxyAgent();
401
- const proxyServer = await createProxyServer();
402
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
403
-
404
- const globalAgent: any = https.globalAgent;
405
- expect(globalAgent.getRejectUnauthorized()).toBe(true);
406
- });
407
-
408
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = false', async () => {
409
- // @ts-expect-error it is expected as we wanted to set process variable with boolean
410
- // oxlint-disable-next-line node/no-process-env
411
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = false;
412
- const globalProxyAgent = createGlobalProxyAgent();
413
- const proxyServer = await createProxyServer();
414
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
415
-
416
- const globalAgent: any = https.globalAgent;
417
- expect(globalAgent.getRejectUnauthorized()).toBe(false);
418
- });
419
-
420
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = yes', async () => {
421
- // oxlint-disable-next-line node/no-process-env
422
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 'yes';
423
- const globalProxyAgent = createGlobalProxyAgent();
424
- const proxyServer = await createProxyServer();
425
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
426
-
427
- const globalAgent: any = https.globalAgent;
428
- expect(globalAgent.getRejectUnauthorized()).toBe(true);
429
- });
430
-
431
- test('Test reject unauthorized variable when NODE_TLS_REJECT_UNAUTHORIZED = no', async () => {
432
- // oxlint-disable-next-line node/no-process-env
433
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 'no';
434
- const globalProxyAgent = createGlobalProxyAgent();
435
- const proxyServer = await createProxyServer();
436
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
437
-
438
- const globalAgent: any = https.globalAgent;
439
- expect(globalAgent.getRejectUnauthorized()).toBe(false);
440
- });
441
-
442
- test('Test addCACertificates and clearCACertificates methods', async () => {
443
- const globalProxyAgent = createGlobalProxyAgent();
444
-
445
- const proxyServer = await createProxyServer();
446
-
447
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
448
- const globalAgent: any = https.globalAgent;
449
- expect(globalAgent.ca).toBe(undefined);
450
- globalAgent.addCACertificates(['test-ca-certficate1', 'test-ca-certficate2']);
451
- globalAgent.addCACertificates(['test-ca-certficate3']);
452
- const result = ['test-ca-certficate1', 'test-ca-certficate2', 'test-ca-certficate3'];
453
- expect(globalAgent.ca.length).toBe(result.length);
454
- expect(JSON.stringify(globalAgent.ca)).toBe(JSON.stringify(result));
455
- globalAgent.clearCACertificates();
456
- expect(globalAgent.ca).toBe(undefined);
457
- });
458
-
459
- test('Test addCACertificates when passed ca is a string', async () => {
460
- // oxlint-disable-next-line node/no-process-env
461
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
462
- const globalProxyAgent = createGlobalProxyAgent();
463
-
464
- const proxyServer = await createProxyServer();
465
-
466
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
467
- const globalAgent: any = https.globalAgent;
468
- expect(globalAgent.ca).toBe(undefined);
469
- globalAgent.addCACertificates('test-ca-certficate1');
470
- globalAgent.addCACertificates('test-ca-certficate2');
471
- expect(globalAgent.ca).toBe('test-ca-certficate1test-ca-certficate2');
472
- const response: HttpResponseType = await new Promise((resolve) => {
473
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
474
- });
475
- expect(response.body).toBe('OK');
476
- });
477
-
478
- test('Test addCACertificates when input ca is a string and existing ca is array', async () => {
479
- // oxlint-disable-next-line node/no-process-env
480
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
481
- const globalProxyAgent = createGlobalProxyAgent({ca: ['test-ca']});
482
-
483
- const proxyServer = await createProxyServer();
484
-
485
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
486
- const globalAgent: any = https.globalAgent;
487
- expect(globalAgent.ca.length).toBe(1);
488
- globalAgent.addCACertificates('test-ca-certficate1');
489
- expect(globalAgent.ca.length).toBe(1);
490
- expect(JSON.stringify(globalAgent.ca)).toBe(JSON.stringify(['test-ca']));
491
- const response: HttpResponseType = await new Promise((resolve) => {
492
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
493
- });
494
- expect(response.body).toBe('OK');
495
- });
496
-
497
- test('Test addCACertificates when input ca array is null or undefined', async () => {
498
- // oxlint-disable-next-line node/no-process-env
499
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
500
- const globalProxyAgent = createGlobalProxyAgent();
501
-
502
- const proxyServer = await createProxyServer();
503
-
504
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
505
- const globalAgent: any = https.globalAgent;
506
- expect(globalAgent.ca).toBe(undefined);
507
- globalAgent.addCACertificates(undefined);
508
- globalAgent.addCACertificates(null);
509
- expect(globalAgent.ca).toBe(undefined);
510
- const response: HttpResponseType = await new Promise((resolve) => {
511
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
512
- });
513
- expect(response.body).toBe('OK');
514
- });
515
-
516
- test('Test initializing ca certificate property while creating global proxy agent', async () => {
517
- // oxlint-disable-next-line node/no-process-env
518
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
519
- const globalProxyAgent = createGlobalProxyAgent({ca: ['test-ca']});
520
-
521
- const proxyServer = await createProxyServer();
522
-
523
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
524
- const globalAgent: any = https.globalAgent;
525
- expect(globalAgent.ca.length).toBe(1);
526
- globalAgent.addCACertificates(['test-ca1']);
527
- expect(globalAgent.ca.length).toBe(2);
528
- expect(globalAgent.ca[0]).toBe('test-ca');
529
- expect(globalAgent.ca[1]).toBe('test-ca1');
530
- const response: HttpResponseType = await new Promise((resolve) => {
531
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
532
- });
533
- expect(response.body).toBe('OK');
534
- });
535
-
536
- test('proxies HTTPS request', async () => {
537
- // oxlint-disable-next-line node/no-process-env
538
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
539
- const globalProxyAgent = createGlobalProxyAgent();
540
-
541
- const proxyServer = await createProxyServer();
542
-
543
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
544
-
545
- const response: HttpResponseType = await new Promise((resolve) => {
546
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
547
- });
548
-
549
- expect(response.body).toBe('OK');
550
- });
551
-
552
- test('proxies HTTPS request with proxy-authorization header', async () => {
553
- // oxlint-disable-next-line node/no-process-env
554
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
555
- const globalProxyAgent = createGlobalProxyAgent();
556
-
557
- const onConnect = stub();
558
-
559
- const proxyServer = await createProxyServer({
560
- onConnect,
561
- });
562
-
563
- globalProxyAgent.HTTP_PROXY = 'http://foo@127.0.0.1:' + proxyServer.port;
564
-
565
- const response: HttpResponseType = await new Promise((resolve) => {
566
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
567
- });
568
-
569
- expect(response.body).toBe('OK');
570
-
571
- expect(onConnect.firstCall.args[0].headers['proxy-authorization']).toBe('Basic Zm9v');
572
- });
573
-
574
- test('does not produce unhandled rejection when cannot connect to proxy', async () => {
575
- const globalProxyAgent = createGlobalProxyAgent();
576
-
577
- const port = await getNextPort();
578
-
579
- globalProxyAgent.HTTP_PROXY = 'http://127.0.0.1:' + port;
580
-
581
- await expect(got('http://127.0.0.1')).rejects.toThrow();
582
- });
583
-
584
- test('proxies HTTPS request with dedicated proxy', async () => {
585
- // oxlint-disable-next-line node/no-process-env
586
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
587
- const globalProxyAgent = createGlobalProxyAgent();
588
-
589
- const proxyServer = await createProxyServer();
590
-
591
- globalProxyAgent.HTTPS_PROXY = proxyServer.url;
592
-
593
- const response: HttpResponseType = await new Promise((resolve) => {
594
- https.get('https://127.0.0.1', createHttpResponseResolver(resolve));
595
- });
596
-
597
- expect(response.body).toBe('OK');
598
- });
599
-
600
- test('ignores dedicated HTTPS proxy for HTTP urls', async () => {
601
- const globalProxyAgent = createGlobalProxyAgent();
602
-
603
- const proxyServer = await createProxyServer();
604
-
605
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
606
- globalProxyAgent.HTTPS_PROXY = 'http://example.org';
607
-
608
- const response: HttpResponseType = await new Promise((resolve) => {
609
- http.get('http://127.0.0.1', {}, createHttpResponseResolver(resolve));
610
- });
611
-
612
- expect(response.body).toBe('OK');
613
- });
614
-
615
- test('forwards requests matching NO_PROXY', async () => {
616
- const globalProxyAgent = createGlobalProxyAgent();
617
-
618
- const proxyServer = await createProxyServer();
619
- const httpServer = await createHttpServer();
620
-
621
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
622
- globalProxyAgent.NO_PROXY = '127.0.0.1';
623
-
624
- const response: HttpResponseType = await new Promise((resolve) => {
625
- http.get(httpServer.url, createHttpResponseResolver(resolve));
626
- });
627
-
628
- expect(response.body).toBe('DIRECT');
629
- });
630
-
631
- test('forwards requests that go to a socket', async () => {
632
- const globalProxyAgent = createGlobalProxyAgent();
633
-
634
- // not relevant as traffic shouldn't go through proxy
635
- globalProxyAgent.HTTP_PROXY = 'localhost:10324';
636
-
637
- const server = http.createServer((request, serverResponse) => {
638
- serverResponse.writeHead(200);
639
- serverResponse.write('OK');
640
- serverResponse.end();
641
- });
642
-
643
- server.listen('/tmp/test.sock');
644
-
645
- const response: HttpResponseType = await new Promise((resolve) => {
646
- http.get({
647
- path: '/endpoint',
648
- socketPath: '/tmp/test.sock',
649
- }, createHttpResponseResolver(resolve));
650
- });
651
-
652
- server.close();
653
-
654
- expect(response.body).toBe('OK');
655
- });
656
-
657
- test('proxies HTTP request (using http.get(host))', async () => {
658
- const globalProxyAgent = createGlobalProxyAgent();
659
-
660
- const proxyServer = await createProxyServer();
661
-
662
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
663
-
664
- const response: HttpResponseType = await new Promise((resolve) => {
665
- http.get({
666
- host: '127.0.0.1',
667
- }, createHttpResponseResolver(resolve));
668
- });
669
-
670
- expect(response.body).toBe('OK');
671
- });
672
-
673
- test('proxies HTTP request (using got)', async () => {
674
- const globalProxyAgent = createGlobalProxyAgent();
675
-
676
- const proxyServer = await createProxyServer();
677
-
678
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
679
-
680
- const response = await got('http://127.0.0.1');
681
-
682
- expect(response.body).toBe('OK');
683
- });
684
-
685
- test('proxies HTTPS request (using got)', async () => {
686
- // oxlint-disable-next-line node/no-process-env
687
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
688
- const globalProxyAgent = createGlobalProxyAgent();
689
-
690
- const proxyServer = await createProxyServer();
691
-
692
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
693
-
694
- const response = await got('https://127.0.0.1');
695
-
696
- expect(response.body).toBe('OK');
697
- });
698
-
699
- test('proxies HTTP request (using axios)', async () => {
700
- const globalProxyAgent = createGlobalProxyAgent();
701
-
702
- const proxyServer = await createProxyServer();
703
-
704
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
705
-
706
- const response = await axios.get('http://127.0.0.1');
707
-
708
- expect(response.data).toBe('OK');
709
- });
710
-
711
- test('proxies HTTPS request (using axios)', async () => {
712
- // oxlint-disable-next-line node/no-process-env
713
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
714
- const globalProxyAgent = createGlobalProxyAgent();
715
-
716
- const proxyServer = await createProxyServer();
717
-
718
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
719
-
720
- const response = await axios.get('https://127.0.0.1');
721
-
722
- expect(response.data).toBe('OK');
723
- });
724
-
725
- test('proxies HTTP request (using request)', async () => {
726
- const globalProxyAgent = createGlobalProxyAgent();
727
-
728
- const proxyServer = await createProxyServer();
729
-
730
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
731
-
732
- const response = await new Promise((resolve) => {
733
- makeRequest('http://127.0.0.1', (error, requestResponse, body) => {
734
- expect(error).toBe(null);
735
-
736
- resolve(body);
737
- });
738
- });
739
-
740
- expect(response).toBe('OK');
741
- });
742
-
743
- test('proxies HTTPS request (using request)', async () => {
744
- // oxlint-disable-next-line node/no-process-env
745
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
746
- const globalProxyAgent = createGlobalProxyAgent();
747
-
748
- const proxyServer = await createProxyServer();
749
-
750
- globalProxyAgent.HTTP_PROXY = proxyServer.url;
751
-
752
- const response = await new Promise((resolve) => {
753
- makeRequest('https://127.0.0.1', (error, requestResponse, body) => {
754
- expect(error).toBe(null);
755
-
756
- resolve(body);
757
- });
758
- });
759
-
760
- expect(response).toBe('OK');
761
- });