Skip to content

Commit 2b72898

Browse files
authored
test: cover native fetch gzip decoding through the legacy bridge (#5874)
Adds a regression test for #5865. When Node's built-in fetch uses HTTP/2 through the legacy dispatcher bridge, the HTTP/2 controller's rawHeaders objects were forwarded as-is to the legacy callbacks, which dropped content-encoding and broke gzip decompression. The source is already reverted to force HTTP/1.1 for legacy consumers; this adds a fixture that asserts headers are preserved and the gzip body decodes through the legacy bridge, failing if the legacy HTTP/2 path is re-enabled without header normalization. Refs: #5865
1 parent 636bf51 commit 2b72898

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict'
2+
3+
// Regression test for https://github.com/nodejs/undici/issues/5865
4+
//
5+
// When Node.js's built-in fetch goes through the legacy dispatcher bridge
6+
// (Dispatcher1Wrapper -> LegacyHandlerWrapper) it used to negotiate HTTP/2 but
7+
// forward the HTTP/2 controller's rawHeaders/rawTrailers **objects** to the
8+
// legacy callbacks, which expect raw header arrays. This dropped response
9+
// headers (notably `content-encoding`), so compressed bytes reached
10+
// response.json() without being decompressed.
11+
//
12+
// This fixture specifically verifies Node.js's built-in fetch via the legacy
13+
// bridge: response headers must be preserved and the gzip body must decode.
14+
15+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
16+
17+
const { createSecureServer } = require('node:http2')
18+
const { once } = require('node:events')
19+
const { gzipSync } = require('node:zlib')
20+
const pem = require('@metcoder95/https-pem')
21+
const { Agent, setGlobalDispatcher } = require('../..')
22+
23+
async function main () {
24+
const rawBody = JSON.stringify({ ok: true })
25+
const compressedBody = gzipSync(rawBody)
26+
27+
// allowHTTP1 is required because the legacy bridge currently falls back to
28+
// HTTP/1.1 (the legacy consumers do not support HTTP/2).
29+
const server = createSecureServer({
30+
key: pem.key,
31+
cert: pem.cert,
32+
allowHTTP1: true
33+
})
34+
35+
// The compat 'request' handler serves both HTTP/1.1 requests and HTTP/2
36+
// streams (via the http2 compat layer), regardless of the negotiated
37+
// protocol.
38+
server.on('request', (_request, response) => {
39+
response.writeHead(200, {
40+
'content-type': 'application/json',
41+
'content-encoding': 'gzip'
42+
})
43+
response.end(compressedBody)
44+
})
45+
46+
const agent = new Agent({
47+
allowH2: true,
48+
connect: {
49+
rejectUnauthorized: false
50+
}
51+
})
52+
53+
try {
54+
server.listen(0, '127.0.0.1')
55+
await once(server, 'listening')
56+
setGlobalDispatcher(agent)
57+
58+
const url = `https://127.0.0.1:${server.address().port}/`
59+
// eslint-disable-next-line no-restricted-globals
60+
const response = await fetch(url)
61+
const contentEncoding = response.headers.get('content-encoding')
62+
const body = await response.json()
63+
64+
process.stdout.write(JSON.stringify({
65+
status: response.status,
66+
contentEncoding,
67+
body
68+
}))
69+
} finally {
70+
await agent.close()
71+
await new Promise((resolve) => server.close(resolve))
72+
}
73+
}
74+
75+
main().catch((err) => {
76+
console.error(err?.cause?.stack || err?.stack || err)
77+
process.exitCode = 1
78+
})

‎test/node-test/global-dispatcher-version.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ test('importing Undici does not break redirects in Node.js global fetch', () =>
119119
})
120120
})
121121

122+
// https://github.com/nodejs/undici/issues/5865
123+
test('Node.js global fetch preserves headers and decodes gzip through the legacy bridge', () => {
124+
const result = runNodeFile('test/fixtures/global-fetch-h2-headers.js')
125+
assert.strictEqual(result.status, 0, result.stderr)
126+
assert.deepStrictEqual(JSON.parse(result.stdout), {
127+
status: 200,
128+
contentEncoding: 'gzip',
129+
body: { ok: true }
130+
})
131+
})
132+
122133
test('setGlobalDispatcher lets Node.js global fetch use a MockAgent interceptor', () => {
123134
const script = `
124135
const { MockAgent, setGlobalDispatcher } = require('./index.js')

0 commit comments

Comments
 (0)