11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst http2 = require('http2');
71cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Check that destroying the Http2ServerResponse stream produces
101cb0ef41Sopenharmony_ci// the expected result.
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst errors = [
131cb0ef41Sopenharmony_ci  'test-error',
141cb0ef41Sopenharmony_ci  Error('test'),
151cb0ef41Sopenharmony_ci];
161cb0ef41Sopenharmony_cilet nextError;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst server = http2.createServer(common.mustCall((req, res) => {
191cb0ef41Sopenharmony_ci  req.on('error', common.mustNotCall());
201cb0ef41Sopenharmony_ci  res.on('error', common.mustNotCall());
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  res.on('finish', common.mustCall(() => {
231cb0ef41Sopenharmony_ci    res.destroy(nextError);
241cb0ef41Sopenharmony_ci    process.nextTick(() => {
251cb0ef41Sopenharmony_ci      res.destroy(nextError);
261cb0ef41Sopenharmony_ci    });
271cb0ef41Sopenharmony_ci  }));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  if (req.url !== '/') {
301cb0ef41Sopenharmony_ci    nextError = errors.shift();
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  res.destroy(nextError);
341cb0ef41Sopenharmony_ci}, 3));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
371cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  const countdown = new Countdown(3, () => {
401cb0ef41Sopenharmony_ci    server.close();
411cb0ef41Sopenharmony_ci    client.close();
421cb0ef41Sopenharmony_ci  });
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  {
451cb0ef41Sopenharmony_ci    const req = client.request();
461cb0ef41Sopenharmony_ci    req.on('response', common.mustNotCall());
471cb0ef41Sopenharmony_ci    req.on('error', common.mustNotCall());
481cb0ef41Sopenharmony_ci    req.on('end', common.mustCall());
491cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => countdown.dec()));
501cb0ef41Sopenharmony_ci    req.resume();
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  {
541cb0ef41Sopenharmony_ci    const req = client.request({ ':path': '/error' });
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    req.on('response', common.mustNotCall());
571cb0ef41Sopenharmony_ci    req.on('error', common.expectsError({
581cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_STREAM_ERROR',
591cb0ef41Sopenharmony_ci      name: 'Error',
601cb0ef41Sopenharmony_ci      message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
611cb0ef41Sopenharmony_ci    }));
621cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => countdown.dec()));
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    req.resume();
651cb0ef41Sopenharmony_ci    req.on('end', common.mustNotCall());
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  {
691cb0ef41Sopenharmony_ci    const req = client.request({ ':path': '/error' });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci    req.on('response', common.mustNotCall());
721cb0ef41Sopenharmony_ci    req.on('error', common.expectsError({
731cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_STREAM_ERROR',
741cb0ef41Sopenharmony_ci      name: 'Error',
751cb0ef41Sopenharmony_ci      message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
761cb0ef41Sopenharmony_ci    }));
771cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => countdown.dec()));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    req.resume();
801cb0ef41Sopenharmony_ci    req.on('end', common.mustNotCall());
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci}));
83