Skip to content

Commit 71e973f

Browse files
committed
test: ensure compatibility with AsyncLocalStorage
Signed-off-by: Sebastian Beltran <[email protected]>
1 parent 7dd1e55 commit 71e973f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/express-integration.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import assert from 'node:assert'
44
import { promisify } from 'node:util'
5+
import { AsyncLocalStorage } from 'node:async_hooks'
56

67
import express from 'express'
78
import FormData from 'form-data'
@@ -10,6 +11,7 @@ import _onFinished from 'on-finished'
1011

1112
import * as util from './_util.js'
1213
import multer from '../index.js'
14+
import http from 'node:http'
1315

1416
const onFinished = promisify(_onFinished)
1517

@@ -105,4 +107,49 @@ describe('Express Integration', () => {
105107
assert.strictEqual(result.body.toString(), 'SUCCESS')
106108
assert.strictEqual(result.res.statusCode, 200)
107109
})
110+
111+
it('should handle async local storage', async () => {
112+
const upload = multer()
113+
const router = new express.Router()
114+
const form = new FormData()
115+
116+
const als = new AsyncLocalStorage()
117+
118+
form.append('avatar', util.file('large'))
119+
120+
router.use((_req, _res, next) => {
121+
als.run({ hello: 'world' }, () => {
122+
next()
123+
})
124+
})
125+
126+
router.post('/profile', upload.single('avatar'), (_, res) => {
127+
res.status(200).end('SUCCESS')
128+
})
129+
130+
router.get('/hello', (_, res) => {
131+
const store = als.getStore()
132+
res.status(200).json(store)
133+
})
134+
135+
app.use('/t3', router)
136+
137+
const result = await submitForm(form, '/t3/profile')
138+
139+
assert.strictEqual(result.body.toString(), 'SUCCESS')
140+
assert.strictEqual(result.res.statusCode, 200)
141+
142+
http.get(`http://localhost:${port}/t3/hello`, (res) => {
143+
let data = ''
144+
145+
res.on('data', (chunk) => {
146+
data += chunk
147+
})
148+
149+
res.on('end', () => {
150+
const store = JSON.parse(data)
151+
assert.deepStrictEqual(store, { hello: 'world' })
152+
})
153+
})
154+
})
108155
})

0 commit comments

Comments
 (0)