-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: process headers after setting up agent
Added tests to clarify the implicit behaviour of array header setting vs object header setting PR-URL: #16568 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
6b74064
commit 8b2a272
Showing
2 changed files
with
108 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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'] | ||
] }); |