diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 6d513160eb1..1eff3b3fba8 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -814,6 +814,14 @@ TLSSocket.prototype._init = function(socket, wrap) { } } + // We can only come here via [kWrapConnectedHandle]() call that happens + // if the connection is established with `autoSelectFamily` set to `true`. + const connectOptions = this[kConnectOptions]; + if (!options.isServer && connectOptions) { + if (connectOptions.servername) { + this.setServername(connectOptions.servername); + } + } if (options.handshakeTimeout > 0) this.setTimeout(options.handshakeTimeout, this._handleTimeout); diff --git a/test/parallel/test-https-happy-eyeballs.js b/test/parallel/test-https-happy-eyeballs.js index 789a95c551f..71a69f2fb20 100644 --- a/test/parallel/test-https-happy-eyeballs.js +++ b/test/parallel/test-https-happy-eyeballs.js @@ -80,7 +80,8 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) { // Test that IPV4 is reached if IPV6 is not reachable { createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) { - const ipv4Server = createServer(options, common.mustCall((_, res) => { + const ipv4Server = createServer(options, common.mustCall((req, res) => { + assert.strictEqual(req.socket.servername, 'example.org'); res.writeHead(200, { Connection: 'close' }); res.end('response-ipv4'); })); @@ -92,7 +93,8 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) { lookup, rejectUnauthorized: false, autoSelectFamily: true, - autoSelectFamilyAttemptTimeout + autoSelectFamilyAttemptTimeout, + servername: 'example.org', }, (res) => { assert.strictEqual(res.statusCode, 200); @@ -118,12 +120,14 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) { // Test that IPV4 is NOT reached if IPV6 is reachable if (common.hasIPv6) { createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) { - const ipv4Server = createServer(options, common.mustNotCall((_, res) => { + const ipv4Server = createServer(options, common.mustNotCall((req, res) => { + assert.strictEqual(req.socket.servername, 'example.org'); res.writeHead(200, { Connection: 'close' }); res.end('response-ipv4'); })); - const ipv6Server = createServer(options, common.mustCall((_, res) => { + const ipv6Server = createServer(options, common.mustCall((req, res) => { + assert.strictEqual(req.socket.servername, 'example.org'); res.writeHead(200, { Connection: 'close' }); res.end('response-ipv6'); })); @@ -139,6 +143,7 @@ if (common.hasIPv6) { rejectUnauthorized: false, autoSelectFamily: true, autoSelectFamilyAttemptTimeout, + servername: 'example.org', }, (res) => { assert.strictEqual(res.statusCode, 200);