-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-use-env-proxy-precedence.mjs
More file actions
117 lines (99 loc) Β· 3.52 KB
/
test-use-env-proxy-precedence.mjs
File metadata and controls
117 lines (99 loc) Β· 3.52 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// This tests the precedence and interaction between --use-env-proxy CLI flag
// and NODE_USE_ENV_PROXY environment variable when both are set.
import * as common from '../common/index.mjs';
import assert from 'node:assert';
import http from 'node:http';
import { once } from 'events';
import { createProxyServer, runProxiedRequest } from '../common/proxy-server.js';
// Start a proxy server for testing
const { proxy, logs } = createProxyServer();
proxy.listen(0);
await once(proxy, 'listening');
// Start a HTTP server to process the final request
const server = http.createServer(common.mustCall((req, res) => {
res.end('Hello world');
}, 4));
server.on('error', common.mustNotCall((err) => { console.error('Server error', err); }));
server.listen(0);
await once(server, 'listening');
const serverHost = `localhost:${server.address().port}`;
const requestUrl = `http://${serverHost}/test`;
delete process.env.NODE_USE_ENV_PROXY; // Ensure the environment variable is not set.
// NODE_USE_ENV_PROXY=1 and --use-env-proxy can be used at the same time.
{
const { code, signal, stderr, stdout } = await runProxiedRequest({
NODE_USE_ENV_PROXY: '1',
REQUEST_URL: requestUrl,
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
}, ['--use-env-proxy']);
assert.strictEqual(stderr.trim(), '');
assert.match(stdout, /Hello world/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
// Should use the proxy
assert.strictEqual(logs.length, 1);
assert.deepStrictEqual(logs[0], {
method: 'GET',
url: requestUrl,
headers: {
'connection': 'keep-alive',
'proxy-connection': 'keep-alive',
'host': serverHost,
},
});
logs.splice(0, logs.length);
}
// NODE_USE_ENV_PROXY=0 and --no-use-env-proxy can be used at the same time.
{
const { code, signal, stderr, stdout } = await runProxiedRequest({
NODE_USE_ENV_PROXY: '0',
REQUEST_URL: requestUrl,
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
}, ['--no-use-env-proxy']);
assert.strictEqual(stderr.trim(), '');
assert.match(stdout, /Hello world/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
// Should NOT use the proxy
assert.strictEqual(logs.length, 0);
}
// --use-env-proxy CLI flag takes precedence over NODE_USE_ENV_PROXY=0.
{
const { code, signal, stderr, stdout } = await runProxiedRequest({
NODE_USE_ENV_PROXY: '0',
REQUEST_URL: requestUrl,
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
}, ['--use-env-proxy']);
assert.strictEqual(stderr.trim(), '');
assert.match(stdout, /Hello world/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
// Should use the proxy because CLI flag takes precedence
assert.strictEqual(logs.length, 1);
assert.deepStrictEqual(logs[0], {
method: 'GET',
url: requestUrl,
headers: {
'connection': 'keep-alive',
'proxy-connection': 'keep-alive',
'host': serverHost,
},
});
logs.splice(0, logs.length);
}
// --no-use-env-proxy CLI flag disables the proxy even if NODE_USE_ENV_PROXY=1.
{
const { code, signal, stderr, stdout } = await runProxiedRequest({
NODE_USE_ENV_PROXY: '1',
REQUEST_URL: requestUrl,
HTTP_PROXY: `http://localhost:${proxy.address().port}`,
}, ['--no-use-env-proxy']);
// Should NOT use the proxy because CLI flag takes precedence.
assert.strictEqual(stderr.trim(), '');
assert.match(stdout, /Hello world/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
assert.strictEqual(logs.length, 0);
}
proxy.close();
server.close();