From 91fd98bd10ab3cc020df913b960c8e82c9d60a95 Mon Sep 17 00:00:00 2001 From: juggernaut451 Date: Wed, 14 Feb 2018 22:22:12 +0530 Subject: [PATCH] test: refactor of test-tls-over-http-tunnel PR-URL: https://github.com/nodejs/node/pull/18784 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Daniel Bevenius --- test/parallel/test-tls-over-http-tunnel.js | 49 ++++++++++++---------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/test/parallel/test-tls-over-http-tunnel.js b/test/parallel/test-tls-over-http-tunnel.js index 8e9bafda5394d6..f3f2bb3f2726ed 100644 --- a/test/parallel/test-tls-over-http-tunnel.js +++ b/test/parallel/test-tls-over-http-tunnel.js @@ -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); });