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