Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of test-tls-over-http-tunnel #18784

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions test/parallel/test-tls-over-http-tunnel.js
Original file line number Diff line number Diff line change
@@ -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,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);
});