From 7d92dff12d7c4058b57c7e77adeb0a76ffab639f Mon Sep 17 00:00:00 2001 From: Jake Champion Date: Mon, 17 Jul 2023 23:52:08 +0100 Subject: [PATCH] fix: when no body supplied, do not set bodyUsed to true --- fetch.js | 2 ++ test/test.js | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/fetch.js b/fetch.js index 37b98218..c4162dae 100644 --- a/fetch.js +++ b/fetch.js @@ -162,6 +162,7 @@ if (support.iterable) { } function consumed(body) { + if (body._noBody) return if (body.bodyUsed) { return Promise.reject(new TypeError('Already read')) } @@ -231,6 +232,7 @@ function Body() { this.bodyUsed = this.bodyUsed this._bodyInit = body if (!body) { + this._noBody = true; this._bodyText = '' } else if (typeof body === 'string') { this._bodyText = body diff --git a/test/test.js b/test/test.js index d6431f40..8508610b 100644 --- a/test/test.js +++ b/test/test.js @@ -996,6 +996,15 @@ exercise.forEach(function(exerciseMode) { assert(error instanceof TypeError, 'Promise rejected after body consumed') }) }) + + test('does not set bodyUsed to true if no body supplied', function() { + var response = new Response(); + assert(response.text, 'Body does not implement text') + assert.equal(response.bodyUsed, false) + response.text() + assert.equal(response.bodyUsed, false) + return response.text() + }) }) })