Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() {

ServerResponse.prototype.writeHead = writeHead;
function writeHead(statusCode, reason, obj) {

if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('write');
}

const originalStatusCode = statusCode;

statusCode |= 0;
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-domain-multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const server = http.createServer((req, res) => {

b.on('error', common.mustCall((er) => {
if (res) {
res.writeHead(500);
res.end('An error occurred');
// Introduce an error on the client by writing unexpected data.
// The client is now expecting a chunk header so any letter will have the parser throw an error.
res.socket.write('H');
}
// res.writeHead(500), res.destroy, etc.
server.close();
Expand Down
22 changes: 21 additions & 1 deletion test/parallel/test-http-write-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const s = http.createServer(common.mustCall((req, res) => {
}, {
code: 'ERR_HTTP_HEADERS_SENT',
name: 'Error',
message: 'Cannot render headers after they are sent to the client'
});

res.end();
Expand All @@ -76,3 +75,24 @@ function runTest() {
response.resume();
}));
}

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [ 'test', '1' ]);
assert.throws(() => res.writeHead(200, [ 'test2', '2' ]), {
code: 'ERR_HTTP_HEADERS_SENT',
name: 'Error',
});
res.end();
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, (res) => {
assert.strictEqual(res.headers.test, '1');
assert.strictEqual('test2' in res.headers, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, feel free to ignore.

Suggested change
assert.strictEqual('test2' in res.headers, false);
assert.strictEqual(res.headers.test2, undefined);

res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}