Skip to content

Commit 5cf144d

Browse files
committed
fix(test): isolate content-encoding chain limit tests with per-test servers
The chain limit tests ran concurrently (node:test uses Promise.all within a Suite), so a shared server was unsafe: an aborted connection's async RST delivery on macOS could corrupt the server state for a sibling test. Replace the shared before/after server with a setupChainServer(t) helper that creates an isolated server+client pair per test and binds cleanup to t.after, which is scoped to each individual test context. Also removes the keepalive: false no-op (undici documents this flag as a noop outside of browser context).
1 parent 0bd6b30 commit 5cf144d

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

test/fetch/encoding.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,35 +99,32 @@ describe('content-encoding chain limit', () => {
9999
// Similar to urllib3 (GHSA-gm62-xv2j-4w53) and curl (CVE-2022-32206)
100100
const MAX_CONTENT_ENCODINGS = 5
101101

102-
let server
103-
before(async () => {
104-
server = createServer({
105-
noDelay: true
106-
}, (req, res) => {
102+
async function setupChainServer (t) {
103+
const server = createServer({ noDelay: true }, (req, res) => {
107104
const encodingCount = parseInt(req.headers['x-encoding-count'] || '1', 10)
108105
const encodings = Array(encodingCount).fill('identity').join(', ')
109-
110106
res.writeHead(200, {
111107
'Content-Encoding': encodings,
112108
'Content-Type': 'text/plain'
113109
})
114110
res.end('test')
115111
})
116112
await once(server.listen(0), 'listening')
117-
})
118-
119-
after(() => {
120-
server.closeAllConnections?.()
121-
server.close()
122-
})
123-
124-
test(`should allow exactly ${MAX_CONTENT_ENCODINGS} content-encodings`, async (t) => {
113+
t.after(async () => {
114+
server.closeAllConnections?.()
115+
server.close()
116+
await once(server, 'close')
117+
})
125118
const client = new Client(`http://localhost:${server.address().port}`)
126119
t.after(() => client.close())
120+
return { server, client }
121+
}
122+
123+
test(`should allow exactly ${MAX_CONTENT_ENCODINGS} content-encodings`, async (t) => {
124+
const { server, client } = await setupChainServer(t)
127125

128126
const response = await fetch(`http://localhost:${server.address().port}`, {
129127
dispatcher: client,
130-
keepalive: false,
131128
headers: { 'x-encoding-count': String(MAX_CONTENT_ENCODINGS) }
132129
})
133130

@@ -137,13 +134,11 @@ describe('content-encoding chain limit', () => {
137134
})
138135

139136
test(`should reject more than ${MAX_CONTENT_ENCODINGS} content-encodings`, async (t) => {
140-
const client = new Client(`http://localhost:${server.address().port}`)
141-
t.after(() => client.close())
137+
const { server, client } = await setupChainServer(t)
142138

143139
await t.assert.rejects(
144140
fetch(`http://localhost:${server.address().port}`, {
145141
dispatcher: client,
146-
keepalive: false,
147142
headers: { 'x-encoding-count': String(MAX_CONTENT_ENCODINGS + 1) }
148143
}),
149144
(err) => {
@@ -154,13 +149,11 @@ describe('content-encoding chain limit', () => {
154149
})
155150

156151
test('should reject excessive content-encoding chains', async (t) => {
157-
const client = new Client(`http://localhost:${server.address().port}`)
158-
t.after(() => client.close())
152+
const { server, client } = await setupChainServer(t)
159153

160154
await t.assert.rejects(
161155
fetch(`http://localhost:${server.address().port}`, {
162156
dispatcher: client,
163-
keepalive: false,
164157
headers: { 'x-encoding-count': '100' }
165158
}),
166159
(err) => {

0 commit comments

Comments
 (0)