summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-headers-array.js
blob: 31ee35a123ccb9a972409ed38703a5772ae8d18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';

require('../common');

const assert = require('assert');
const http = require('http');

function execute(options) {
  http.createServer(function(req, res) {
    const expectHeaders = {
      'x-foo': 'boom',
      'cookie': 'a=1; b=2; c=3',
      'connection': 'close'
    };

    // no Host header when you set headers an array
    if (!Array.isArray(options.headers)) {
      expectHeaders.host = `localhost:${this.address().port}`;
    }

    // no Authorization header when you set headers an array
    if (options.auth && !Array.isArray(options.headers)) {
      expectHeaders.authorization =
          `Basic ${Buffer.from(options.auth).toString('base64')}`;
    }

    this.close();

    assert.deepStrictEqual(req.headers, expectHeaders);

    res.end();
  }).listen(0, function() {
    options = Object.assign(options, {
      port: this.address().port,
      path: '/'
    });
    const req = http.request(options);
    req.end();
  });
}

// Should be the same except for implicit Host header on the first two
execute({ headers: { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } });
execute({ headers: { 'x-foo': 'boom', 'cookie': [ 'a=1', 'b=2', 'c=3' ] } });
execute({ headers: [[ 'x-foo', 'boom' ], [ 'cookie', 'a=1; b=2; c=3' ]] });
execute({ headers: [
  [ 'x-foo', 'boom' ], [ 'cookie', [ 'a=1', 'b=2', 'c=3' ]]
] });
execute({ headers: [
  [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ],
  [ 'cookie', 'b=2' ], [ 'cookie', 'c=3']
] });

// Authorization and Host header both missing from the second
execute({ auth: 'foo:bar', headers:
  { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } });
execute({ auth: 'foo:bar', headers: [
  [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ],
  [ 'cookie', 'b=2' ], [ 'cookie', 'c=3']
] });