11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst h2 = require('http2');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// This test case ensures that calling of res.end after sending
91cb0ef41Sopenharmony_ci// 204, 205 and 304 HTTP statuses will not cause an error
101cb0ef41Sopenharmony_ci// See issue: https://github.com/nodejs/node/issues/21740
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  HTTP_STATUS_NO_CONTENT,
141cb0ef41Sopenharmony_ci  HTTP_STATUS_RESET_CONTENT,
151cb0ef41Sopenharmony_ci  HTTP_STATUS_NOT_MODIFIED
161cb0ef41Sopenharmony_ci} = h2.constants;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst statusWithoutBody = [
191cb0ef41Sopenharmony_ci  HTTP_STATUS_NO_CONTENT,
201cb0ef41Sopenharmony_ci  HTTP_STATUS_RESET_CONTENT,
211cb0ef41Sopenharmony_ci  HTTP_STATUS_NOT_MODIFIED,
221cb0ef41Sopenharmony_ci];
231cb0ef41Sopenharmony_ciconst STATUS_CODES_COUNT = statusWithoutBody.length;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst server = h2.createServer(common.mustCall(function(req, res) {
261cb0ef41Sopenharmony_ci  res.writeHead(statusWithoutBody.pop());
271cb0ef41Sopenharmony_ci  res.end();
281cb0ef41Sopenharmony_ci}, STATUS_CODES_COUNT));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
311cb0ef41Sopenharmony_ci  const url = `http://localhost:${server.address().port}`;
321cb0ef41Sopenharmony_ci  const client = h2.connect(url, common.mustCall(() => {
331cb0ef41Sopenharmony_ci    let responseCount = 0;
341cb0ef41Sopenharmony_ci    const closeAfterResponse = () => {
351cb0ef41Sopenharmony_ci      if (STATUS_CODES_COUNT === ++responseCount) {
361cb0ef41Sopenharmony_ci        client.destroy();
371cb0ef41Sopenharmony_ci        server.close();
381cb0ef41Sopenharmony_ci      }
391cb0ef41Sopenharmony_ci    };
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    for (let i = 0; i < STATUS_CODES_COUNT; i++) {
421cb0ef41Sopenharmony_ci      const request = client.request();
431cb0ef41Sopenharmony_ci      request.on('response', common.mustCall(closeAfterResponse));
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci}));
48