1'use strict'; 2 3require('../common'); 4 5const assert = require('assert'); 6const http = require('http'); 7 8function execute(options) { 9 http.createServer(function(req, res) { 10 const expectHeaders = { 11 'x-foo': 'boom', 12 'cookie': 'a=1; b=2; c=3', 13 'connection': 'close' 14 }; 15 16 // no Host header when you set headers an array 17 if (!Array.isArray(options.headers)) { 18 expectHeaders.host = `localhost:${this.address().port}`; 19 } 20 21 // no Authorization header when you set headers an array 22 if (options.auth && !Array.isArray(options.headers)) { 23 expectHeaders.authorization = 24 `Basic ${Buffer.from(options.auth).toString('base64')}`; 25 } 26 27 this.close(); 28 29 assert.deepStrictEqual(req.headers, expectHeaders); 30 31 res.end(); 32 }).listen(0, function() { 33 options = Object.assign(options, { 34 port: this.address().port, 35 path: '/' 36 }); 37 const req = http.request(options); 38 req.end(); 39 }); 40} 41 42// Should be the same except for implicit Host header on the first two 43execute({ headers: { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } }); 44execute({ headers: { 'x-foo': 'boom', 'cookie': [ 'a=1', 'b=2', 'c=3' ] } }); 45execute({ headers: [[ 'x-foo', 'boom' ], [ 'cookie', 'a=1; b=2; c=3' ]] }); 46execute({ headers: [ 47 [ 'x-foo', 'boom' ], [ 'cookie', [ 'a=1', 'b=2', 'c=3' ]], 48] }); 49execute({ headers: [ 50 [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ], 51 [ 'cookie', 'b=2' ], [ 'cookie', 'c=3'], 52] }); 53 54// Authorization and Host header both missing from the second 55execute({ auth: 'foo:bar', headers: 56 { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } }); 57execute({ auth: 'foo:bar', headers: [ 58 [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ], 59 [ 'cookie', 'b=2' ], [ 'cookie', 'c=3'], 60] }); 61