◐ Shell
clean mode source ↗

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 = {}) {

1818

const 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+

}

2133

proxy.on('request', (req, res) => {

2234

logRequest(logs, req);

2335

const [hostname, port] = req.headers.host.split(':');

2436

const targetPort = port || 80;

253738+

const url = new URL(req.url);

2639

const options = {

2740

hostname: hostname,

2841

port: targetPort,

29-

path: req.url,

42+

path: url.pathname + url.search, // Convert back to relative URL.

3043

method: req.method,

3144

headers: req.headers,

3245

};

@@ -38,8 +51,16 @@ exports.createProxyServer = function() {

38513952

proxyReq.on('error', (err) => {

4053

logs.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

});

44654566

req.pipe(proxyReq, { end: true });

@@ -49,6 +70,11 @@ exports.createProxyServer = function() {

4970

logRequest(logs, req);

50715172

const [hostname, port] = req.url.split(':');

73+74+

res.on('error', (err) => {

75+

logs.push({ error: err, source: 'proxy response' });

76+

});

77+5278

const proxyReq = net.connect(port, hostname, () => {

5379

res.write(

5480

'HTTP/1.1 200 Connection Established\r\n' +

@@ -74,8 +100,46 @@ exports.createProxyServer = function() {

74100

return { 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) {

79143

const fixtures = require('./fixtures');

80144

const { code, signal, stdout, stderr } = await spawnPromisified(

81145

process.execPath,