diff --git a/HISTORY.md b/HISTORY.md index 9c5f528..1c5e71c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ================== + * fix: prevent writing a response after headers are sent while flushing the request * fix: use the status code as the error body when it has no name * deps: * debug@^4.4.3 diff --git a/index.js b/index.js index e1e4a6e..28aa0a9 100644 --- a/index.js +++ b/index.js @@ -243,6 +243,11 @@ function getResponseStatusCode (res) { function send (req, res, status, headers, message) { function write () { + if (res.headersSent) { + debug('cannot %d after headers sent', status) + return + } + // response body var body = createHtmlDocument(message) diff --git a/test/test.js b/test/test.js index c463b3b..6dcc15c 100644 --- a/test/test.js +++ b/test/test.js @@ -536,6 +536,17 @@ var topDescribe = function (type, createServer) { }) describe('request started', function () { + it('should not respond after headers are sent while flushing the request', function (done) { + var server = createServer(function (req, res) { + finalhandler(req, res)() + res.end('respond not by finalhandler') + }) + + wrapper(request(server) + .post('/foo')) + .expect(200, 'respond not by finalhandler', done) + }) + it('should not respond', function (done) { var server = createServer(function (req, res) { var done = finalhandler(req, res)