CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
async_hooks.test.js72 linesDownload Raw Back to test
1'use strict'2 3const { createHook } = require('node:async_hooks')4const { test } = require('node:test')5const Fastify = require('..')6const sget = require('simple-get').concat7 8const remainingIds = new Set()9 10createHook({11  init (asyncId, type, triggerAsyncId, resource) {12    if (type === 'content-type-parser:run') {13      remainingIds.add(asyncId)14    }15  },16  destroy (asyncId) {17    remainingIds.delete(asyncId)18  }19})20 21const app = Fastify({ logger: false })22 23test('test async hooks', (t, done) => {24  app.get('/', function (request, reply) {25    reply.send({ id: 42 })26  })27 28  app.post('/', function (request, reply) {29    reply.send({ id: 42 })30  })31 32  app.listen({ port: 0 }, function (err, address) {33    t.assert.ifError(err)34 35    sget({36      method: 'POST',37      url: 'http://localhost:' + app.server.address().port,38      body: {39        hello: 'world'40      },41      json: true42    }, (err, response, body) => {43      t.assert.ifError(err)44      t.assert.strictEqual(response.statusCode, 200)45 46      sget({47        method: 'POST',48        url: 'http://localhost:' + app.server.address().port,49        body: {50          hello: 'world'51        },52        json: true53      }, (err, response, body) => {54        t.assert.ifError(err)55        t.assert.strictEqual(response.statusCode, 200)56 57        sget({58          method: 'GET',59          url: 'http://localhost:' + app.server.address().port,60          json: true61        }, (err, response, body) => {62          t.assert.ifError(err)63          t.assert.strictEqual(response.statusCode, 200)64          app.close()65          t.assert.strictEqual(remainingIds.size, 0)66          done()67        })68      })69    })70  })71})72