forked from felicienfrancois/node-electron-proxy-agent
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutils.ts
More file actions
94 lines (85 loc) · 2.96 KB
/
utils.ts
File metadata and controls
94 lines (85 loc) · 2.96 KB
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
import * as http from 'http';
import * as https from 'https';
import * as fs from 'fs';
import * as path from 'path';
import * as assert from 'assert';
import * as vpa from '../../..';
import { loadSystemCertificates } from '../../../src';
export const log = { ...console, debug: () => {}, trace: () => {} };
export const ca = [
fs.readFileSync(path.join(__dirname, '../../test-https-server/ssl_cert.pem')).toString(),
fs.readFileSync(path.join(__dirname, '../../test-https-server/ssl_teapot_cert.pem')).toString(),
];
export const unusedCa = fs.readFileSync(path.join(__dirname, '../../test-https-server/ssl_unused_cert.pem')).toString();
export const directProxyAgentParams: vpa.ProxyAgentParams = {
resolveProxy: async () => 'DIRECT',
getProxyURL: () => undefined,
getProxySupport: () => 'override',
isAdditionalFetchSupportEnabled: () => true,
addCertificatesV1: () => false,
addCertificatesV2: () => true,
log,
getLogLevel: () => vpa.LogLevel.Trace,
proxyResolveTelemetry: () => undefined,
useHostProxy: true,
loadAdditionalCertificates: async () => [
...await loadSystemCertificates({ log }),
...ca,
],
env: {},
};
export const directProxyAgentParamsV1: vpa.ProxyAgentParams = {
...directProxyAgentParams,
addCertificatesV1: () => true,
addCertificatesV2: () => false,
};
export const proxiedProxyAgentParamsV1: vpa.ProxyAgentParams = {
...directProxyAgentParamsV1,
resolveProxy: async () => 'PROXY test-http-proxy:3128',
};
export const tlsProxiedProxyAgentParamsV1: vpa.ProxyAgentParams = {
...directProxyAgentParamsV1,
resolveProxy: async () => 'HTTPS test-https-proxy:8080',
};
export async function testRequest<C extends typeof https | typeof http>(client: C, options: C extends typeof https ? (https.RequestOptions & vpa.SecureContextOptionsPatch) : http.RequestOptions, testOptions: { assertResult?: (result: any, req: http.ClientRequest, res: http.IncomingMessage) => void; } = {}) {
return new Promise<void>((resolve, reject) => {
const req = client.request(options, res => {
if (!res.statusCode || res.statusCode < 200 || res.statusCode > 299) {
const chunks: Buffer[] = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => {
const err = new Error(`Error status: ${res.statusCode} ${res.statusMessage} \n${Buffer.concat(chunks).toString()}`);
(err as any).statusCode = res.statusCode;
(err as any).statusMessage = res.statusMessage;
reject(err);
});
return;
}
let data = '';
res.setEncoding('utf8');
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(data);
assert.equal(result.status, 'OK!');
if (testOptions.assertResult) {
testOptions.assertResult(result, req, res);
}
resolve();
} catch (err: any) {
err.message = `${err.message}: ${data}`;
reject(err);
}
});
res.on('error', err => {
reject(err);
});
});
req.on('error', err => {
reject(err);
});
req.end();
});
}