Skip to content

Commit

Permalink
test: refactor of test-tls-over-http-tunnel
Browse files Browse the repository at this point in the history
PR-URL: #18784
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
  • Loading branch information
juggernaut451 authored and BridgeAR committed Feb 16, 2018
1 parent 862389b commit 2960096
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions test/parallel/test-tls-over-http-tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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} ` +
Expand All @@ -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: {
Expand All @@ -117,7 +120,7 @@ proxy.listen(0, function() {

function onUpgrade(res, socket, head) {
// Hacky.
process.nextTick(function() {
process.nextTick(() => {
onConnect(res, socket, head);
});
}
Expand Down Expand Up @@ -145,29 +148,29 @@ 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.
if (er.code !== 'ECONNRESET')
throw er;
}).end();
}
});
}));

process.on('exit', function() {
process.on('exit', () => {
assert.ok(gotRequest);
});

0 comments on commit 2960096

Please sign in to comment.