0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-http2-sent-headers.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

48 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const server = h2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.additionalHeaders({ ':status': 102 });
assert.strictEqual(stream.sentInfoHeaders[0][':status'], 102);
stream.respond({ abc: 'xyz' }, { waitForTrailers: true });
stream.on('wantTrailers', () => {
stream.sendTrailers({ xyz: 'abc' });
});
assert.strictEqual(stream.sentHeaders.abc, 'xyz');
assert.strictEqual(stream.sentHeaders[':status'], 200);
assert.notStrictEqual(stream.sentHeaders.date, undefined);
stream.end();
stream.on('close', () => {
assert.strictEqual(stream.sentTrailers.xyz, 'abc');
});
}));
server.listen(0, common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.on('headers', common.mustCall((headers, flags) => {
assert.strictEqual(headers[':status'], 102);
assert.strictEqual(typeof flags === 'number', true);
}));
assert.strictEqual(req.sentHeaders[':method'], 'GET');
assert.strictEqual(req.sentHeaders[':authority'],
`localhost:${server.address().port}`);
assert.strictEqual(req.sentHeaders[':scheme'], 'http');
assert.strictEqual(req.sentHeaders[':path'], '/');
req.resume();
req.on('close', () => {
server.close();
client.close();
});
}));