1'use strict';
2
3const common = require('../common');
4const http = require('http');
5const assert = require('assert');
6const Countdown = require('../common/countdown');
7
8// Test that certain response header fields do not repeat.
9// 'content-length' should also be in this list but it is
10// handled differently because multiple content-lengths are
11// an error (see test-http-response-multi-content-length.js).
12const norepeat = [
13  'content-type',
14  'user-agent',
15  'referer',
16  'host',
17  'authorization',
18  'proxy-authorization',
19  'if-modified-since',
20  'if-unmodified-since',
21  'from',
22  'location',
23  'max-forwards',
24  'retry-after',
25  'etag',
26  'last-modified',
27  'server',
28  'age',
29  'expires',
30];
31const runCount = 2;
32
33const server = http.createServer(function(req, res) {
34  const num = req.headers['x-num'];
35  if (num === '1') {
36    for (const name of norepeat) {
37      res.setHeader(name, ['A', 'B']);
38    }
39    res.setHeader('X-A', ['A', 'B']);
40  } else if (num === '2') {
41    const headers = {};
42    for (const name of norepeat) {
43      headers[name] = ['A', 'B'];
44    }
45    headers['X-A'] = ['A', 'B'];
46    res.writeHead(200, headers);
47  }
48  res.end('ok');
49});
50
51server.listen(0, common.mustCall(function() {
52  const countdown = new Countdown(runCount, () => server.close());
53  for (let n = 1; n <= runCount; n++) {
54    // This runs twice, the first time, the server will use
55    // setHeader, the second time it uses writeHead. The
56    // result on the client side should be the same in
57    // either case -- only the first instance of the header
58    // value should be reported for the header fields listed
59    // in the norepeat array.
60    http.get(
61      { port: this.address().port, headers: { 'x-num': n } },
62      common.mustCall(function(res) {
63        countdown.dec();
64        for (const name of norepeat) {
65          assert.strictEqual(res.headers[name], 'A');
66        }
67        assert.strictEqual(res.headers['x-a'], 'A, B');
68      })
69    );
70  }
71}));
72