11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto) {
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { createServer } = require('https');
101cb0ef41Sopenharmony_ciconst { connect } = require('tls');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst options = {
151cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
161cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
171cb0ef41Sopenharmony_ci};
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cilet connections = 0;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst server = createServer(options, common.mustCall(function(req, res) {
221cb0ef41Sopenharmony_ci  res.writeHead(200, { Connection: 'keep-alive' });
231cb0ef41Sopenharmony_ci  res.end();
241cb0ef41Sopenharmony_ci}), {
251cb0ef41Sopenharmony_ci  headersTimeout: 0,
261cb0ef41Sopenharmony_ci  keepAliveTimeout: 0,
271cb0ef41Sopenharmony_ci  requestTimeout: common.platformTimeout(60000),
281cb0ef41Sopenharmony_ci});
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciserver.on('connection', function() {
311cb0ef41Sopenharmony_ci  connections++;
321cb0ef41Sopenharmony_ci});
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciserver.listen(0, function() {
351cb0ef41Sopenharmony_ci  const port = server.address().port;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // Create a first request but never finish it
381cb0ef41Sopenharmony_ci  const client1 = connect({ port, rejectUnauthorized: false });
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  client1.on('connect', common.mustCall(() => {
411cb0ef41Sopenharmony_ci    // Create a second request, let it finish but leave the connection opened using HTTP keep-alive
421cb0ef41Sopenharmony_ci    const client2 = connect({ port, rejectUnauthorized: false });
431cb0ef41Sopenharmony_ci    let response = '';
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    client2.setEncoding('utf8');
461cb0ef41Sopenharmony_ci    client2.on('data', common.mustCall((chunk) => {
471cb0ef41Sopenharmony_ci      response += chunk;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci      if (response.endsWith('0\r\n\r\n')) {
501cb0ef41Sopenharmony_ci        assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
511cb0ef41Sopenharmony_ci        assert.strictEqual(connections, 2);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci        server.closeAllConnections();
541cb0ef41Sopenharmony_ci        server.close(common.mustCall());
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci        // This timer should never go off as the server.close should shut everything down
571cb0ef41Sopenharmony_ci        setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref();
581cb0ef41Sopenharmony_ci      }
591cb0ef41Sopenharmony_ci    }));
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    client2.on('close', common.mustCall());
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    client2.write('GET / HTTP/1.1\r\n\r\n');
641cb0ef41Sopenharmony_ci  }));
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  client1.on('close', common.mustCall());
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  client1.on('error', () => {});
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  client1.write('GET / HTTP/1.1');
711cb0ef41Sopenharmony_ci});
72