11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst h2 = require('http2');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst server = h2.createServer();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
121cb0ef41Sopenharmony_ci  assert(stream.session);
131cb0ef41Sopenharmony_ci  stream.session.destroy();
141cb0ef41Sopenharmony_ci  assert.strictEqual(stream.session, undefined);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  // Test that stream.state getter returns an empty object
171cb0ef41Sopenharmony_ci  // when the stream session has been destroyed
181cb0ef41Sopenharmony_ci  assert.deepStrictEqual({}, stream.state);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // Test that ERR_HTTP2_INVALID_STREAM is thrown while calling
211cb0ef41Sopenharmony_ci  // stream operations after the stream session has been destroyed
221cb0ef41Sopenharmony_ci  const invalidStreamError = {
231cb0ef41Sopenharmony_ci    name: 'Error',
241cb0ef41Sopenharmony_ci    code: 'ERR_HTTP2_INVALID_STREAM',
251cb0ef41Sopenharmony_ci    message: 'The stream has been destroyed'
261cb0ef41Sopenharmony_ci  };
271cb0ef41Sopenharmony_ci  assert.throws(() => stream.additionalHeaders(), invalidStreamError);
281cb0ef41Sopenharmony_ci  assert.throws(() => stream.priority(), invalidStreamError);
291cb0ef41Sopenharmony_ci  assert.throws(() => stream.respond(), invalidStreamError);
301cb0ef41Sopenharmony_ci  assert.throws(
311cb0ef41Sopenharmony_ci    () => stream.pushStream({}, common.mustNotCall()),
321cb0ef41Sopenharmony_ci    {
331cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_PUSH_DISABLED',
341cb0ef41Sopenharmony_ci      name: 'Error'
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci  );
371cb0ef41Sopenharmony_ci  // When session is destroyed all streams are destroyed and no further
381cb0ef41Sopenharmony_ci  // error should be emitted.
391cb0ef41Sopenharmony_ci  stream.on('error', common.mustNotCall());
401cb0ef41Sopenharmony_ci  assert.strictEqual(stream.write('data', common.expectsError({
411cb0ef41Sopenharmony_ci    name: 'Error',
421cb0ef41Sopenharmony_ci    code: 'ERR_STREAM_WRITE_AFTER_END',
431cb0ef41Sopenharmony_ci    message: 'write after end'
441cb0ef41Sopenharmony_ci  })), false);
451cb0ef41Sopenharmony_ci}));
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
481cb0ef41Sopenharmony_ci  const client = h2.connect(`http://localhost:${server.address().port}`);
491cb0ef41Sopenharmony_ci  const req = client.request();
501cb0ef41Sopenharmony_ci  req.resume();
511cb0ef41Sopenharmony_ci  req.on('end', common.mustCall());
521cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => server.close(common.mustCall())));
531cb0ef41Sopenharmony_ci}));
54