Skip to content

Commit

Permalink
Response.bodyUsed should be false when there is no body
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrotoff committed Dec 2, 2022
1 parent 55a4870 commit a68cfda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/body.js
Expand Up @@ -196,8 +196,6 @@ async function consumeBody(data) {
throw new TypeError(`body used already for: ${data.url}`);
}

data[INTERNALS].disturbed = true;

if (data[INTERNALS].error) {
throw data[INTERNALS].error;
}
Expand All @@ -209,6 +207,8 @@ async function consumeBody(data) {
return Buffer.alloc(0);
}

data[INTERNALS].disturbed = true;

/* c8 ignore next 3 */
if (!(body instanceof Stream)) {
return Buffer.alloc(0);
Expand Down
20 changes: 20 additions & 0 deletions test/response.js
Expand Up @@ -264,4 +264,24 @@ describe('Response', () => {

new Response('a').data;
}));

it('should set bodyUsed to true if body has been read', async () => {
for (const toCheck of ['a', '']) {
const res = new Response(toCheck);
expect(res.bodyUsed).to.equal(false);

expect(await res.text()).to.equal(toCheck);
expect(res.bodyUsed).to.equal(true);
}
});

it('should not change bodyUsed if nothing to read', async () => {
for (const toCheck of [undefined, null]) {
const res = new Response(toCheck);
expect(res.bodyUsed).to.equal(false);

expect(await res.text()).to.equal('');
expect(res.bodyUsed).to.equal(false);
}
});
});

0 comments on commit a68cfda

Please sign in to comment.