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,74 +1,65 @@
'use strict'
const { AsyncLocalStorage } = require('node:async_hooks')
const t = require('tap')
const { test } = require('node:test')
const Fastify = require('..')
const sget = require('simple-get').concat
if (!AsyncLocalStorage) {
t.skip('AsyncLocalStorage not available, skipping test')
process.exit(0)
}
test('Async Local Storage test', async (t) => {
t.plan(12)
if (!AsyncLocalStorage) {
t.skip('AsyncLocalStorage not available, skipping test')
process.exit(0)
}
const storage = new AsyncLocalStorage()
const app = Fastify({ logger: false })
const storage = new AsyncLocalStorage()
const app = Fastify({ logger: false })
let counter = 0
app.addHook('onRequest', (req, reply, next) => {
const id = counter++
storage.run({ id }, next)
})
t.after(() => app.close())
app.get('/', function (request, reply) {
t.ok(storage.getStore())
const id = storage.getStore().id
reply.send({ id })
})
app.post('/', function (request, reply) {
t.ok(storage.getStore())
const id = storage.getStore().id
reply.send({ id })
})
app.listen({ port: 0 }, function (err, address) {
t.error(err)
sget({
method: 'POST',
url: 'http://localhost:' + app.server.address().port,
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body, { id: 0 })
sget({
method: 'POST',
url: 'http://localhost:' + app.server.address().port,
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body, { id: 1 })
sget({
method: 'GET',
url: 'http://localhost:' + app.server.address().port,
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body, { id: 2 })
app.close()
t.end()
})
})
let counter = 0
app.addHook('onRequest', (req, reply, next) => {
const id = counter++
storage.run({ id }, next)
})
app.get('/', function (request, reply) {
t.assert.ok(storage.getStore())
const id = storage.getStore().id
reply.send({ id })
})
app.post('/', function (request, reply) {
t.assert.ok(storage.getStore())
const id = storage.getStore().id
reply.send({ id })
})
const fastifyServer = await app.listen({ port: 0 })
// First POST request
const result1 = await fetch(fastifyServer, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hello: 'world' })
})
t.assert.ok(result1.ok)
t.assert.strictEqual(result1.status, 200)
t.assert.deepStrictEqual(await result1.json(), { id: 0 })
const result2 = await fetch(fastifyServer, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ hello: 'world' })
})
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
t.assert.deepStrictEqual(await result2.json(), { id: 1 })
// GET request
const result3 = await fetch(fastifyServer, {
method: 'GET'
})
t.assert.ok(result3.ok)
t.assert.strictEqual(result3.status, 200)
t.assert.deepStrictEqual(await result3.json(), { id: 2 })
})