11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { createServer } = require('http');
61cb0ef41Sopenharmony_ciconst { connect } = require('net');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// This test validates that the server returns 408
91cb0ef41Sopenharmony_ci// after server.requestTimeout if the client
101cb0ef41Sopenharmony_ci// does not complete a request when using pipelining.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst requestTimeout = common.platformTimeout(2000);
131cb0ef41Sopenharmony_ciconst server = createServer({
141cb0ef41Sopenharmony_ci  headersTimeout: 0,
151cb0ef41Sopenharmony_ci  requestTimeout,
161cb0ef41Sopenharmony_ci  keepAliveTimeout: 0,
171cb0ef41Sopenharmony_ci  connectionsCheckingInterval: requestTimeout / 4
181cb0ef41Sopenharmony_ci}, common.mustCallAtLeast((req, res) => {
191cb0ef41Sopenharmony_ci  res.writeHead(200, { 'Content-Type': 'text/plain' });
201cb0ef41Sopenharmony_ci  res.end();
211cb0ef41Sopenharmony_ci}));
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciassert.strictEqual(server.requestTimeout, requestTimeout);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
261cb0ef41Sopenharmony_ci  const client = connect(server.address().port);
271cb0ef41Sopenharmony_ci  let second = false;
281cb0ef41Sopenharmony_ci  let response = '';
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  client.setEncoding('utf8');
311cb0ef41Sopenharmony_ci  client.on('data', common.mustCallAtLeast((chunk) => {
321cb0ef41Sopenharmony_ci    response += chunk;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    // First response has ended
351cb0ef41Sopenharmony_ci    if (!second && response.endsWith('\r\n\r\n')) {
361cb0ef41Sopenharmony_ci      assert.strictEqual(
371cb0ef41Sopenharmony_ci        response.split('\r\n')[0],
381cb0ef41Sopenharmony_ci        'HTTP/1.1 200 OK'
391cb0ef41Sopenharmony_ci      );
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci      response = '';
421cb0ef41Sopenharmony_ci      second = true;
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci  }, 1));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  const errOrEnd = common.mustCall(function(err) {
471cb0ef41Sopenharmony_ci    if (!second) {
481cb0ef41Sopenharmony_ci      return;
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    assert.strictEqual(
521cb0ef41Sopenharmony_ci      response,
531cb0ef41Sopenharmony_ci      'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
541cb0ef41Sopenharmony_ci    );
551cb0ef41Sopenharmony_ci    server.close();
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  client.on('error', errOrEnd);
591cb0ef41Sopenharmony_ci  client.on('end', errOrEnd);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  // Send two requests using pipelining. Delay before finishing the second one
621cb0ef41Sopenharmony_ci  client.resume();
631cb0ef41Sopenharmony_ci  client.write('GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\nGET / HTTP/1.1\r\nConnection: ');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  // Complete the request
661cb0ef41Sopenharmony_ci  setTimeout(() => {
671cb0ef41Sopenharmony_ci    client.write('close\r\n\r\n');
681cb0ef41Sopenharmony_ci  }, requestTimeout * 2).unref();
691cb0ef41Sopenharmony_ci}));
70