1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5if (!common.hasCrypto) common.skip('missing crypto');
6const h2 = require('http2');
7const assert = require('assert');
8const { ServerHttp2Session } = require('internal/http2/core');
9
10const server = h2.createServer();
11
12server.on('stream', common.mustNotCall());
13server.on('error', common.mustNotCall());
14
15server.listen(0, common.mustCall(() => {
16
17  // Setting the maxSendHeaderBlockLength > nghttp2 threshold
18  // cause a 'sessionError' and no memory leak when session destroy
19  const options = {
20    maxSendHeaderBlockLength: 100000
21  };
22
23  const client = h2.connect(`http://localhost:${server.address().port}`,
24                            options);
25  client.on('error', common.expectsError({
26    code: 'ERR_HTTP2_SESSION_ERROR',
27    name: 'Error',
28    message: 'Session closed with error code 9'
29  }));
30
31  const req = client.request({
32    // Greater than 65536 bytes
33    'test-header': 'A'.repeat(90000)
34  });
35  req.on('response', common.mustNotCall());
36
37  req.on('close', common.mustCall(() => {
38    client.close();
39    server.close();
40  }));
41
42  req.on('error', common.expectsError({
43    code: 'ERR_HTTP2_SESSION_ERROR',
44    name: 'Error',
45    message: 'Session closed with error code 9'
46  }));
47  req.end();
48}));
49
50{
51  const options = {
52    maxSendHeaderBlockLength: 100000,
53  };
54
55  const server = h2.createServer(options);
56
57  server.on('error', common.mustNotCall());
58  server.on(
59    'session',
60    common.mustCall((session) => {
61      assert.strictEqual(session instanceof ServerHttp2Session, true);
62    }),
63  );
64  server.on(
65    'stream',
66    common.mustCall((stream) => {
67      stream.additionalHeaders({
68        // Greater than 65536 bytes
69        'test-header': 'A'.repeat(90000),
70      });
71      stream.respond();
72      stream.end();
73    }),
74  );
75
76  server.on(
77    'sessionError',
78    common.mustCall((err, session) => {
79      assert.strictEqual(err.code, 'ERR_HTTP2_SESSION_ERROR');
80      assert.strictEqual(err.name, 'Error');
81      assert.strictEqual(err.message, 'Session closed with error code 9');
82      assert.strictEqual(session instanceof ServerHttp2Session, true);
83      server.close();
84    }),
85  );
86
87  server.listen(
88    0,
89    common.mustCall(() => {
90      const client = h2.connect(`http://localhost:${server.address().port}`);
91      client.on('error', common.mustNotCall());
92
93      const req = client.request();
94      req.on('response', common.mustNotCall());
95      req.on('error', common.mustNotCall());
96      req.end();
97    }),
98  );
99}
100