Skip to content

Commit

Permalink
fix #3099 (#3100)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed May 26, 2021
1 parent 0c39ac9 commit a848b0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Object.defineProperties(Reply.prototype, {
throw new FST_ERR_REP_SENT_VALUE()
}

if (this.sent) {
// We throw only if sent was overwritten from Fastify
if (this.sent && this[kReplySentOverwritten]) {
throw new FST_ERR_REP_ALREADY_SENT()
}

Expand Down
18 changes: 18 additions & 0 deletions test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ test('should throw error when attempting to set reply.sent more than once', t =>
reply.sent = true
try {
reply.sent = true
t.fail('must throw')
} catch (err) {
t.equal(err.code, 'FST_ERR_REP_ALREADY_SENT')
t.equal(err.message, 'Reply was already sent.')
Expand All @@ -1342,6 +1343,23 @@ test('should throw error when attempting to set reply.sent more than once', t =>
})
})

test('should not throw error when attempting to set reply.sent if the underlining request was sent', t => {
t.plan(3)
const fastify = require('../..')()

fastify.get('/', function (req, reply) {
reply.raw.end()
t.doesNotThrow(() => {
reply.sent = true
})
})

fastify.inject('/', (err, res) => {
t.error(err)
t.pass()
})
})

test('reply.getResponseTime() should return 0 before the timer is initialised on the reply by setting up response listeners', t => {
t.plan(1)
const response = { statusCode: 200 }
Expand Down

0 comments on commit a848b0f

Please sign in to comment.