-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e107d5b
commit 7e83838
Showing
15 changed files
with
252 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
ts: true | ||
jsx: false | ||
flow: false | ||
coverage: true | ||
check-coverage: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('child') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ hooked: req.hooked }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
module.exports = new Promise((res) => { | ||
res(async function (app, opts) { | ||
app.addHook('onRequest', async (req, reply) => { | ||
req.hooked = req.hooked || [] | ||
req.hooked.push('promisified') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ hooked: req.hooked }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ hooked: req.hooked }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const path = require('path') | ||
const autoload = require('../../..') | ||
|
||
test('Should throw an error when trying to load invalid hooks', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'invalid-autohooks'), | ||
autoHooks: true | ||
}) | ||
|
||
await t.rejects(app.ready(), /Invalid or unexpected token/) | ||
}) | ||
|
||
test('Should throw an error when trying to import hooks plugin using index.ts if typescriptSupport is not enabled', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'invalid-index-type'), | ||
autoHooks: true | ||
}) | ||
|
||
await t.rejects(app.ready(), new Error(`@fastify/autoload cannot import hooks plugin at '${path.join(__dirname, 'invalid-index-type/index.ts')}'`)) | ||
}) | ||
|
||
test('Should not accumulate plugin if doesn\'t comply to matchFilter', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'routes') | ||
}) | ||
|
||
await app.ready() | ||
|
||
const res = await app.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
|
||
const app2 = Fastify() | ||
app2.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
matchFilter: /invalid/ | ||
}) | ||
|
||
await app2.ready() | ||
|
||
const res2 = await app2.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res2.statusCode, 404) | ||
}) | ||
|
||
test('Should be able to filter paths using a string', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
matchFilter: 'routes.js' | ||
}) | ||
|
||
await app.ready() | ||
|
||
const res = await app.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
|
||
const app2 = Fastify() | ||
app2.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
matchFilter: 'invalid-path' | ||
}) | ||
|
||
await app2.ready() | ||
|
||
const res2 = await app2.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res2.statusCode, 404) | ||
}) | ||
|
||
test('Should be able to filter paths using a function', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
matchFilter: (path) => path.includes('routes.js') | ||
}) | ||
|
||
await app.ready() | ||
|
||
const res = await app.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
|
||
const app2 = Fastify() | ||
app2.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
matchFilter: (path) => path.includes('invalid-path') | ||
}) | ||
|
||
await app2.ready() | ||
|
||
const res2 = await app2.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res2.statusCode, 404) | ||
}) | ||
|
||
test('Should not accumulate plugin if ignoreFilter is matched', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
ignoreFilter: /\/not-exists.js/ | ||
}) | ||
|
||
await app.ready() | ||
|
||
const res = await app.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
|
||
const app2 = Fastify() | ||
app2.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
ignoreFilter: /\/routes.js/, | ||
autoHooks: true | ||
}) | ||
|
||
await app2.ready() | ||
|
||
const res2 = await app2.inject({ | ||
url: '/' | ||
}) | ||
|
||
t.equal(res2.statusCode, 404) | ||
}) | ||
|
||
test('Should not set skip-override if hook plugin is not a function or async function', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
autoHooks: true, | ||
cascadeHooks: true | ||
}) | ||
|
||
app.decorateRequest('hooked', '') | ||
|
||
await app.ready() | ||
|
||
const res = await app.inject({ | ||
url: '/child' | ||
}) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.same(JSON.parse(res.payload), { hooked: ['root', 'child'] }) | ||
|
||
const res2 = await app.inject({ | ||
url: '/promisified' | ||
}) | ||
|
||
t.equal(res2.statusCode, 200) | ||
t.same(JSON.parse(res2.payload), { hooked: ['root'] }) | ||
}) | ||
|
||
test('Should not enrich non-SyntaxError', async (t) => { | ||
const app = Fastify() | ||
app.register(autoload, { | ||
dir: path.join(__dirname, 'non-SyntaxError'), | ||
autoHooks: true | ||
}) | ||
|
||
t.rejects(app.ready(), new ReferenceError('x is not defined')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters