Skip to content

Commit

Permalink
Fix content length (fastify#2546)
Browse files Browse the repository at this point in the history
* fix(reply): add content-length to supress error.headers['content-length']

* tests: add tests when content-length is wrong at errorHandler

* fix(reply): add content-length always on onSendEnd

* fix(reply): add undefined to content-length before errorHandler
  • Loading branch information
RafaelGSS authored Sep 9, 2020
1 parent ee84e87 commit f458cea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ function handleError (reply, error, cb) {
reply[kReplySent] = false
reply[kReplyIsError] = false
reply[kReplyErrorHandlerCalled] = true
reply[kReplyHeaders]['content-length'] = undefined
var result = errorHandler(error, reply.request, reply)
if (result && typeof result.then === 'function') {
wrapThenable(result, reply)
Expand Down
27 changes: 27 additions & 0 deletions test/content-length.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,30 @@ test('#2214 - wrong content-length', t => {
t.end()
})
})

test('#2543 - wrong content-length with errorHandler', t => {
const fastify = Fastify()

fastify.setErrorHandler((_error, _request, reply) => {
reply.code(500).send({ message: 'longer than 2 bytes' })
})

fastify.get('/', async () => {
const error = new Error('MY_ERROR_MESSAGE')
error.headers = {
'content-length': 2
}
throw error
})

fastify.inject({
method: 'GET',
path: '/'
})
.then(res => {
t.strictEqual(res.statusCode, 500)
t.strictEqual(res.headers['content-length'], '' + res.rawPayload.length)
t.deepEqual(JSON.parse(res.payload), { message: 'longer than 2 bytes' })
t.end()
})
})

0 comments on commit f458cea

Please sign in to comment.