11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst events = require('events');
91cb0ef41Sopenharmony_ciconst { createServer, connect } = require('http2');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cievents.captureRejections = true;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  // Test error thrown in the server 'stream' event,
151cb0ef41Sopenharmony_ci  // after a respond()
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  const server = createServer();
181cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall(async (stream) => {
191cb0ef41Sopenharmony_ci    server.close();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    stream.respond({ ':status': 200 });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    const _err = new Error('kaboom');
241cb0ef41Sopenharmony_ci    stream.on('error', common.mustCall((err) => {
251cb0ef41Sopenharmony_ci      assert.strictEqual(err, _err);
261cb0ef41Sopenharmony_ci    }));
271cb0ef41Sopenharmony_ci    throw _err;
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
311cb0ef41Sopenharmony_ci    const { port } = server.address();
321cb0ef41Sopenharmony_ci    const session = connect(`http://localhost:${port}`);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    const req = session.request();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    req.on('error', common.mustCall((err) => {
371cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
381cb0ef41Sopenharmony_ci    }));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
411cb0ef41Sopenharmony_ci      session.close();
421cb0ef41Sopenharmony_ci    }));
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci{
471cb0ef41Sopenharmony_ci  // Test error thrown in the server 'stream' event,
481cb0ef41Sopenharmony_ci  // before a respond().
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const server = createServer();
511cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall(async (stream) => {
521cb0ef41Sopenharmony_ci    server.close();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    stream.on('error', common.mustNotCall());
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    throw new Error('kaboom');
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
601cb0ef41Sopenharmony_ci    const { port } = server.address();
611cb0ef41Sopenharmony_ci    const session = connect(`http://localhost:${port}`);
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci    const req = session.request();
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((headers) => {
661cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 500);
671cb0ef41Sopenharmony_ci    }));
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
701cb0ef41Sopenharmony_ci      session.close();
711cb0ef41Sopenharmony_ci    }));
721cb0ef41Sopenharmony_ci  }));
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  // Test error thrown in 'request' event
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  const server = createServer(common.mustCall(async (req, res) => {
791cb0ef41Sopenharmony_ci    server.close();
801cb0ef41Sopenharmony_ci    res.setHeader('content-type', 'application/json');
811cb0ef41Sopenharmony_ci    const _err = new Error('kaboom');
821cb0ef41Sopenharmony_ci    throw _err;
831cb0ef41Sopenharmony_ci  }));
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
861cb0ef41Sopenharmony_ci    const { port } = server.address();
871cb0ef41Sopenharmony_ci    const session = connect(`http://localhost:${port}`);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    const req = session.request();
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((headers) => {
921cb0ef41Sopenharmony_ci      assert.strictEqual(headers[':status'], 500);
931cb0ef41Sopenharmony_ci      assert.strictEqual(Object.hasOwn(headers, 'content-type'), false);
941cb0ef41Sopenharmony_ci    }));
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
971cb0ef41Sopenharmony_ci      session.close();
981cb0ef41Sopenharmony_ci    }));
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    req.resume();
1011cb0ef41Sopenharmony_ci  }));
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci{
1051cb0ef41Sopenharmony_ci  // Test error thrown in the client 'stream' event
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  const server = createServer();
1081cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall(async (stream) => {
1091cb0ef41Sopenharmony_ci    const { port } = server.address();
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    server.close();
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci    stream.pushStream({
1141cb0ef41Sopenharmony_ci      ':scheme': 'http',
1151cb0ef41Sopenharmony_ci      ':path': '/foobar',
1161cb0ef41Sopenharmony_ci      ':authority': `localhost:${port}`,
1171cb0ef41Sopenharmony_ci    }, common.mustCall((err, push) => {
1181cb0ef41Sopenharmony_ci      push.respond({
1191cb0ef41Sopenharmony_ci        'content-type': 'text/html',
1201cb0ef41Sopenharmony_ci        ':status': 200
1211cb0ef41Sopenharmony_ci      });
1221cb0ef41Sopenharmony_ci      push.end('pushed by the server');
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci      stream.end('test');
1251cb0ef41Sopenharmony_ci    }));
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci    stream.respond({
1281cb0ef41Sopenharmony_ci      ':status': 200
1291cb0ef41Sopenharmony_ci    });
1301cb0ef41Sopenharmony_ci  }));
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
1331cb0ef41Sopenharmony_ci    const { port } = server.address();
1341cb0ef41Sopenharmony_ci    const session = connect(`http://localhost:${port}`);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci    const req = session.request();
1371cb0ef41Sopenharmony_ci    req.resume();
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci    session.on('stream', common.mustCall(async (stream) => {
1401cb0ef41Sopenharmony_ci      session.close();
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci      const _err = new Error('kaboom');
1431cb0ef41Sopenharmony_ci      stream.on('error', common.mustCall((err) => {
1441cb0ef41Sopenharmony_ci        assert.strictEqual(err, _err);
1451cb0ef41Sopenharmony_ci      }));
1461cb0ef41Sopenharmony_ci      throw _err;
1471cb0ef41Sopenharmony_ci    }));
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci    req.end();
1501cb0ef41Sopenharmony_ci  }));
1511cb0ef41Sopenharmony_ci}
152