Skip to content

Commit

Permalink
fix: when no body supplied, do not set bodyUsed to true
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Champion committed Jul 17, 2023
1 parent f796a9f commit b7bb747
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ if (support.iterable) {
}

function consumed(body) {
if (body._noBody) return
if (body.bodyUsed) {
return Promise.reject(new TypeError('Already read'))
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})

Expand Down

0 comments on commit b7bb747

Please sign in to comment.