Skip to content

Commit

Permalink
test: keep regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Jun 17, 2024
1 parent 455ed89 commit 54f5585
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/issues/374/routes/entity/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
7 changes: 7 additions & 0 deletions test/issues/374/routes/entity/_entity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url, hooked: req.hooked, params: req.params })
})
}
7 changes: 7 additions & 0 deletions test/issues/374/routes/entity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url, hooked: req.hooked })
})
}
7 changes: 7 additions & 0 deletions test/issues/374/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url })
})
}
44 changes: 44 additions & 0 deletions test/issues/374/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const t = require('tap')
const path = require('node:path')
const Fastify = require('fastify')
const autoLoad = require('../../../')

t.plan(10)

const app = Fastify()

app.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
autoHooks: true,
cascadeHooks: true
})

app.ready(function (err) {
t.error(err)

app.inject({
url: '/'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/' })
})

app.inject({
url: '/entity'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/entity', hooked: ['root'] })
})

app.inject({
url: '/entity/1'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/entity/1', hooked: ['root'], params: { entity: 1 } })
})
})
8 changes: 8 additions & 0 deletions test/issues/376/routes/entity/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
7 changes: 7 additions & 0 deletions test/issues/376/routes/entity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url, hooked: req.hooked })
})
}
7 changes: 7 additions & 0 deletions test/issues/376/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url })
})
}
58 changes: 58 additions & 0 deletions test/issues/376/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const t = require('tap')
const path = require('node:path')
const Fastify = require('fastify')
const autoLoad = require('../../../')

t.plan(13)

const app = Fastify()

app.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
autoHooks: true,
options: { prefix: '/api' }
})

app.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
autoHooks: true,
options: { prefix: '/prefix/' }
})

app.ready(function (err) {
t.error(err)

app.inject({
url: '/api'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/api' })
})

app.inject({
url: '/api/entity'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/api/entity', hooked: ['root'] })
})

app.inject({
url: '/prefix'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/prefix' })
})

app.inject({
url: '/prefix/entity'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/prefix/entity', hooked: ['root'] })
})
})

0 comments on commit 54f5585

Please sign in to comment.