• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// This test ensures that a Trailer header is set only when a chunked transfer
5// encoding is used.
6
7const assert = require('assert');
8const http = require('http');
9
10const server = http.createServer(common.mustCall(function(req, res) {
11  res.setHeader('Trailer', 'baz');
12  const trailerInvalidErr = {
13    code: 'ERR_HTTP_TRAILER_INVALID',
14    message: 'Trailers are invalid with this transfer encoding',
15    name: 'Error'
16  };
17  assert.throws(() => res.writeHead(200, { 'Content-Length': '2' }),
18                trailerInvalidErr);
19  res.removeHeader('Trailer');
20  res.end('ok');
21}));
22server.listen(0, common.mustCall(() => {
23  http.get({ port: server.address().port }, common.mustCall((res) => {
24    assert.strictEqual(res.statusCode, 200);
25    let buf = '';
26    res.on('data', (chunk) => {
27      buf += chunk;
28    }).on('end', common.mustCall(() => {
29      assert.strictEqual(buf, 'ok');
30    }));
31    server.close();
32  }));
33}));
34