Skip to content

Commit

Permalink
Updated to Fastify v4 (#66)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 11, 2022
1 parent 657fac4 commit 50efe67
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"homepage": "https://github.com/fastify/fastify-routes#readme",
"devDependencies": {
"@types/node": "^17.0.0",
"fastify": "^3.17.0",
"fastify": "^4.0.0-rc.2",
"pre-commit": "^1.2.2",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ function fastifyRoutes (fastify, options, next) {
}

module.exports = fp(fastifyRoutes, {
fastify: '>=1.1.0',
fastify: '4.x',
name: 'fastify-routes'
})
48 changes: 44 additions & 4 deletions test/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ const constrainedRoute = {
}

test('should correctly map routes', async (t) => {
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.register(plugin)
await fastify.register(plugin)

fastify.register(routeA, { prefix: '/v1' })
fastify.register(routeB, { prefix: '/v1' })
Expand Down Expand Up @@ -90,9 +90,9 @@ test('should correctly map routes', async (t) => {
})

test('should allow other later onRoute handlers to change route options', async (t) => {
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.register(plugin)
await fastify.register(plugin)
fastify.addHook('onRoute', (options) => {
options.constraints = { host: 'some-automatic-constraint.com' }
})
Expand All @@ -103,3 +103,43 @@ test('should allow other later onRoute handlers to change route options', async
t.equal(fastify.routes.get('/hello/:world')[0].url, '/hello/:world')
t.same(fastify.routes.get('/hello/:world')[0].constraints, { host: 'some-automatic-constraint.com' })
})

test('should correctly map routes with automatic HEAD routes', async (t) => {
const fastify = Fastify()

await fastify.register(plugin)

fastify.register(routeA, { prefix: '/v1' })
fastify.register(routeB, { prefix: '/v1' })
fastify.route(routeC)
fastify.route(constrainedRoute)

await fastify.ready()

t.equal(fastify.routes.get('/v1/hello/:world')[0].method, 'GET')
t.equal(fastify.routes.get('/v1/hello/:world')[0].url, '/v1/hello/:world')
t.equal(fastify.routes.get('/v1/hello/:world')[0].logLevel, 'warn')
t.equal(fastify.routes.get('/v1/hello/:world')[0].prefix, '/v1')
t.equal(fastify.routes.get('/v1/hello/:world')[0].bodyLimit, 1000)
t.equal(fastify.routes.get('/v1/hello/:world')[0].handler, handler)
t.same(fastify.routes.get('/v1/hello/:world')[0].schema, schema)

t.equal(fastify.routes.get('/v1/hello/:world')[1].method, 'HEAD')
t.equal(fastify.routes.get('/v1/hello/:world')[1].url, '/v1/hello/:world')
t.equal(fastify.routes.get('/v1/hello/:world')[1].logLevel, 'warn')
t.equal(fastify.routes.get('/v1/hello/:world')[1].prefix, '/v1')
t.equal(fastify.routes.get('/v1/hello/:world')[1].bodyLimit, 1000)
t.equal(fastify.routes.get('/v1/hello/:world')[1].handler, handler)
t.same(fastify.routes.get('/v1/hello/:world')[1].schema, schema)

t.equal(fastify.routes.get('/v1/hello/:world')[2].method, 'POST')
t.equal(fastify.routes.get('/v1/hello/:world')[2].url, '/v1/hello/:world')
t.equal(fastify.routes.get('/v1/hello/:world')[2].logLevel, 'info')
t.equal(fastify.routes.get('/v1/hello/:world')[2].prefix, '/v1')
t.equal(fastify.routes.get('/v1/hello/:world')[2].bodyLimit, 2000)
t.equal(fastify.routes.get('/v1/hello/:world')[2].handler, handler)

t.same(fastify.routes.get('/foo')[0].method, ['GET', 'HEAD'])
t.equal(fastify.routes.get('/foo')[0].constraints, undefined)
t.same(fastify.routes.get('/foo')[1].constraints, { host: 'fastify.io' })
})

0 comments on commit 50efe67

Please sign in to comment.