test: move http proxy tests to test/client-proxy · nodejs/node@16dc53c
@@ -14,19 +14,32 @@ function logRequest(logs, req) {
14141515// This creates a minimal proxy server that logs the requests it gets
1616// to an array before performing proxying.
17-exports.createProxyServer = function() {
17+exports.createProxyServer = function(options = {}) {
1818const logs = [];
191920-const proxy = http.createServer();
20+let proxy;
21+if (options.https) {
22+const common = require('../common');
23+if (!common.hasCrypto) {
24+common.skip('missing crypto');
25+}
26+proxy = require('https').createServer({
27+cert: require('./fixtures').readKey('agent9-cert.pem'),
28+key: require('./fixtures').readKey('agent9-key.pem'),
29+});
30+} else {
31+proxy = http.createServer();
32+}
2133proxy.on('request', (req, res) => {
2234logRequest(logs, req);
2335const [hostname, port] = req.headers.host.split(':');
2436const targetPort = port || 80;
253738+const url = new URL(req.url);
2639const options = {
2740hostname: hostname,
2841port: targetPort,
29-path: req.url,
42+path: url.pathname + url.search, // Convert back to relative URL.
3043method: req.method,
3144headers: req.headers,
3245};
@@ -38,8 +51,16 @@ exports.createProxyServer = function() {
38513952proxyReq.on('error', (err) => {
4053logs.push({ error: err, source: 'proxy request' });
41-res.writeHead(500);
42-res.end('Proxy error: ' + err.message);
54+if (!res.headersSent) {
55+res.writeHead(500);
56+}
57+if (!res.writableEnded) {
58+res.end(`Proxy error ${err.code}: ${err.message}`);
59+}
60+});
61+62+res.on('error', (err) => {
63+logs.push({ error: err, source: 'proxy response' });
4364});
44654566req.pipe(proxyReq, { end: true });
@@ -49,6 +70,11 @@ exports.createProxyServer = function() {
4970logRequest(logs, req);
50715172const [hostname, port] = req.url.split(':');
73+74+res.on('error', (err) => {
75+logs.push({ error: err, source: 'proxy response' });
76+});
77+5278const proxyReq = net.connect(port, hostname, () => {
5379res.write(
5480'HTTP/1.1 200 Connection Established\r\n' +
@@ -74,8 +100,46 @@ exports.createProxyServer = function() {
74100return { proxy, logs };
75101};
7610277-exports.checkProxiedRequest = async function(envExtension, expectation) {
78-const { spawnPromisified } = require('./');
103+function spawnPromisified(...args) {
104+const { spawn } = require('child_process');
105+let stderr = '';
106+let stdout = '';
107+108+const child = spawn(...args);
109+child.stderr.setEncoding('utf8');
110+child.stderr.on('data', (data) => {
111+console.error('[STDERR]', data);
112+stderr += data;
113+});
114+child.stdout.setEncoding('utf8');
115+child.stdout.on('data', (data) => {
116+console.log('[STDOUT]', data);
117+stdout += data;
118+});
119+120+return new Promise((resolve, reject) => {
121+child.on('close', (code, signal) => {
122+console.log('[CLOSE]', code, signal);
123+resolve({
124+ code,
125+ signal,
126+ stderr,
127+ stdout,
128+});
129+});
130+child.on('error', (code, signal) => {
131+console.log('[ERROR]', code, signal);
132+reject({
133+ code,
134+ signal,
135+ stderr,
136+ stdout,
137+});
138+});
139+});
140+}
141+142+exports.checkProxiedFetch = async function(envExtension, expectation) {
79143const fixtures = require('./fixtures');
80144const { code, signal, stdout, stderr } = await spawnPromisified(
81145process.execPath,