2017-07-17 17:29:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
2017-08-07 05:54:44 +00:00
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
2017-07-17 17:29:42 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const h2 = require('http2');
|
|
|
|
|
|
|
|
const server = h2.createServer();
|
2017-11-15 18:55:31 +00:00
|
|
|
server.on('stream', (stream) => {
|
2017-12-12 19:34:17 +00:00
|
|
|
stream.on('close', common.mustCall());
|
2017-11-15 18:55:31 +00:00
|
|
|
stream.respond();
|
|
|
|
stream.end('ok');
|
|
|
|
});
|
2017-07-17 17:29:42 +00:00
|
|
|
|
2017-12-12 19:34:17 +00:00
|
|
|
server.listen(0, common.mustCall(() => {
|
2017-07-17 17:29:42 +00:00
|
|
|
const client = h2.connect(`http://localhost:${server.address().port}`);
|
2017-12-12 19:34:17 +00:00
|
|
|
const req = client.request();
|
2018-02-18 08:11:06 +00:00
|
|
|
const closeCode = 1;
|
|
|
|
|
2018-04-11 01:10:22 +00:00
|
|
|
assert.throws(
|
2018-02-18 08:11:06 +00:00
|
|
|
() => req.close(2 ** 32),
|
|
|
|
{
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'RangeError',
|
2018-02-18 08:11:06 +00:00
|
|
|
code: 'ERR_OUT_OF_RANGE',
|
2018-04-11 01:10:22 +00:00
|
|
|
message: 'The value of "code" is out of range. It must be ' +
|
|
|
|
'>= 0 && <= 4294967295. Received 4294967296'
|
2018-02-18 08:11:06 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert.strictEqual(req.closed, false);
|
|
|
|
|
2018-02-28 15:18:29 +00:00
|
|
|
[true, 1, {}, [], null, 'test'].forEach((notFunction) => {
|
2019-12-25 17:02:16 +00:00
|
|
|
assert.throws(
|
2018-02-28 15:18:29 +00:00
|
|
|
() => req.close(closeCode, notFunction),
|
|
|
|
{
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'TypeError',
|
2022-01-24 16:09:16 +00:00
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2018-02-28 15:18:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert.strictEqual(req.closed, false);
|
|
|
|
});
|
|
|
|
|
2018-02-18 08:11:06 +00:00
|
|
|
req.close(closeCode, common.mustCall());
|
2017-12-12 19:34:17 +00:00
|
|
|
assert.strictEqual(req.closed, true);
|
2017-07-17 17:29:42 +00:00
|
|
|
|
2018-02-10 01:33:08 +00:00
|
|
|
// Make sure that destroy is called.
|
2017-07-17 17:29:42 +00:00
|
|
|
req._destroy = common.mustCall(req._destroy.bind(req));
|
|
|
|
|
2018-02-10 01:33:08 +00:00
|
|
|
// Second call doesn't do anything.
|
2018-02-18 08:11:06 +00:00
|
|
|
req.close(closeCode + 1);
|
2017-11-04 17:42:07 +00:00
|
|
|
|
2018-03-19 16:17:23 +00:00
|
|
|
req.on('close', common.mustCall(() => {
|
2017-07-17 17:29:42 +00:00
|
|
|
assert.strictEqual(req.destroyed, true);
|
2018-03-19 16:17:23 +00:00
|
|
|
assert.strictEqual(req.rstCode, closeCode);
|
2017-07-17 17:29:42 +00:00
|
|
|
server.close();
|
2017-12-12 19:34:17 +00:00
|
|
|
client.close();
|
|
|
|
}));
|
|
|
|
|
|
|
|
req.on('error', common.expectsError({
|
|
|
|
code: 'ERR_HTTP2_STREAM_ERROR',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'Error',
|
2018-02-23 21:28:29 +00:00
|
|
|
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
|
2017-07-17 17:29:42 +00:00
|
|
|
}));
|
|
|
|
|
2018-04-16 20:03:10 +00:00
|
|
|
// The `response` event should not fire as the server should receive the
|
|
|
|
// RST_STREAM frame before it ever has a chance to reply.
|
|
|
|
req.on('response', common.mustNotCall());
|
|
|
|
|
|
|
|
// The `end` event should still fire as we close the readable stream by
|
|
|
|
// pushing a `null` chunk.
|
2017-07-17 17:29:42 +00:00
|
|
|
req.on('end', common.mustCall());
|
2018-04-16 20:03:10 +00:00
|
|
|
|
|
|
|
req.resume();
|
2017-07-17 17:29:42 +00:00
|
|
|
req.end();
|
|
|
|
}));
|