diff --git a/lib/_http_server.js b/lib/_http_server.js index 52e25977769d31..bf1cd91b288491 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -347,7 +347,7 @@ function writeHead(statusCode, reason, obj) { // writeHead(statusCode[, headers]) if (!this.statusMessage) this.statusMessage = STATUS_CODES[statusCode] || 'unknown'; - obj = reason; + obj ??= reason; } this.statusCode = statusCode; diff --git a/test/parallel/test-http-write-head-2.js b/test/parallel/test-http-write-head-2.js index a47d0d72e3be56..d64b8259f01215 100644 --- a/test/parallel/test-http-write-head-2.js +++ b/test/parallel/test-http-write-head-2.js @@ -59,3 +59,21 @@ const http = require('http'); })); })); } + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.writeHead(200, undefined, [ 'foo', 'bar' ]); + res.end(); + })); + + server.listen(0, common.mustCall(() => { + http.get({ port: server.address().port }, common.mustCall((res) => { + assert.strictEqual(res.statusMessage, 'OK'); + assert.strictEqual(res.statusCode, 200); + assert.strictEqual(res.headers.foo, 'bar'); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + })); + })); +}