node/test/parallel/test-http-write-head-after-set-header.js
Tim Perry 4b76ccea95
http: preserve raw header duplicates in writeHead after setHeader calls
writeHead accepts a raw header array, which is intended to allow
directly specifying raw header details, such as ordering, duplicates
and header key casing. When used by itself this works correctly.

However, if setHeader was called first, it effectively changed the
behaviour of subsequent writeHead calls, so that even if a raw header
array was provided, duplicates were collapsed, losing raw header data.

This change preserves the raw headers passed to writeHead, while still
maintaining the 'writeHead overwrites setHeader' behaviour.

PR-URL: https://github.com/nodejs/node/pull/50394
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-12-14 14:26:30 +00:00

48 lines
1.0 KiB
JavaScript

'use strict';
const common = require('../common');
const Countdown = require('../common/countdown');
const assert = require('assert');
const { createServer, request } = require('http');
const server = createServer(common.mustCall((req, res) => {
if (req.url.includes('setHeader')) {
res.setHeader('set-val', 'abc');
}
res.writeHead(200, [
'array-val', '1',
'array-val', '2',
]);
res.end();
}, 2));
const countdown = new Countdown(2, () => server.close());
server.listen(0, common.mustCall(() => {
request({
port: server.address().port
}, common.mustCall((res) => {
assert.deepStrictEqual(res.rawHeaders.slice(0, 4), [
'array-val', '1',
'array-val', '2',
]);
countdown.dec();
})).end();
request({
port: server.address().port,
path: '/?setHeader'
}, common.mustCall((res) => {
assert.deepStrictEqual(res.rawHeaders.slice(0, 6), [
'set-val', 'abc',
'array-val', '1',
'array-val', '2',
]);
countdown.dec();
})).end();
}));