From e1bf3bb5a812044aee051c093d66430e6851e090 Mon Sep 17 00:00:00 2001 From: jean Date: Tue, 11 Jun 2024 12:34:26 +0200 Subject: [PATCH] test: test prefix behavior documented in doc --- README.md | 9 +++++++++ test/commonjs/basic.js | 20 ++++++++++++++++++- test/commonjs/basic/foo/configPrefix.js | 13 ++++++++++++ .../basic/foo/configPrefixCallback.js | 14 +++++++++++++ test/module/basic.js | 20 ++++++++++++++++++- test/module/basic/foo/configPrefix.js | 13 ++++++++++++ test/module/basic/foo/configPrefixCallback.js | 12 +++++++++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 test/commonjs/basic/foo/configPrefix.js create mode 100644 test/commonjs/basic/foo/configPrefixCallback.js create mode 100644 test/module/basic/foo/configPrefix.js create mode 100644 test/module/basic/foo/configPrefixCallback.js diff --git a/README.md b/README.md index 5f2ea988..dbe7b2f2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test/commonjs/basic.js b/test/commonjs/basic.js index 9ba5aec1..88186da3 100644 --- a/test/commonjs/basic.js +++ b/test/commonjs/basic.js @@ -3,7 +3,7 @@ const t = require('tap') const Fastify = require('fastify') -t.plan(101) +t.plan(107) const app = Fastify() @@ -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 }) + }) }) diff --git a/test/commonjs/basic/foo/configPrefix.js b/test/commonjs/basic/foo/configPrefix.js new file mode 100644 index 00000000..fc2232b2 --- /dev/null +++ b/test/commonjs/basic/foo/configPrefix.js @@ -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' +} diff --git a/test/commonjs/basic/foo/configPrefixCallback.js b/test/commonjs/basic/foo/configPrefixCallback.js new file mode 100644 index 00000000..f20ba70c --- /dev/null +++ b/test/commonjs/basic/foo/configPrefixCallback.js @@ -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 diff --git a/test/module/basic.js b/test/module/basic.js index 1e55ad6c..9aa91e77 100644 --- a/test/module/basic.js +++ b/test/module/basic.js @@ -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() @@ -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 }) + }) }) diff --git a/test/module/basic/foo/configPrefix.js b/test/module/basic/foo/configPrefix.js new file mode 100644 index 00000000..faf0e526 --- /dev/null +++ b/test/module/basic/foo/configPrefix.js @@ -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' +} diff --git a/test/module/basic/foo/configPrefixCallback.js b/test/module/basic/foo/configPrefixCallback.js new file mode 100644 index 00000000..7b3f595e --- /dev/null +++ b/test/module/basic/foo/configPrefixCallback.js @@ -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'