Skip to content

test: ensure compatibility with AsyncLocalStorage #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: v3
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions test/express-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import assert from 'node:assert'
import { promisify } from 'node:util'
import { AsyncLocalStorage } from 'node:async_hooks'

import express from 'express'
import FormData from 'form-data'
Expand All @@ -10,6 +11,7 @@ import _onFinished from 'on-finished'

import * as util from './_util.js'
import multer from '../index.js'
import http from 'node:http'

const onFinished = promisify(_onFinished)

Expand All @@ -18,9 +20,9 @@ const port = 34279
describe('Express Integration', () => {
let app, server

before((done) => {
before(() => {
app = express()
server = app.listen(port, done)
server = app.listen(port)
})

after((done) => {
Expand Down Expand Up @@ -105,4 +107,49 @@ describe('Express Integration', () => {
assert.strictEqual(result.body.toString(), 'SUCCESS')
assert.strictEqual(result.res.statusCode, 200)
})

it('should handle async local storage', async () => {
const upload = multer()
const router = new express.Router()
const form = new FormData()

const als = new AsyncLocalStorage()

form.append('avatar', util.file('large'))

router.use((_req, _res, next) => {
als.run({ hello: 'world' }, () => {
next()
})
})

router.post('/profile', upload.single('avatar'), (_, res) => {
res.status(200).end('SUCCESS')
})

router.get('/hello', (_, res) => {
const store = als.getStore()
res.status(200).json(store)
})

app.use('/t3', router)

const result = await submitForm(form, '/t3/profile')

assert.strictEqual(result.body.toString(), 'SUCCESS')
assert.strictEqual(result.res.statusCode, 200)

http.get(`http://localhost:${port}/t3/hello`, (res) => {
let data = ''

res.on('data', (chunk) => {
data += chunk
})

res.on('end', () => {
const store = JSON.parse(data)
assert.deepStrictEqual(store, { hello: 'world' })
})
})
})
})