• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const http2 = require('http2');
7const Countdown = require('../common/countdown');
8
9// Check that destroying the Http2ServerResponse stream produces
10// the expected result.
11
12const errors = [
13  'test-error',
14  Error('test'),
15];
16let nextError;
17
18const server = http2.createServer(common.mustCall((req, res) => {
19  req.on('error', common.mustNotCall());
20  res.on('error', common.mustNotCall());
21
22  res.on('finish', common.mustCall(() => {
23    res.destroy(nextError);
24    process.nextTick(() => {
25      res.destroy(nextError);
26    });
27  }));
28
29  if (req.url !== '/') {
30    nextError = errors.shift();
31  }
32
33  res.destroy(nextError);
34}, 3));
35
36server.listen(0, common.mustCall(() => {
37  const client = http2.connect(`http://localhost:${server.address().port}`);
38
39  const countdown = new Countdown(3, () => {
40    server.close();
41    client.close();
42  });
43
44  {
45    const req = client.request();
46    req.on('response', common.mustNotCall());
47    req.on('error', common.mustNotCall());
48    req.on('end', common.mustCall());
49    req.on('close', common.mustCall(() => countdown.dec()));
50    req.resume();
51  }
52
53  {
54    const req = client.request({ ':path': '/error' });
55
56    req.on('response', common.mustNotCall());
57    req.on('error', common.expectsError({
58      code: 'ERR_HTTP2_STREAM_ERROR',
59      name: 'Error',
60      message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
61    }));
62    req.on('close', common.mustCall(() => countdown.dec()));
63
64    req.resume();
65    req.on('end', common.mustCall());
66  }
67
68  {
69    const req = client.request({ ':path': '/error' });
70
71    req.on('response', common.mustNotCall());
72    req.on('error', common.expectsError({
73      code: 'ERR_HTTP2_STREAM_ERROR',
74      name: 'Error',
75      message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
76    }));
77    req.on('close', common.mustCall(() => countdown.dec()));
78
79    req.resume();
80    req.on('end', common.mustCall());
81  }
82}));
83