1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const h2 = require('http2'); 8 9const server = h2.createServer(); 10 11server.on('stream', common.mustCall((stream) => { 12 assert(stream.session); 13 stream.session.destroy(); 14 assert.strictEqual(stream.session, undefined); 15 16 // Test that stream.state getter returns an empty object 17 // when the stream session has been destroyed 18 assert.deepStrictEqual({}, stream.state); 19 20 // Test that ERR_HTTP2_INVALID_STREAM is thrown while calling 21 // stream operations after the stream session has been destroyed 22 const invalidStreamError = { 23 name: 'Error', 24 code: 'ERR_HTTP2_INVALID_STREAM', 25 message: 'The stream has been destroyed' 26 }; 27 assert.throws(() => stream.additionalHeaders(), invalidStreamError); 28 assert.throws(() => stream.priority(), invalidStreamError); 29 assert.throws(() => stream.respond(), invalidStreamError); 30 assert.throws( 31 () => stream.pushStream({}, common.mustNotCall()), 32 { 33 code: 'ERR_HTTP2_PUSH_DISABLED', 34 name: 'Error' 35 } 36 ); 37 // When session is detroyed all streams are destroyed and no further 38 // error should be emitted. 39 stream.on('error', common.mustNotCall()); 40 assert.strictEqual(stream.write('data', common.expectsError({ 41 name: 'Error', 42 code: 'ERR_STREAM_WRITE_AFTER_END', 43 message: 'write after end' 44 })), false); 45})); 46 47server.listen(0, common.mustCall(() => { 48 const client = h2.connect(`http://localhost:${server.address().port}`); 49 const req = client.request(); 50 req.resume(); 51 req.on('end', common.mustCall()); 52 req.on('close', common.mustCall(() => server.close(common.mustCall()))); 53})); 54