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 http2 = require('http2');
81cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst {
111cb0ef41Sopenharmony_ci  NGHTTP2_CANCEL,
121cb0ef41Sopenharmony_ci  NGHTTP2_NO_ERROR,
131cb0ef41Sopenharmony_ci  NGHTTP2_PROTOCOL_ERROR,
141cb0ef41Sopenharmony_ci  NGHTTP2_REFUSED_STREAM,
151cb0ef41Sopenharmony_ci  NGHTTP2_INTERNAL_ERROR
161cb0ef41Sopenharmony_ci} = http2.constants;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst tests = [
191cb0ef41Sopenharmony_ci  [NGHTTP2_NO_ERROR, false],
201cb0ef41Sopenharmony_ci  [NGHTTP2_NO_ERROR, false],
211cb0ef41Sopenharmony_ci  [NGHTTP2_PROTOCOL_ERROR, true, 'NGHTTP2_PROTOCOL_ERROR'],
221cb0ef41Sopenharmony_ci  [NGHTTP2_CANCEL, false],
231cb0ef41Sopenharmony_ci  [NGHTTP2_REFUSED_STREAM, true, 'NGHTTP2_REFUSED_STREAM'],
241cb0ef41Sopenharmony_ci  [NGHTTP2_INTERNAL_ERROR, true, 'NGHTTP2_INTERNAL_ERROR'],
251cb0ef41Sopenharmony_ci];
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst server = http2.createServer();
281cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => {
291cb0ef41Sopenharmony_ci  const test = tests.find((t) => t[0] === Number(headers.rstcode));
301cb0ef41Sopenharmony_ci  if (test[1]) {
311cb0ef41Sopenharmony_ci    stream.on('error', common.expectsError({
321cb0ef41Sopenharmony_ci      name: 'Error',
331cb0ef41Sopenharmony_ci      code: 'ERR_HTTP2_STREAM_ERROR',
341cb0ef41Sopenharmony_ci      message: `Stream closed with error code ${test[2]}`
351cb0ef41Sopenharmony_ci    }));
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  stream.close(headers.rstcode | 0);
381cb0ef41Sopenharmony_ci});
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
411cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const countdown = new Countdown(tests.length, () => {
441cb0ef41Sopenharmony_ci    client.close();
451cb0ef41Sopenharmony_ci    server.close();
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  tests.forEach((test) => {
491cb0ef41Sopenharmony_ci    const req = client.request({
501cb0ef41Sopenharmony_ci      ':method': 'POST',
511cb0ef41Sopenharmony_ci      'rstcode': test[0]
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci    req.on('close', common.mustCall(() => {
541cb0ef41Sopenharmony_ci      assert.strictEqual(req.rstCode, test[0]);
551cb0ef41Sopenharmony_ci      countdown.dec();
561cb0ef41Sopenharmony_ci    }));
571cb0ef41Sopenharmony_ci    req.on('aborted', common.mustCall());
581cb0ef41Sopenharmony_ci    if (test[1])
591cb0ef41Sopenharmony_ci      req.on('error', common.mustCall());
601cb0ef41Sopenharmony_ci    else
611cb0ef41Sopenharmony_ci      req.on('error', common.mustNotCall());
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}));
64