CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
als.test.js78 linesDownload Raw Back to test
1'use strict'2 3const { AsyncLocalStorage } = require('node:async_hooks')4const { test } = require('node:test')5const Fastify = require('..')6const sget = require('simple-get').concat7 8test('Async Local Storage test', (t, done) => {9  t.plan(13)10  if (!AsyncLocalStorage) {11    t.skip('AsyncLocalStorage not available, skipping test')12    process.exit(0)13  }14 15  const storage = new AsyncLocalStorage()16  const app = Fastify({ logger: false })17 18  let counter = 019  app.addHook('onRequest', (req, reply, next) => {20    const id = counter++21    storage.run({ id }, next)22  })23 24  app.get('/', function (request, reply) {25    t.assert.ok(storage.getStore())26    const id = storage.getStore().id27    reply.send({ id })28  })29 30  app.post('/', function (request, reply) {31    t.assert.ok(storage.getStore())32    const id = storage.getStore().id33    reply.send({ id })34  })35 36  app.listen({ port: 0 }, function (err, address) {37    t.assert.ifError(err)38 39    sget({40      method: 'POST',41      url: 'http://localhost:' + app.server.address().port,42      body: {43        hello: 'world'44      },45      json: true46    }, (err, response, body) => {47      t.assert.ifError(err)48      t.assert.strictEqual(response.statusCode, 200)49      t.assert.deepStrictEqual(body, { id: 0 })50 51      sget({52        method: 'POST',53        url: 'http://localhost:' + app.server.address().port,54        body: {55          hello: 'world'56        },57        json: true58      }, (err, response, body) => {59        t.assert.ifError(err)60        t.assert.strictEqual(response.statusCode, 200)61        t.assert.deepStrictEqual(body, { id: 1 })62 63        sget({64          method: 'GET',65          url: 'http://localhost:' + app.server.address().port,66          json: true67        }, (err, response, body) => {68          t.assert.ifError(err)69          t.assert.strictEqual(response.statusCode, 200)70          t.assert.deepStrictEqual(body, { id: 2 })71          app.close()72          done()73        })74      })75    })76  })77})78