0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/parallel/test-http2-options-max-headers-exceeds-nghttp2.js
Daeyeon Jeong 714e2d7eb4
http2: improve tests and docs
This commit documents the event parameters and `http2stream.respond`,
and adds some tests to ensure the actual behaviors are aligned with
the docs.

Testing the 'Http2Server.sessionError' event is added by updating
`test/parallel/test-http2-options-max-headers-exceeds-nghttp2.js`.
The event seemingly has not been tested so far.

`ServerHttp2Session` is exported to validate the `session` event
and the `sessionError` event.

Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com

PR-URL: https://github.com/nodejs/node/pull/42858
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
2022-05-24 12:29:56 +01:00

100 lines
2.5 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
if (!common.hasCrypto) common.skip('missing crypto');
const h2 = require('http2');
const assert = require('assert');
const { ServerHttp2Session } = require('internal/http2/core');
const server = h2.createServer();
server.on('stream', common.mustNotCall());
server.on('error', common.mustNotCall());
server.listen(0, common.mustCall(() => {
// Setting the maxSendHeaderBlockLength > nghttp2 threshold
// cause a 'sessionError' and no memory leak when session destroy
const options = {
maxSendHeaderBlockLength: 100000
};
const client = h2.connect(`http://localhost:${server.address().port}`,
options);
client.on('error', common.expectsError({
code: 'ERR_HTTP2_SESSION_ERROR',
name: 'Error',
message: 'Session closed with error code 9'
}));
const req = client.request({
// Greater than 65536 bytes
'test-header': 'A'.repeat(90000)
});
req.on('response', common.mustNotCall());
req.on('close', common.mustCall(() => {
client.close();
server.close();
}));
req.on('error', common.expectsError({
code: 'ERR_HTTP2_SESSION_ERROR',
name: 'Error',
message: 'Session closed with error code 9'
}));
req.end();
}));
{
const options = {
maxSendHeaderBlockLength: 100000,
};
const server = h2.createServer(options);
server.on('error', common.mustNotCall());
server.on(
'session',
common.mustCall((session) => {
assert.strictEqual(session instanceof ServerHttp2Session, true);
}),
);
server.on(
'stream',
common.mustCall((stream) => {
stream.additionalHeaders({
// Greater than 65536 bytes
'test-header': 'A'.repeat(90000),
});
stream.respond();
stream.end();
}),
);
server.on(
'sessionError',
common.mustCall((err, session) => {
assert.strictEqual(err.code, 'ERR_HTTP2_SESSION_ERROR');
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Session closed with error code 9');
assert.strictEqual(session instanceof ServerHttp2Session, true);
server.close();
}),
);
server.listen(
0,
common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
client.on('error', common.mustNotCall());
const req = client.request();
req.on('response', common.mustNotCall());
req.on('error', common.mustNotCall());
req.end();
}),
);
}