11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst h2 = require('http2');
101cb0ef41Sopenharmony_ciconst { kSocket } = require('internal/http2/util');
111cb0ef41Sopenharmony_ciconst { once } = require('events');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = h2.createServer();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// We use the lower-level API here
161cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall(onStream));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction onStream(stream) {
191cb0ef41Sopenharmony_ci  stream.respond();
201cb0ef41Sopenharmony_ci  stream.write('test');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const socket = stream.session[kSocket];
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // When the socket is destroyed, the close events must be triggered
251cb0ef41Sopenharmony_ci  // on the socket, server and session.
261cb0ef41Sopenharmony_ci  socket.on('close', common.mustCall());
271cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
281cb0ef41Sopenharmony_ci  server.on('close', common.mustCall());
291cb0ef41Sopenharmony_ci  stream.session.on('close', common.mustCall(() => server.close()));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Also, the aborted event must be triggered on the stream
321cb0ef41Sopenharmony_ci  stream.on('aborted', common.mustCall());
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  assert.notStrictEqual(stream.session, undefined);
351cb0ef41Sopenharmony_ci  socket.destroy();
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciserver.listen(0);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciserver.on('listening', common.mustCall(async () => {
411cb0ef41Sopenharmony_ci  const client = h2.connect(`http://localhost:${server.address().port}`);
421cb0ef41Sopenharmony_ci  // The client may have an ECONNRESET error here depending on the operating
431cb0ef41Sopenharmony_ci  // system, due mainly to differences in the timing of socket closing. Do
441cb0ef41Sopenharmony_ci  // not wrap this in a common mustCall.
451cb0ef41Sopenharmony_ci  client.on('error', (err) => {
461cb0ef41Sopenharmony_ci    if (err.code !== 'ECONNRESET')
471cb0ef41Sopenharmony_ci      throw err;
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci  client.on('close', common.mustCall());
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  const req = client.request({ ':method': 'POST' });
521cb0ef41Sopenharmony_ci  // The client may have an ECONNRESET error here depending on the operating
531cb0ef41Sopenharmony_ci  // system, due mainly to differences in the timing of socket closing. Do
541cb0ef41Sopenharmony_ci  // not wrap this in a common mustCall.
551cb0ef41Sopenharmony_ci  req.on('error', (err) => {
561cb0ef41Sopenharmony_ci    if (err.code !== 'ECONNRESET')
571cb0ef41Sopenharmony_ci      throw err;
581cb0ef41Sopenharmony_ci  });
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  req.on('aborted', common.mustCall());
611cb0ef41Sopenharmony_ci  req.resume();
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  try {
641cb0ef41Sopenharmony_ci    await once(req, 'end');
651cb0ef41Sopenharmony_ci  } catch {
661cb0ef41Sopenharmony_ci    // Continue regardless of error.
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci}));
69