Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -1,14 +1,10 @@
'use strict'
/* eslint no-prototype-builtins: 0 */
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const Fastify = require('../fastify')
const sget = require('simple-get').concat
const fp = require('fastify-plugin')
test('if a plugin raises an error and there is not a callback to handle it, the server must not start', t => {
test('if a plugin raises an error and there is not a callback to handle it, the server must not start', (t, testDone) => {
t.plan(2)
const fastify = Fastify()
@@ -17,14 +13,16 @@ test('if a plugin raises an error and there is not a callback to handle it, the
})
fastify.listen({ port: 0 }, err => {
t.ok(err instanceof Error)
t.equal(err.message, 'err')
t.assert.ok(err instanceof Error)
t.assert.strictEqual(err.message, 'err')
testDone()
})
})
test('add hooks after route declaration', t => {
t.plan(3)
test('add hooks after route declaration', async t => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
function plugin (instance, opts, done) {
instance.decorateRequest('check', null)
@@ -59,26 +57,19 @@ test('add hooks after route declaration', t => {
done()
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.same(JSON.parse(body), { hook1: true, hook2: true, hook3: true })
fastify.close()
})
})
const result = await fetch(fastifyServer)
t.assert.ok(result.ok)
t.assert.deepStrictEqual(await result.json(), { hook1: true, hook2: true, hook3: true })
})
test('nested plugins', t => {
t.plan(5)
test('nested plugins', async t => {
t.plan(4)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (fastify, opts, done) {
fastify.register((fastify, opts, done) => {
@@ -98,33 +89,23 @@ test('nested plugins', t => {
done()
}, { prefix: '/parent' })
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
}, (err, response, body) => {
t.error(err)
t.same(body.toString(), 'I am child 1')
})
const result1 = await fetch(fastifyServer + '/parent/child1')
t.assert.ok(result1.ok)
t.assert.deepStrictEqual(await result1.text(), 'I am child 1')
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
}, (err, response, body) => {
t.error(err)
t.same(body.toString(), 'I am child 2')
})
})
const result2 = await fetch(fastifyServer + '/parent/child2')
t.assert.ok(result2.ok)
t.assert.deepStrictEqual(await result2.text(), 'I am child 2')
})
test('nested plugins awaited', t => {
t.plan(5)
test('nested plugins awaited', async t => {
t.plan(4)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(async function wrap (fastify, opts) {
await fastify.register(async function child1 (fastify, opts) {
@@ -140,28 +121,18 @@ test('nested plugins awaited', t => {
}, { prefix: '/child2' })
}, { prefix: '/parent' })
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/parent/child1'
}, (err, response, body) => {
t.error(err)
t.same(body.toString(), 'I am child 1')
})
const result1 = await fetch(fastifyServer + '/parent/child1')
t.assert.ok(result1.ok)
t.assert.deepStrictEqual(await result1.text(), 'I am child 1')
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/parent/child2'
}, (err, response, body) => {
t.error(err)
t.same(body.toString(), 'I am child 2')
})
})
const result2 = await fetch(fastifyServer + '/parent/child2')
t.assert.ok(result2.ok)
t.assert.deepStrictEqual(await result2.text(), 'I am child 2')
})
test('plugin metadata - decorators', t => {
test('plugin metadata - decorators', (t, testDone) => {
t.plan(1)
const fastify = Fastify()
@@ -181,7 +152,8 @@ test('plugin metadata - decorators', t => {
fastify.register(plugin)
fastify.ready(() => {
t.ok(fastify.plugin)
t.assert.ok(fastify.plugin)
testDone()
})
function plugin (instance, opts, done) {
@@ -190,7 +162,7 @@ test('plugin metadata - decorators', t => {
}
})
test('plugin metadata - decorators - should throw', t => {
test('plugin metadata - decorators - should throw', (t, testDone) => {
t.plan(1)
const fastify = Fastify()
@@ -208,7 +180,8 @@ test('plugin metadata - decorators - should throw', t => {
fastify.register(plugin)
fastify.ready((err) => {
t.equal(err.message, "The decorator 'plugin1' is not present in Request")
t.assert.strictEqual(err.message, "The decorator 'plugin1' is not present in Request")
testDone()
})
function plugin (instance, opts, done) {
@@ -217,7 +190,7 @@ test('plugin metadata - decorators - should throw', t => {
}
})
test('plugin metadata - decorators - should throw with plugin name', t => {
test('plugin metadata - decorators - should throw with plugin name', (t, testDone) => {
t.plan(1)
const fastify = Fastify()
@@ -236,7 +209,8 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
fastify.register(plugin)
fastify.ready((err) => {
t.equal(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
t.assert.strictEqual(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
testDone()
})
function plugin (instance, opts, done) {
@@ -245,7 +219,7 @@ test('plugin metadata - decorators - should throw with plugin name', t => {
}
})
test('plugin metadata - dependencies', t => {
test('plugin metadata - dependencies', (t, testDone) => {
t.plan(1)
const fastify = Fastify()
@@ -263,7 +237,8 @@ test('plugin metadata - dependencies', t => {
fastify.register(plugin)
fastify.ready(() => {
t.pass('everything right')
t.assert.ok('everything right')
testDone()
})
function dependency (instance, opts, done) {
@@ -275,7 +250,7 @@ test('plugin metadata - dependencies', t => {
}
})
test('plugin metadata - dependencies (nested)', t => {
test('plugin metadata - dependencies (nested)', (t, testDone) => {
t.plan(1)
const fastify = Fastify()
@@ -293,7 +268,8 @@ test('plugin metadata - dependencies (nested)', t => {
fastify.register(plugin)
fastify.ready(() => {
t.pass('everything right')
t.assert.ok('everything right')
testDone()
})
function dependency (instance, opts, done) {