Skip to content

Commit

Permalink
fix: request body.body regression
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 13, 2021
1 parent d4f6a2f commit 5a8a8a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError } = require('../core/errors')
const util = require('../core/util')
const { toWebReadable } = require('../fetch/util')

let Blob

Expand Down Expand Up @@ -130,7 +131,7 @@ module.exports = class BodyReadable extends Readable {
// https://fetch.spec.whatwg.org/#dom-body-body
get body () {
if (!this[kBody]) {
this[kBody] = util.toWeb(this)
this[kBody] = toWebReadable(this)
if (this[kConsume]) {
// TODO: Is this the best way to force a lock?
this[kBody].getReader() // Ensure stream is locked.
Expand Down
26 changes: 26 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,29 @@ test('request arrayBuffer', (t) => {
t.strictSame(Buffer.from(JSON.stringify(obj)), Buffer.from(await body.arrayBuffer()))
})
})

test('request body', (t) => {
t.plan(1)

const obj = { asd: true }
const server = createServer((req, res) => {
res.end(JSON.stringify(obj))
})
t.teardown(server.close.bind(server))

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

const { body } = await client.request({
path: '/',
method: 'GET'
})

let x = ''
for await (const chunk of body.body) {
x += chunk
}
t.strictSame(JSON.stringify(obj), x)
})
})

0 comments on commit 5a8a8a5

Please sign in to comment.