Skip to content

Commit

Permalink
feat: make reply.redirect() and reply.callNotFound() return reply (#4037
Browse files Browse the repository at this point in the history
)

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jun 19, 2022
1 parent 2324549 commit 44589be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ Reply.prototype.redirect = function (code, url) {
}

this.header('location', url).code(code).send()
return this
}

Reply.prototype.callNotFound = function () {
notFound(this)
return this
}

Reply.prototype.getResponseTime = function () {
Expand Down
4 changes: 2 additions & 2 deletions test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ test('preHandler option for setNotFoundHandler', t => {

// https://github.com/fastify/fastify/issues/2229
t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', { timeout: 40000 }, t => {
t.plan(2)
t.plan(3)
const fastify = Fastify()

fastify.setNotFoundHandler({
Expand All @@ -1333,7 +1333,7 @@ test('preHandler option for setNotFoundHandler', t => {
})

fastify.post('/', function (req, reply) {
reply.callNotFound()
t.equal(reply.callNotFound(), reply)
})

fastify.inject({
Expand Down
12 changes: 12 additions & 0 deletions test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ test('within an instance', t => {
reply.redirect('/')
})

fastify.get('/redirect-async', async function (req, reply) {
return reply.redirect('/')
})

fastify.get('/redirect-code', function (req, reply) {
reply.redirect(301, '/')
})
Expand Down Expand Up @@ -412,6 +416,14 @@ test('within an instance', t => {
})
})

test('redirect with async function to `/` - 10', t => {
t.plan(1)

http.get('http://localhost:' + fastify.server.address().port + '/redirect-async', function (response) {
t.equal(response.statusCode, 302)
})
})

t.end()
})
})
Expand Down

0 comments on commit 44589be

Please sign in to comment.