Skip to content

Commit

Permalink
test: test prefix behavior documented in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-michelet committed Jun 11, 2024
1 parent 852bc73 commit e1bf3bb
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ Each plugin can be individually configured using the following module properties
}
```
However, note that the `prefix` option should be set directly on `autoConfig` for autoloading to work as expected:
```js
export const autoConfig = (fastify) => {
return { name: 'y ' + fastify.rootName }
}

autoConfig.prefix = '/hello'
```
- `plugin.autoPrefix` - Set routing prefix for plugin
```js
Expand Down
20 changes: 19 additions & 1 deletion test/commonjs/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const t = require('tap')
const Fastify = require('fastify')

t.plan(101)
t.plan(107)

const app = Fastify()

Expand Down Expand Up @@ -270,4 +270,22 @@ app.ready(function (err) {
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { works: true })
})

app.inject({
url: '/configPrefix'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefix: true })
})

app.inject({
url: '/configPrefixCallback'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefixCallback: true })
})
})
13 changes: 13 additions & 0 deletions test/commonjs/basic/foo/configPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefix: true })
})

next()
}

module.exports.autoConfig = {
prefix: '/configPrefix'
}
14 changes: 14 additions & 0 deletions test/commonjs/basic/foo/configPrefixCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefixCallback: true })
})

next()
}

const options = () => ({})
options.prefix = '/configPrefixCallback'

module.exports.autoConfig = options
20 changes: 19 additions & 1 deletion test/module/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import t from 'tap'
import fastify from 'fastify'
import basicApp from './basic/app.js'

t.plan(74)
t.plan(80)

const app = fastify()

Expand Down Expand Up @@ -227,4 +227,22 @@ app.ready(function (err) {
statusCode: 404
})
})

app.inject({
url: '/configPrefix'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefix: true })
})

app.inject({
url: '/configPrefixCallback'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { configPrefixCallback: true })
})
})
13 changes: 13 additions & 0 deletions test/module/basic/foo/configPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

export default function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefix: true })
})

next()
}

export const autoConfig = {
prefix: '/configPrefix'
}
12 changes: 12 additions & 0 deletions test/module/basic/foo/configPrefixCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

export default function (f, opts, next) {
f.get('/', (request, reply) => {
reply.send({ configPrefixCallback: true })
})

next()
}

export const autoConfig = () => ({})
autoConfig.prefix = '/configPrefixCallback'

0 comments on commit e1bf3bb

Please sign in to comment.