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_ciconst NGHTTP2_INTERNAL_ERROR = h2.constants.NGHTTP2_INTERNAL_ERROR;
91cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst server = h2.createServer();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Do not mustCall the server side callbacks, they may or may not be called
141cb0ef41Sopenharmony_ci// depending on the OS. The determination is based largely on operating
151cb0ef41Sopenharmony_ci// system specific timings
161cb0ef41Sopenharmony_ciserver.on('stream', (stream) => {
171cb0ef41Sopenharmony_ci  // Do not wrap in a must call or use common.expectsError (which now uses
181cb0ef41Sopenharmony_ci  // must call). The error may or may not be reported depending on operating
191cb0ef41Sopenharmony_ci  // system specific timings.
201cb0ef41Sopenharmony_ci  stream.on('error', (err) => {
211cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
221cb0ef41Sopenharmony_ci    assert.strictEqual(err.message,
231cb0ef41Sopenharmony_ci                       'Stream closed with error code NGHTTP2_INTERNAL_ERROR');
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci  stream.respond();
261cb0ef41Sopenharmony_ci  stream.end();
271cb0ef41Sopenharmony_ci});
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
301cb0ef41Sopenharmony_ci  const client = h2.connect(`http://localhost:${server.address().port}`);
311cb0ef41Sopenharmony_ci  const countdown = new Countdown(2, () => {
321cb0ef41Sopenharmony_ci    server.close();
331cb0ef41Sopenharmony_ci    client.close();
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci  client.on('connect', () => countdown.dec());
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const req = client.request();
381cb0ef41Sopenharmony_ci  req.destroy(new Error('test'));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  req.on('error', common.expectsError({
411cb0ef41Sopenharmony_ci    name: 'Error',
421cb0ef41Sopenharmony_ci    message: 'test'
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => {
461cb0ef41Sopenharmony_ci    assert.strictEqual(req.rstCode, NGHTTP2_INTERNAL_ERROR);
471cb0ef41Sopenharmony_ci    assert.strictEqual(req.rstCode, NGHTTP2_INTERNAL_ERROR);
481cb0ef41Sopenharmony_ci    countdown.dec();
491cb0ef41Sopenharmony_ci  }));
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  req.on('response', common.mustNotCall());
521cb0ef41Sopenharmony_ci  req.resume();
531cb0ef41Sopenharmony_ci  req.on('end', common.mustNotCall());
541cb0ef41Sopenharmony_ci}));
55