Skip to content

Commit

Permalink
test: fixup test-http2-create-client-secure-session
Browse files Browse the repository at this point in the history
General improvements to test and verify that a secureConnect
handler is present

PR-URL: #17328
Fixes: #15303
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Sebastiaan Deckers <[email protected]>
  • Loading branch information
jasnell authored and MylesBorins committed Dec 12, 2017
1 parent 67abc1e commit 047bac2
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions test/parallel/test-http2-create-client-secure-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ function loadKey(keyname) {
function onStream(stream, headers) {
const socket = stream.session[kSocket];
assert(headers[':authority'].startsWith(socket.servername));
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.respond({ 'content-type': 'application/json' });
stream.end(JSON.stringify({
servername: socket.servername,
alpnProtocol: socket.alpnProtocol
Expand All @@ -33,35 +30,32 @@ function onStream(stream, headers) {
function verifySecureSession(key, cert, ca, opts) {
const server = h2.createSecureServer({ cert, key });
server.on('stream', common.mustCall(onStream));
server.listen(0);
server.on('listening', common.mustCall(function() {
const headers = { ':path': '/' };
if (!opts) {
opts = {};
}
server.listen(0, common.mustCall(() => {
opts = opts || { };
opts.secureContext = tls.createSecureContext({ ca });
const client = h2.connect(`https://localhost:${this.address().port}`, opts, function() {
const req = client.request(headers);
const client = h2.connect(`https://localhost:${server.address().port}`,
opts);
// Verify that a 'secureConnect' listener is attached
assert.strictEqual(client.socket.listenerCount('secureConnect'), 1);
const req = client.request();

req.on('response', common.mustCall(function(headers) {
assert.strictEqual(headers[':status'], 200, 'status code is set');
assert.strictEqual(headers['content-type'], 'text/html',
'content type is set');
assert(headers['date'], 'there is a date');
}));
req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
assert.strictEqual(headers['content-type'], 'application/json');
assert(headers['date']);
}));

let data = '';
req.setEncoding('utf8');
req.on('data', (d) => data += d);
req.on('end', common.mustCall(() => {
const jsonData = JSON.parse(data);
assert.strictEqual(jsonData.servername, opts.servername || 'localhost');
assert.strictEqual(jsonData.alpnProtocol, 'h2');
server.close();
client[kSocket].destroy();
}));
req.end();
});
let data = '';
req.setEncoding('utf8');
req.on('data', (d) => data += d);
req.on('end', common.mustCall(() => {
const jsonData = JSON.parse(data);
assert.strictEqual(jsonData.servername,
opts.servername || 'localhost');
assert.strictEqual(jsonData.alpnProtocol, 'h2');
server.close();
client[kSocket].destroy();
}));
}));
}

Expand Down

0 comments on commit 047bac2

Please sign in to comment.