1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const h2 = require('http2');
7
8// This test case ensures that calling of res.end after sending
9// 204, 205 and 304 HTTP statuses will not cause an error
10// See issue: https://github.com/nodejs/node/issues/21740
11
12const {
13  HTTP_STATUS_NO_CONTENT,
14  HTTP_STATUS_RESET_CONTENT,
15  HTTP_STATUS_NOT_MODIFIED
16} = h2.constants;
17
18const statusWithoutBody = [
19  HTTP_STATUS_NO_CONTENT,
20  HTTP_STATUS_RESET_CONTENT,
21  HTTP_STATUS_NOT_MODIFIED,
22];
23const STATUS_CODES_COUNT = statusWithoutBody.length;
24
25const server = h2.createServer(common.mustCall(function(req, res) {
26  res.writeHead(statusWithoutBody.pop());
27  res.end();
28}, STATUS_CODES_COUNT));
29
30server.listen(0, common.mustCall(function() {
31  const url = `http://localhost:${server.address().port}`;
32  const client = h2.connect(url, common.mustCall(() => {
33    let responseCount = 0;
34    const closeAfterResponse = () => {
35      if (STATUS_CODES_COUNT === ++responseCount) {
36        client.destroy();
37        server.close();
38      }
39    };
40
41    for (let i = 0; i < STATUS_CODES_COUNT; i++) {
42      const request = client.request();
43      request.on('response', common.mustCall(closeAfterResponse));
44    }
45
46  }));
47}));
48