• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const assert = require('assert');
7const http2 = require('http2');
8const Countdown = require('../common/countdown');
9
10const {
11  NGHTTP2_CANCEL,
12  NGHTTP2_NO_ERROR,
13  NGHTTP2_PROTOCOL_ERROR,
14  NGHTTP2_REFUSED_STREAM,
15  NGHTTP2_INTERNAL_ERROR
16} = http2.constants;
17
18const tests = [
19  [NGHTTP2_NO_ERROR, false],
20  [NGHTTP2_NO_ERROR, false],
21  [NGHTTP2_PROTOCOL_ERROR, true, 'NGHTTP2_PROTOCOL_ERROR'],
22  [NGHTTP2_CANCEL, false],
23  [NGHTTP2_REFUSED_STREAM, true, 'NGHTTP2_REFUSED_STREAM'],
24  [NGHTTP2_INTERNAL_ERROR, true, 'NGHTTP2_INTERNAL_ERROR'],
25];
26
27const server = http2.createServer();
28server.on('stream', (stream, headers) => {
29  const test = tests.find((t) => t[0] === Number(headers.rstcode));
30  if (test[1]) {
31    stream.on('error', common.expectsError({
32      name: 'Error',
33      code: 'ERR_HTTP2_STREAM_ERROR',
34      message: `Stream closed with error code ${test[2]}`
35    }));
36  }
37  stream.close(headers.rstcode | 0);
38});
39
40server.listen(0, common.mustCall(() => {
41  const client = http2.connect(`http://localhost:${server.address().port}`);
42
43  const countdown = new Countdown(tests.length, () => {
44    client.close();
45    server.close();
46  });
47
48  tests.forEach((test) => {
49    const req = client.request({
50      ':method': 'POST',
51      'rstcode': test[0]
52    });
53    req.on('close', common.mustCall(() => {
54      assert.strictEqual(req.rstCode, test[0]);
55      countdown.dec();
56    }));
57    req.on('aborted', common.mustCall());
58    if (test[1])
59      req.on('error', common.mustCall());
60    else
61      req.on('error', common.mustNotCall());
62  });
63}));
64