mirror of
https://github.com/nodejs/node.git
synced 2025-05-10 14:09:34 +00:00

PR-URL: https://github.com/nodejs/node/pull/17406 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> This is a significant cleanup and refactoring of the cleanup/close/destroy logic for Http2Stream and Http2Session. There are significant changes here in the timing and ordering of cleanup logic, JS apis. and various related necessary edits.
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const assert = require('assert');
|
|
const h2 = require('http2');
|
|
const Countdown = require('../common/countdown');
|
|
|
|
const body =
|
|
'<html><head></head><body><h1>this is some data</h2></body></html>';
|
|
|
|
const server = h2.createServer();
|
|
const count = 100;
|
|
|
|
// we use the lower-level API here
|
|
server.on('stream', common.mustCall(onStream, count));
|
|
|
|
function onStream(stream, headers, flags) {
|
|
assert.strictEqual(headers[':scheme'], 'http');
|
|
assert.ok(headers[':authority']);
|
|
assert.strictEqual(headers[':method'], 'GET');
|
|
assert.strictEqual(flags, 5);
|
|
stream.respond({
|
|
'content-type': 'text/html',
|
|
':status': 200
|
|
});
|
|
stream.write(body.slice(0, 20));
|
|
stream.end(body.slice(20));
|
|
}
|
|
|
|
server.listen(0);
|
|
|
|
server.on('listening', common.mustCall(() => {
|
|
|
|
const client = h2.connect(`http://localhost:${server.address().port}`);
|
|
client.setMaxListeners(100);
|
|
|
|
client.on('goaway', console.log);
|
|
|
|
const countdown = new Countdown(count, () => {
|
|
client.close();
|
|
server.close();
|
|
});
|
|
|
|
for (let n = 0; n < count; n++) {
|
|
const req = client.request();
|
|
|
|
req.on('response', common.mustCall(function(headers) {
|
|
assert.strictEqual(headers[':status'], 200, 'status code is set');
|
|
assert.strictEqual(headers['content-type'], 'text/html',
|
|
'content type is set');
|
|
assert(headers['date'], 'there is a date');
|
|
}));
|
|
|
|
let data = '';
|
|
req.setEncoding('utf8');
|
|
req.on('data', (d) => data += d);
|
|
req.on('end', common.mustCall(() => {
|
|
assert.strictEqual(body, data);
|
|
}));
|
|
req.on('close', common.mustCall(() => countdown.dec()));
|
|
}
|
|
}));
|