Skip to content

Commit ba95ff6

Browse files
authored
fix: fix request with readable mode is object (#2279)
In the stream Readable.from(buf) state, the length of 1 refers to the number of objects, not the length of buf. Therefore, when the stream mode is set to 'object,' should not use the length from the state.
1 parent a732603 commit ba95ff6

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/core/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function bodyLength (body) {
168168
return 0
169169
} else if (isStream(body)) {
170170
const state = body._readableState
171-
return state && state.ended === true && Number.isFinite(state.length)
171+
return state && state.objectMode === false && state.ended === true && Number.isFinite(state.length)
172172
? state.length
173173
: null
174174
} else if (isBlobLike(body)) {

test/content-length.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,32 @@ test('response invalid content length with close', (t) => {
300300
})
301301
})
302302
})
303+
304+
test('request streaming with Readable.from(buf)', (t) => {
305+
const server = createServer((req, res) => {
306+
req.pipe(res)
307+
})
308+
t.teardown(server.close.bind(server))
309+
server.listen(0, () => {
310+
const client = new Client(`http://localhost:${server.address().port}`)
311+
t.teardown(client.destroy.bind(client))
312+
313+
client.request({
314+
path: '/',
315+
method: 'PUT',
316+
body: Readable.from(Buffer.from('hello'))
317+
}, (err, data) => {
318+
const chunks = []
319+
t.error(err)
320+
data.body
321+
.on('data', (chunk) => {
322+
chunks.push(chunk)
323+
})
324+
.on('end', () => {
325+
t.equal(Buffer.concat(chunks).toString(), 'hello')
326+
t.pass()
327+
t.end()
328+
})
329+
})
330+
})
331+
})

0 commit comments

Comments
 (0)