• 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');
8
9const errCheck = common.expectsError({
10  name: 'Error',
11  code: 'ERR_STREAM_WRITE_AFTER_END',
12  message: 'write after end'
13}, 1);
14
15const {
16  HTTP2_HEADER_PATH,
17  HTTP2_HEADER_METHOD,
18  HTTP2_HEADER_STATUS,
19  HTTP2_METHOD_HEAD,
20} = http2.constants;
21
22const server = http2.createServer();
23server.on('stream', (stream, headers) => {
24
25  assert.strictEqual(headers[HTTP2_HEADER_METHOD], HTTP2_METHOD_HEAD);
26
27  stream.respond({ [HTTP2_HEADER_STATUS]: 200 });
28
29  // Because this is a head request, the outbound stream is closed automatically
30  stream.on('error', errCheck);
31  stream.write('data');
32});
33
34
35server.listen(0, () => {
36
37  const client = http2.connect(`http://localhost:${server.address().port}`);
38
39  const req = client.request({
40    [HTTP2_HEADER_METHOD]: HTTP2_METHOD_HEAD,
41    [HTTP2_HEADER_PATH]: '/'
42  });
43
44  req.on('response', common.mustCall((headers, flags) => {
45    assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200);
46    assert.strictEqual(flags, 5); // The end of stream flag is set
47  }));
48  req.on('data', common.mustNotCall());
49  req.on('end', common.mustCall(() => {
50    server.close();
51    client.close();
52  }));
53});
54