Skip to content

Commit

Permalink
fix: ignore content-length when dumping HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed May 9, 2024
1 parent 9f26aff commit 2ea4ea6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
12 changes: 10 additions & 2 deletions 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 @@ -110,8 +111,15 @@ class RequestHandler extends AsyncResource {

const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const contentType = parsedHeaders['content-type']
const contentLength = parsedHeaders['content-length']
const body = new Readable({ resume, abort, contentType, contentLength, highWaterMark })
const body = new Readable({
resume,
abort,
contentType,
contentLength: this.method !== 'HEAD' && parsedHeaders['content-length']
? Number(parsedHeaders['content-length'])
: null,
highWaterMark
})

if (this.removeAbortListener) {
// TODO (fix): 'close' is sufficient but breaks tests.
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 2ea4ea6

Please sign in to comment.