• 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 server = http2.createServer();
10server.listen(0, common.mustCall(() => {
11  const port = server.address().port;
12  server.once('request', common.mustCall((request, response) => {
13    response.addTrailers({
14      ABC: 123
15    });
16    response.setTrailer('ABCD', 123);
17
18    assert.throws(
19      () => response.addTrailers({ '': 'test' }),
20      {
21        code: 'ERR_INVALID_HTTP_TOKEN',
22        name: 'TypeError',
23        message: 'Header name must be a valid HTTP token [""]'
24      }
25    );
26    assert.throws(
27      () => response.setTrailer('test', undefined),
28      {
29        code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
30        name: 'TypeError',
31        message: 'Invalid value "undefined" for header "test"'
32      }
33    );
34    assert.throws(
35      () => response.setTrailer('test', null),
36      {
37        code: 'ERR_HTTP2_INVALID_HEADER_VALUE',
38        name: 'TypeError',
39        message: 'Invalid value "null" for header "test"'
40      }
41    );
42    assert.throws(
43      () => response.setTrailer(), // Trailer name undefined
44      {
45        code: 'ERR_INVALID_ARG_TYPE',
46        name: 'TypeError',
47        message: 'The "name" argument must be of type string. Received ' +
48                 'undefined'
49      }
50    );
51    assert.throws(
52      () => response.setTrailer(''),
53      {
54        code: 'ERR_INVALID_HTTP_TOKEN',
55        name: 'TypeError',
56        message: 'Header name must be a valid HTTP token [""]'
57      }
58    );
59
60    response.end('hello');
61  }));
62
63  const url = `http://localhost:${port}`;
64  const client = http2.connect(url, common.mustCall(() => {
65    const request = client.request();
66    request.on('trailers', common.mustCall((headers) => {
67      assert.strictEqual(headers.abc, '123');
68      assert.strictEqual(headers.abcd, '123');
69    }));
70    request.resume();
71    request.on('end', common.mustCall(() => {
72      client.close();
73      server.close();
74    }));
75  }));
76}));
77