Skip to content
This repository was archived by the owner on Jul 6, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ function initializeTLSOptions(options, servername) {
options = initializeOptions(options);
options.ALPNProtocols = ['hc', 'h2'];
options.NPNProtocols = ['hc', 'h2'];
if (servername !== undefined) {
if (servername !== undefined && options.servername === undefined) {
options.servername = servername;
}
return options;
Expand Down
78 changes: 44 additions & 34 deletions test/parallel/test-http2-create-client-secure-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ const fs = require('fs');
const tls = require('tls');
const h2 = require('http2');

const key = loadKey('agent8-key.pem');
const cert = loadKey('agent8-cert.pem');
const ca = loadKey('fake-startcom-root-cert.pem');

function loadKey(keyname) {
return fs.readFileSync(
path.join(common.fixturesDir, 'keys', keyname), 'binary');
}

const server = h2.createSecureServer({cert, key});

// we use the lower-level API here
server.on('stream', common.mustCall(onStream));

function onStream(stream) {
stream.respond({
'content-type': 'text/html',
Expand All @@ -33,33 +24,52 @@ function onStream(stream) {
}));
}

server.listen(0);
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 = {};
}
opts.secureContext = tls.createSecureContext({ca});
const client = h2.connect(`https://localhost:${this.address().port}`, opts, function() {
const req = client.request(headers);

server.on('listening', common.mustCall(function() {
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');
}));

const headers = { ':path': '/' };
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(
jsonData.alpnProtocol === 'h2' || jsonData.alpnProtocol === 'hc');
server.close();
client.socket.destroy();
}));
req.end();
});
}));
}

const clientOptions = {secureContext: tls.createSecureContext({ca})};
const client = h2.connect(`https://localhost:${this.address().port}`, clientOptions, function() {
const req = client.request(headers);
// The server can be connected as 'localhost'.
verifySecureSession(
loadKey('agent8-key.pem'),
loadKey('agent8-cert.pem'),
loadKey('fake-startcom-root-cert.pem'));

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');
}));

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, 'localhost');
assert(jsonData.alpnProtocol === 'h2' || jsonData.alpnProtocol === 'hc');
server.close();
client.socket.destroy();
}));
req.end();
});
}));
// Custom servername is specified.
verifySecureSession(
loadKey('agent1-key.pem'),
loadKey('agent1-cert.pem'),
loadKey('ca1-cert.pem'),
{servername: 'agent1'});