11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst https = require('https');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst options = {
111cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
121cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
131cb0ef41Sopenharmony_ci};
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst TOTAL = 4;
161cb0ef41Sopenharmony_cilet waiting = TOTAL;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst server = https.Server(options, function(req, res) {
191cb0ef41Sopenharmony_ci  if (--waiting === 0) server.close();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const servername = req.socket.servername;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  if (servername !== false) {
241cb0ef41Sopenharmony_ci    res.setHeader('x-sni', servername);
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  res.end('hello world');
281cb0ef41Sopenharmony_ci});
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciserver.listen(0, function() {
311cb0ef41Sopenharmony_ci  function expectResponse(id) {
321cb0ef41Sopenharmony_ci    return common.mustCall(function(res) {
331cb0ef41Sopenharmony_ci      res.resume();
341cb0ef41Sopenharmony_ci      assert.strictEqual(res.headers['x-sni'],
351cb0ef41Sopenharmony_ci                         id === false ? undefined : `sni.${id}`);
361cb0ef41Sopenharmony_ci    });
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  const agent = new https.Agent({
401cb0ef41Sopenharmony_ci    maxSockets: 1
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci  for (let j = 0; j < TOTAL; j++) {
431cb0ef41Sopenharmony_ci    https.get({
441cb0ef41Sopenharmony_ci      agent: agent,
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci      path: '/',
471cb0ef41Sopenharmony_ci      port: this.address().port,
481cb0ef41Sopenharmony_ci      host: '127.0.0.1',
491cb0ef41Sopenharmony_ci      servername: `sni.${j}`,
501cb0ef41Sopenharmony_ci      rejectUnauthorized: false
511cb0ef41Sopenharmony_ci    }, expectResponse(j));
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci  https.get({
541cb0ef41Sopenharmony_ci    agent: agent,
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    path: '/',
571cb0ef41Sopenharmony_ci    port: this.address().port,
581cb0ef41Sopenharmony_ci    host: '127.0.0.1',
591cb0ef41Sopenharmony_ci    servername: '',
601cb0ef41Sopenharmony_ci    rejectUnauthorized: false
611cb0ef41Sopenharmony_ci  }, expectResponse(false));
621cb0ef41Sopenharmony_ci});
63