Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make reply.redirect() and reply.callNotFound() return reply #4037

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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