summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-client-rststream-before-connect.js
blob: d186f418b5b9aa2f5da5bba42eec22ed447504d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const { inspect } = require('util');

const server = h2.createServer();
server.on('stream', (stream) => {
  stream.on('close', common.mustCall());
  stream.respond();
  stream.end('ok');
});

server.listen(0, common.mustCall(() => {
  const client = h2.connect(`http://localhost:${server.address().port}`);
  const req = client.request();
  const closeCode = 1;

  assert.throws(
    () => req.close(2 ** 32),
    {
      name: 'RangeError',
      code: 'ERR_OUT_OF_RANGE',
      message: 'The value of "code" is out of range. It must be ' +
               '>= 0 && <= 4294967295. Received 4294967296'
    }
  );
  assert.strictEqual(req.closed, false);

  [true, 1, {}, [], null, 'test'].forEach((notFunction) => {
    common.expectsError(
      () => req.close(closeCode, notFunction),
      {
        type: TypeError,
        code: 'ERR_INVALID_CALLBACK',
        message: `Callback must be a function. Received ${inspect(notFunction)}`
      }
    );
    assert.strictEqual(req.closed, false);
  });

  req.close(closeCode, common.mustCall());
  assert.strictEqual(req.closed, true);

  // Make sure that destroy is called.
  req._destroy = common.mustCall(req._destroy.bind(req));

  // Second call doesn't do anything.
  req.close(closeCode + 1);

  req.on('close', common.mustCall(() => {
    assert.strictEqual(req.destroyed, true);
    assert.strictEqual(req.rstCode, closeCode);
    server.close();
    client.close();
  }));

  req.on('error', common.expectsError({
    code: 'ERR_HTTP2_STREAM_ERROR',
    type: Error,
    message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
  }));

  // The `response` event should not fire as the server should receive the
  // RST_STREAM frame before it ever has a chance to reply.
  req.on('response', common.mustNotCall());

  // The `end` event should still fire as we close the readable stream by
  // pushing a `null` chunk.
  req.on('end', common.mustCall());

  req.resume();
  req.end();
}));