• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Flags: --expose-internals
3
4const common = require('../common');
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7const assert = require('assert');
8const http2 = require('http2');
9const { internalBinding } = require('internal/test/binding');
10const { Http2Stream } = internalBinding('http2');
11
12const server = http2.createServer();
13
14Http2Stream.prototype.respond = () => 1;
15server.on('stream', common.mustCall((stream) => {
16
17  // Send headers
18  stream.respond({ 'content-type': 'text/plain' });
19
20  // Should throw if headers already sent
21  assert.throws(
22    () => stream.respond(),
23    {
24      name: 'Error',
25      code: 'ERR_HTTP2_HEADERS_SENT',
26      message: 'Response has already been initiated.'
27    }
28  );
29
30  // Should throw if stream already destroyed
31  stream.destroy();
32  assert.throws(
33    () => stream.respond(),
34    {
35      name: 'Error',
36      code: 'ERR_HTTP2_INVALID_STREAM',
37      message: 'The stream has been destroyed'
38    }
39  );
40}));
41
42server.listen(0, common.mustCall(() => {
43  const client = http2.connect(`http://localhost:${server.address().port}`);
44  const req = client.request();
45
46  req.on('end', common.mustCall(() => {
47    client.close();
48    server.close();
49  }));
50  req.resume();
51  req.end();
52}));
53