Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.bodyUsed should be false when there is no body #1686

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if disturbed = true should be done after this line

return Buffer.alloc(0);
Expand Down
22 changes: 22 additions & 0 deletions test/response.js
Expand Up @@ -264,4 +264,26 @@ 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);

/* eslint-disable-next-line no-await-in-loop */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this lint rule matters in context of unit test

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);

/* eslint-disable-next-line no-await-in-loop */
expect(await res.text()).to.equal('');
expect(res.bodyUsed).to.equal(false);
}
});
});