11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// When the response is ended immediately, `req` should emit `close`
81cb0ef41Sopenharmony_ci// after `res`
91cb0ef41Sopenharmony_ci{
101cb0ef41Sopenharmony_ci  const server = http.Server(common.mustCall((req, res) => {
111cb0ef41Sopenharmony_ci    let resClosed = false;
121cb0ef41Sopenharmony_ci    let reqClosed = false;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci    res.end();
151cb0ef41Sopenharmony_ci    let resFinished = false;
161cb0ef41Sopenharmony_ci    res.on('finish', common.mustCall(() => {
171cb0ef41Sopenharmony_ci      resFinished = true;
181cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
191cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, false);
201cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
211cb0ef41Sopenharmony_ci    }));
221cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
231cb0ef41Sopenharmony_ci    res.on('close', common.mustCall(() => {
241cb0ef41Sopenharmony_ci      resClosed = true;
251cb0ef41Sopenharmony_ci      assert.strictEqual(resFinished, true);
261cb0ef41Sopenharmony_ci      assert.strictEqual(reqClosed, false);
271cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, true);
281cb0ef41Sopenharmony_ci    }));
291cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
301cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
311cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, false);
321cb0ef41Sopenharmony_ci    }));
331cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
341cb0ef41Sopenharmony_ci      reqClosed = true;
351cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, true);
361cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, true);
371cb0ef41Sopenharmony_ci      assert.strictEqual(req._readableState.ended, true);
381cb0ef41Sopenharmony_ci    }));
391cb0ef41Sopenharmony_ci    res.socket.on('close', () => server.close());
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
431cb0ef41Sopenharmony_ci    http.get({ port: server.address().port }, common.mustCall());
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// When there's no `data` handler attached to `req`,
481cb0ef41Sopenharmony_ci// `req` should emit `close` after `res`.
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const server = http.Server(common.mustCall((req, res) => {
511cb0ef41Sopenharmony_ci    let resClosed = false;
521cb0ef41Sopenharmony_ci    let reqClosed = false;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    // This time, don't end the response immediately
551cb0ef41Sopenharmony_ci    setTimeout(() => res.end(), 100);
561cb0ef41Sopenharmony_ci    let resFinished = false;
571cb0ef41Sopenharmony_ci    res.on('finish', common.mustCall(() => {
581cb0ef41Sopenharmony_ci      resFinished = true;
591cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
601cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, false);
611cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
621cb0ef41Sopenharmony_ci    }));
631cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
641cb0ef41Sopenharmony_ci    res.on('close', common.mustCall(() => {
651cb0ef41Sopenharmony_ci      resClosed = true;
661cb0ef41Sopenharmony_ci      assert.strictEqual(resFinished, true);
671cb0ef41Sopenharmony_ci      assert.strictEqual(reqClosed, false);
681cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, true);
691cb0ef41Sopenharmony_ci    }));
701cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
711cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
721cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, false);
731cb0ef41Sopenharmony_ci    }));
741cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
751cb0ef41Sopenharmony_ci      reqClosed = true;
761cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, true);
771cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, true);
781cb0ef41Sopenharmony_ci      assert.strictEqual(req._readableState.ended, true);
791cb0ef41Sopenharmony_ci    }));
801cb0ef41Sopenharmony_ci    res.socket.on('close', () => server.close());
811cb0ef41Sopenharmony_ci  }));
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
841cb0ef41Sopenharmony_ci    http.get({ port: server.address().port }, common.mustCall());
851cb0ef41Sopenharmony_ci  }));
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci// When a `data` handler is `attached` to `req`
891cb0ef41Sopenharmony_ci// (i.e. the server is consuming  data from it), `req` should emit `close`
901cb0ef41Sopenharmony_ci// before `res`.
911cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/pull/33035 introduced this change in behavior.
921cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node/pull/33035#issuecomment-751482764
931cb0ef41Sopenharmony_ci{
941cb0ef41Sopenharmony_ci  const server = http.Server(common.mustCall((req, res) => {
951cb0ef41Sopenharmony_ci    let resClosed = false;
961cb0ef41Sopenharmony_ci    let reqClosed = false;
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci    // Don't end the response immediately
991cb0ef41Sopenharmony_ci    setTimeout(() => res.end(), 100);
1001cb0ef41Sopenharmony_ci    let resFinished = false;
1011cb0ef41Sopenharmony_ci    req.on('data', () => {});
1021cb0ef41Sopenharmony_ci    res.on('finish', common.mustCall(() => {
1031cb0ef41Sopenharmony_ci      resFinished = true;
1041cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
1051cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, false);
1061cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
1071cb0ef41Sopenharmony_ci    }));
1081cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
1091cb0ef41Sopenharmony_ci    res.on('close', common.mustCall(() => {
1101cb0ef41Sopenharmony_ci      resClosed = true;
1111cb0ef41Sopenharmony_ci      assert.strictEqual(resFinished, true);
1121cb0ef41Sopenharmony_ci      assert.strictEqual(reqClosed, true);
1131cb0ef41Sopenharmony_ci      assert.strictEqual(res.destroyed, true);
1141cb0ef41Sopenharmony_ci    }));
1151cb0ef41Sopenharmony_ci    assert.strictEqual(req.destroyed, false);
1161cb0ef41Sopenharmony_ci    req.on('end', common.mustCall(() => {
1171cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, false);
1181cb0ef41Sopenharmony_ci    }));
1191cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
1201cb0ef41Sopenharmony_ci      reqClosed = true;
1211cb0ef41Sopenharmony_ci      assert.strictEqual(resClosed, false);
1221cb0ef41Sopenharmony_ci      assert.strictEqual(req.destroyed, true);
1231cb0ef41Sopenharmony_ci      assert.strictEqual(req._readableState.ended, true);
1241cb0ef41Sopenharmony_ci    }));
1251cb0ef41Sopenharmony_ci    res.socket.on('close', () => server.close());
1261cb0ef41Sopenharmony_ci  }));
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1291cb0ef41Sopenharmony_ci    http.get({ port: server.address().port }, common.mustCall());
1301cb0ef41Sopenharmony_ci  }));
1311cb0ef41Sopenharmony_ci}
132