0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http2-compat-serverresponse-finished.js
Robert Nagy 6f613d8abb http,stream: add writableEnded
This is work towards resolving the response.finished confusion and
future deprecation.

Note that implementation-wise, streams have both an ending and ended
state. However, in this case (in order to avoid confusion in user space)
writableEnded is equal to writable.ending. The ending vs ended situation
is internal state required for internal stream logic.

PR-URL: https://github.com/nodejs/node/pull/28934
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-08-17 00:28:06 -07:00

50 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 net = require('net');
// Http2ServerResponse.finished
const server = h2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
server.once('request', common.mustCall((request, response) => {
assert.ok(response.socket instanceof net.Socket);
assert.ok(response.connection instanceof net.Socket);
assert.strictEqual(response.socket, response.connection);
response.on('finish', common.mustCall(() => {
assert.strictEqual(response.socket, undefined);
assert.strictEqual(response.connection, undefined);
process.nextTick(common.mustCall(() => {
assert.ok(response.stream);
server.close();
}));
}));
assert.strictEqual(response.finished, false);
assert.strictEqual(response.writableEnded, false);
response.end();
assert.strictEqual(response.finished, true);
assert.strictEqual(response.writableEnded, true);
}));
const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(() => {
const headers = {
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.on('end', common.mustCall(() => {
client.close();
}));
request.end();
request.resume();
}));
}));