forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathals.test.js
77 lines (67 loc) · 1.94 KB
/
als.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const { AsyncLocalStorage } = require('node:async_hooks')
const { test } = require('node:test')
const Fastify = require('..')
const sget = require('simple-get').concat
test('Async Local Storage test', (t, done) => {
t.plan(13)
if (!AsyncLocalStorage) {
t.skip('AsyncLocalStorage not available, skipping test')
process.exit(0)
}
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)
})
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 })
})
app.listen({ port: 0 }, function (err, address) {
t.assert.ifError(err)
sget({
method: 'POST',
url: 'http://localhost:' + app.server.address().port,
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body, { id: 0 })
sget({
method: 'POST',
url: 'http://localhost:' + app.server.address().port,
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body, { id: 1 })
sget({
method: 'GET',
url: 'http://localhost:' + app.server.address().port,
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(body, { id: 2 })
app.close()
done()
})
})
})
})
})