Skip to content

Commit a07d945

Browse files
authored
fix: align h2 empty body content-length methods with h1 (#5172)
1 parent 3d6f502 commit a07d945

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

lib/dispatcher/client-h2.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,10 @@ function writeH2 (client, request) {
804804
const expectsPayload = (
805805
method === 'PUT' ||
806806
method === 'POST' ||
807-
method === 'PATCH'
807+
method === 'PATCH' ||
808+
method === 'QUERY' ||
809+
method === 'PROPFIND' ||
810+
method === 'PROPPATCH'
808811
)
809812

810813
if (body && typeof body.read === 'function') {

test/http2-body.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,50 @@ test('Should handle h2 request without body', async t => {
7070
await t.completed
7171
})
7272

73+
test('Should send content-length: 0 for empty h2 requests with payload-expecting methods', async t => {
74+
const assert = tspl(t, { plan: 18 })
75+
const methods = ['PUT', 'POST', 'PATCH', 'QUERY', 'PROPFIND', 'PROPPATCH']
76+
77+
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))
78+
79+
server.on('stream', (stream, headers) => {
80+
assert.strictEqual(headers['content-length'], '0')
81+
assert.ok(methods.includes(headers[':method']))
82+
83+
stream.respond({ ':status': 200 })
84+
stream.end('ok')
85+
})
86+
87+
await once(server.listen(0), 'listening')
88+
89+
const client = new Client(`https://localhost:${server.address().port}`, {
90+
connect: {
91+
rejectUnauthorized: false
92+
},
93+
allowH2: true
94+
})
95+
96+
t.after(async () => {
97+
server.close()
98+
await client.close()
99+
})
100+
101+
for (const method of methods) {
102+
const response = await client.request({
103+
path: '/',
104+
method,
105+
body: ''
106+
})
107+
108+
assert.strictEqual(response.statusCode, 200)
109+
110+
response.body.resume()
111+
await once(response.body, 'end')
112+
}
113+
114+
await assert.completed
115+
})
116+
73117
test('Should handle h2 request with body (string or buffer) - dispatch', async t => {
74118
t = tspl(t, { plan: 7 })
75119

0 commit comments

Comments
 (0)