Skip to content

Commit

Permalink
test: test case for multiple res.writeHead and res.getHeader
Browse files Browse the repository at this point in the history
PR-URL: #45508
Fixes: #36721
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
marco-ippolito committed Nov 24, 2022
1 parent be9cd3e commit c03354d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
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);
res.resume().on('end', common.mustCall(() => {
server.close();
}));
});
}));
}

0 comments on commit c03354d

Please sign in to comment.