Skip to content
Closed
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
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ function getResponseStatusCode (res) {

function send (req, res, status, headers, message) {
function write () {
// cannot actually respond
if (res.headersSent) {
debug('cannot respond after headers sent')
return
}

// response body
var body = createHtmlDocument(message)

Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ var topDescribe = function (type, createServer) {
})

describe('error response', function () {
it('should leave the response unchanged when headers are sent while waiting for the request', function (done) {
var buf = Buffer.alloc(1024 * 1024, '.')
var server = createServer(function (req, res, final) {
final(createError('oops!'))

// send the response while finalhandler waits for the request body
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('sent by middleware')
})
var test = wrapper(request(server).post('/foo'))
test.write(buf)
test.write(buf)
test.expect(200, 'sent by middleware', done)
})

it('should include error stack', function (done) {
wrapper(request(createServer(createError('boom!')))
.get('/foo'))
Expand Down