Skip to content

Commit

Permalink
fix: ignore content-length when dumping HEAD (#3222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored May 10, 2024
1 parent 3b1b2fb commit 077531a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RequestHandler extends AsyncResource {
throw err
}

this.method = method
this.responseHeaders = responseHeaders || null
this.opaque = opaque || null
this.callback = callback
Expand Down Expand Up @@ -114,7 +115,15 @@ class RequestHandler extends AsyncResource {
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const contentType = parsedHeaders['content-type']
const contentLength = parsedHeaders['content-length']
const res = new Readable({ resume, abort, contentType, contentLength, highWaterMark })
const res = new Readable({
resume,
abort,
contentType,
contentLength: this.method !== 'HEAD' && contentLength
? Number(contentLength)
: null,
highWaterMark
})

if (this.removeAbortListener) {
res.on('close', this.removeAbortListener)
Expand Down
33 changes: 33 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@ const { promisify } = require('node:util')
const { NotSupportedError, InvalidArgumentError } = require('../lib/core/errors')
const { parseFormDataString } = require('./utils/formdata')

test('request dump head', async (t) => {
t = tspl(t, { plan: 3 })

const server = createServer((req, res) => {
res.setHeader('content-length', 5 * 100)
res.flushHeaders()
res.write('hello'.repeat(100))
})
after(() => server.close())

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.destroy())

let dumped = false
client.on('disconnect', () => {
t.strictEqual(dumped, true)
})
client.request({
path: '/',
method: 'HEAD'
}, (err, { body }) => {
t.ifError(err)
body.dump({ limit: 1 }).then(() => {
dumped = true
t.ok(true, 'pass')
})
})
})

await t.completed
})

test('request dump big', async (t) => {
t = tspl(t, { plan: 3 })

Expand Down

0 comments on commit 077531a

Please sign in to comment.