summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-multistream-destroy-on-read-tls.js
blob: 91cbec6b2dc975224caceedca98ea988b462ab83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const http2 = require('http2');

// Regression test for https://github.com/nodejs/node/issues/29353.
// Test that it’s okay for an HTTP2 + TLS server to destroy a stream instance
// while reading it.

const server = http2.createSecureServer({
  key: fixtures.readKey('agent2-key.pem'),
  cert: fixtures.readKey('agent2-cert.pem')
});

const filenames = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

server.on('stream', common.mustCall((stream) => {
  function write() {
    stream.write('a'.repeat(10240));
    stream.once('drain', write);
  }
  write();
}, filenames.length));

server.listen(0, common.mustCall(() => {
  const client = http2.connect(`https://localhost:${server.address().port}`, {
    ca: fixtures.readKey('agent2-cert.pem'),
    servername: 'agent2'
  });

  let destroyed = 0;
  for (const entry of filenames) {
    const stream = client.request({
      ':path': `/${entry}`
    });
    stream.once('data', common.mustCall(() => {
      stream.destroy();

      if (++destroyed === filenames.length) {
        client.destroy();
        server.close();
      }
    }));
  }
}));