11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Test that certain response header fields do not repeat.
91cb0ef41Sopenharmony_ci// 'content-length' should also be in this list but it is
101cb0ef41Sopenharmony_ci// handled differently because multiple content-lengths are
111cb0ef41Sopenharmony_ci// an error (see test-http-response-multi-content-length.js).
121cb0ef41Sopenharmony_ciconst norepeat = [
131cb0ef41Sopenharmony_ci  'content-type',
141cb0ef41Sopenharmony_ci  'user-agent',
151cb0ef41Sopenharmony_ci  'referer',
161cb0ef41Sopenharmony_ci  'host',
171cb0ef41Sopenharmony_ci  'authorization',
181cb0ef41Sopenharmony_ci  'proxy-authorization',
191cb0ef41Sopenharmony_ci  'if-modified-since',
201cb0ef41Sopenharmony_ci  'if-unmodified-since',
211cb0ef41Sopenharmony_ci  'from',
221cb0ef41Sopenharmony_ci  'location',
231cb0ef41Sopenharmony_ci  'max-forwards',
241cb0ef41Sopenharmony_ci  'retry-after',
251cb0ef41Sopenharmony_ci  'etag',
261cb0ef41Sopenharmony_ci  'last-modified',
271cb0ef41Sopenharmony_ci  'server',
281cb0ef41Sopenharmony_ci  'age',
291cb0ef41Sopenharmony_ci  'expires',
301cb0ef41Sopenharmony_ci];
311cb0ef41Sopenharmony_ciconst runCount = 2;
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) {
341cb0ef41Sopenharmony_ci  const num = req.headers['x-num'];
351cb0ef41Sopenharmony_ci  if (num === '1') {
361cb0ef41Sopenharmony_ci    for (const name of norepeat) {
371cb0ef41Sopenharmony_ci      res.setHeader(name, ['A', 'B']);
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci    res.setHeader('X-A', ['A', 'B']);
401cb0ef41Sopenharmony_ci  } else if (num === '2') {
411cb0ef41Sopenharmony_ci    const headers = {};
421cb0ef41Sopenharmony_ci    for (const name of norepeat) {
431cb0ef41Sopenharmony_ci      headers[name] = ['A', 'B'];
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci    headers['X-A'] = ['A', 'B'];
461cb0ef41Sopenharmony_ci    res.writeHead(200, headers);
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci  res.end('ok');
491cb0ef41Sopenharmony_ci});
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
521cb0ef41Sopenharmony_ci  const countdown = new Countdown(runCount, () => server.close());
531cb0ef41Sopenharmony_ci  for (let n = 1; n <= runCount; n++) {
541cb0ef41Sopenharmony_ci    // This runs twice, the first time, the server will use
551cb0ef41Sopenharmony_ci    // setHeader, the second time it uses writeHead. The
561cb0ef41Sopenharmony_ci    // result on the client side should be the same in
571cb0ef41Sopenharmony_ci    // either case -- only the first instance of the header
581cb0ef41Sopenharmony_ci    // value should be reported for the header fields listed
591cb0ef41Sopenharmony_ci    // in the norepeat array.
601cb0ef41Sopenharmony_ci    http.get(
611cb0ef41Sopenharmony_ci      { port: this.address().port, headers: { 'x-num': n } },
621cb0ef41Sopenharmony_ci      common.mustCall(function(res) {
631cb0ef41Sopenharmony_ci        countdown.dec();
641cb0ef41Sopenharmony_ci        for (const name of norepeat) {
651cb0ef41Sopenharmony_ci          assert.strictEqual(res.headers[name], 'A');
661cb0ef41Sopenharmony_ci        }
671cb0ef41Sopenharmony_ci        assert.strictEqual(res.headers['x-a'], 'A, B');
681cb0ef41Sopenharmony_ci      })
691cb0ef41Sopenharmony_ci    );
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci}));
72