• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const http2 = require('http2');
9const server = http2.createServer();
10const errRegEx = /Session closed with error code 7/;
11const destroyCode = http2.constants.NGHTTP2_REFUSED_STREAM;
12
13server.on('error', common.mustNotCall());
14
15server.on('session', (session) => {
16  session.on('close', common.mustCall());
17  session.on('error', common.mustCall((err) => {
18    assert.ok(errRegEx.test(err), `server session err: ${err}`);
19    assert.strictEqual(session.closed, false);
20    assert.strictEqual(session.destroyed, true);
21  }));
22
23  session.on('stream', common.mustCall((stream) => {
24    stream.on('error', common.mustCall((err) => {
25      assert.ok(errRegEx.test(err), `server stream err: ${err}`);
26      assert.strictEqual(session.closed, false);
27      assert.strictEqual(session.destroyed, true);
28      assert.strictEqual(stream.rstCode, destroyCode);
29    }));
30
31    session.destroy(destroyCode);
32  }));
33});
34
35server.listen(0, common.mustCall(() => {
36  const session = http2.connect(`http://localhost:${server.address().port}`);
37
38  session.on('error', common.mustCall((err) => {
39    assert.ok(errRegEx.test(err), `client session err: ${err}`);
40    assert.strictEqual(session.closed, false);
41    assert.strictEqual(session.destroyed, true);
42  }));
43
44  const stream = session.request({ [http2.constants.HTTP2_HEADER_PATH]: '/' });
45
46  stream.on('error', common.mustCall((err) => {
47    assert.ok(errRegEx.test(err), `client stream err: ${err}`);
48    assert.strictEqual(stream.rstCode, destroyCode);
49  }));
50
51  stream.on('close', common.mustCall(() => {
52    server.close();
53  }));
54}));
55