test: refactor of test-tls-over-http-tunnel

PR-URL: https://github.com/nodejs/node/pull/18784
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
pull/18785/merge
juggernaut451 2018-02-14 22:22:12 +05:30 committed by Ruben Bridgewater
parent 862389b0aa
commit 2960096a91
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
1 changed files with 26 additions and 23 deletions

View File

@ -24,6 +24,9 @@ const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
// This test ensures that the data received through tls over http tunnel
// is same as what is sent.
const assert = require('assert');
const https = require('https');
const net = require('net');
@ -37,21 +40,21 @@ const cert = fixtures.readKey('agent1-cert.pem');
const options = { key, cert };
const server = https.createServer(options, function(req, res) {
const server = https.createServer(options, common.mustCall((req, res) => {
console.log('SERVER: got request');
res.writeHead(200, {
'content-type': 'text/plain'
});
console.log('SERVER: sending response');
res.end('hello world\n');
});
}));
const proxy = net.createServer(function(clientSocket) {
const proxy = net.createServer((clientSocket) => {
console.log('PROXY: got a client connection');
let serverSocket = null;
clientSocket.on('data', function(chunk) {
clientSocket.on('data', (chunk) => {
if (!serverSocket) {
// Verify the CONNECT request
assert.strictEqual(`CONNECT localhost:${server.address().port} ` +
@ -65,39 +68,39 @@ const proxy = net.createServer(function(clientSocket) {
console.log('PROXY: creating a tunnel');
// create the tunnel
serverSocket = net.connect(server.address().port, function() {
serverSocket = net.connect(server.address().port, common.mustCall(() => {
console.log('PROXY: replying to client CONNECT request');
// Send the response
clientSocket.write('HTTP/1.1 200 OK\r\nProxy-Connections: keep' +
'-alive\r\nConnections: keep-alive\r\nVia: ' +
`localhost:${proxy.address().port}\r\n\r\n`);
});
'-alive\r\nConnections: keep-alive\r\nVia: ' +
`localhost:${proxy.address().port}\r\n\r\n`);
}));
serverSocket.on('data', function(chunk) {
serverSocket.on('data', (chunk) => {
clientSocket.write(chunk);
});
serverSocket.on('end', function() {
serverSocket.on('end', common.mustCall(() => {
clientSocket.destroy();
});
}));
} else {
serverSocket.write(chunk);
}
});
clientSocket.on('end', function() {
clientSocket.on('end', () => {
serverSocket.destroy();
});
});
server.listen(0);
proxy.listen(0, function() {
proxy.listen(0, common.mustCall(() => {
console.log('CLIENT: Making CONNECT request');
const req = http.request({
port: this.address().port,
port: proxy.address().port,
method: 'CONNECT',
path: `localhost:${server.address().port}`,
headers: {
@ -117,7 +120,7 @@ proxy.listen(0, function() {
function onUpgrade(res, socket, head) {
// Hacky.
process.nextTick(function() {
process.nextTick(() => {
onConnect(res, socket, head);
});
}
@ -145,20 +148,20 @@ proxy.listen(0, function() {
socket: socket, // reuse the socket
agent: false,
rejectUnauthorized: false
}, function(res) {
}, (res) => {
assert.strictEqual(200, res.statusCode);
res.on('data', function(chunk) {
res.on('data', common.mustCall((chunk) => {
assert.strictEqual('hello world\n', chunk.toString());
console.log('CLIENT: got HTTPS response');
gotRequest = true;
});
}));
res.on('end', function() {
res.on('end', common.mustCall(() => {
proxy.close();
server.close();
});
}).on('error', function(er) {
}));
}).on('error', (er) => {
// We're ok with getting ECONNRESET in this test, but it's
// timing-dependent, and thus unreliable. Any other errors
// are just failures, though.
@ -166,8 +169,8 @@ proxy.listen(0, function() {
throw er;
}).end();
}
});
}));
process.on('exit', function() {
process.on('exit', () => {
assert.ok(gotRequest);
});