nodejs/test/parallel/test-http-dont-set-default-headers-with-setHost.js
Tim Perry 7a40aa75a5
http: add setDefaultHeaders option to http.request
This makes it possible to disable the various default headers directly
from the constructor. While this is possible for many use cases by
manually calling removeHeader on the request object instead, when
passing a raw header array to the request constructor the headers are
serialized and prepared to send immediately, and removeHeader cannot
subsequently be used.

With this change, it's now possible to 100% control sent request
headers by passing 'setDefaultHeaders: false' and a raw headers array to
http.request.

PR-URL: https://github.com/nodejs/node/pull/56112
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2024-12-12 16:43:10 +00:00

24 lines
557 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const server = http.createServer(common.mustCall(function(req, res) {
assert.deepStrictEqual(req.rawHeaders, [
'Host', `${common.localhostIPv4}:${server.address().port}`,
]);
res.end('ok');
server.close();
}));
server.listen(0, common.localhostIPv4, function() {
http.request({
method: 'POST',
host: common.localhostIPv4,
port: this.address().port,
setDefaultHeaders: false,
setHost: true
}).end();
});