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 http2 = require('http2');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst { kSocket } = require('internal/http2/util');
111cb0ef41Sopenharmony_ciconst { ServerHttp2Session } = require('internal/http2/core');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst server = http2.createServer();
141cb0ef41Sopenharmony_ciserver.on('stream', common.mustNotCall());
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet test = 0;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciserver.on('session', common.mustCall((session) => {
191cb0ef41Sopenharmony_ci  assert.strictEqual(session instanceof ServerHttp2Session, true);
201cb0ef41Sopenharmony_ci  switch (++test) {
211cb0ef41Sopenharmony_ci    case 1:
221cb0ef41Sopenharmony_ci      server.on('error', common.mustNotCall());
231cb0ef41Sopenharmony_ci      session.on('error', common.expectsError({
241cb0ef41Sopenharmony_ci        name: 'Error',
251cb0ef41Sopenharmony_ci        message: 'test'
261cb0ef41Sopenharmony_ci      }));
271cb0ef41Sopenharmony_ci      session[kSocket].emit('error', new Error('test'));
281cb0ef41Sopenharmony_ci      break;
291cb0ef41Sopenharmony_ci    case 2:
301cb0ef41Sopenharmony_ci      // If the server does not have a socketError listener,
311cb0ef41Sopenharmony_ci      // error will be silent on the server but will close
321cb0ef41Sopenharmony_ci      // the session
331cb0ef41Sopenharmony_ci      session[kSocket].emit('error', new Error('test'));
341cb0ef41Sopenharmony_ci      break;
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci}, 2));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciserver.on('sessionError', common.mustCall((err, session) => {
391cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'Error');
401cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'test');
411cb0ef41Sopenharmony_ci  assert.strictEqual(session instanceof ServerHttp2Session, true);
421cb0ef41Sopenharmony_ci}, 2));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
451cb0ef41Sopenharmony_ci  const url = `http://localhost:${server.address().port}`;
461cb0ef41Sopenharmony_ci  http2.connect(url)
471cb0ef41Sopenharmony_ci    .on('error', common.mustCall((err) => {
481cb0ef41Sopenharmony_ci      if (err.code !== 'ECONNRESET') {
491cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ERR_HTTP2_SESSION_ERROR');
501cb0ef41Sopenharmony_ci        assert.strictEqual(err.message, 'Session closed with error code 2');
511cb0ef41Sopenharmony_ci      }
521cb0ef41Sopenharmony_ci    }))
531cb0ef41Sopenharmony_ci    .on('close', () => {
541cb0ef41Sopenharmony_ci      server.removeAllListeners('error');
551cb0ef41Sopenharmony_ci      http2.connect(url)
561cb0ef41Sopenharmony_ci      .on('error', common.mustCall((err) => {
571cb0ef41Sopenharmony_ci        if (err.code !== 'ECONNRESET') {
581cb0ef41Sopenharmony_ci          assert.strictEqual(err.code, 'ERR_HTTP2_SESSION_ERROR');
591cb0ef41Sopenharmony_ci          assert.strictEqual(err.message, 'Session closed with error code 2');
601cb0ef41Sopenharmony_ci        }
611cb0ef41Sopenharmony_ci      }))
621cb0ef41Sopenharmony_ci        .on('close', () => server.close());
631cb0ef41Sopenharmony_ci    });
641cb0ef41Sopenharmony_ci}));
65