diff --git a/components/script/dom/response.rs b/components/script/dom/response.rs index ad5872fa749..c01cd7e5c48 100644 --- a/components/script/dom/response.rs +++ b/components/script/dom/response.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use std::cell::Cell; use std::rc::Rc; use std::str::FromStr; @@ -48,6 +49,7 @@ pub(crate) struct Response { #[ignore_malloc_size_of = "StreamConsumer"] stream_consumer: DomRefCell>, redirected: DomRefCell, + is_body_empty: Cell, } #[allow(non_snake_case)] @@ -69,6 +71,7 @@ impl Response { body_stream: MutNullableDom::new(Some(&*stream)), stream_consumer: DomRefCell::new(None), redirected: DomRefCell::new(false), + is_body_empty: Cell::new(true), } } @@ -153,6 +156,9 @@ impl ResponseMethods for Response { // 1. Set this’s response to a new response. // Our Response/Body types don't actually hold onto an internal fetch Response. let response = Response::new_with_proto(global, proto, can_gc); + if body_init.is_some() { + response.is_body_empty.set(false); + } // 2. Set this’s headers to a new Headers object with this’s relevant realm, // whose header list is this’s response’s header list and guard is "response". @@ -324,6 +330,7 @@ impl ResponseMethods for Response { if let Some(stream) = self.body_stream.get().clone() { new_response.body_stream.set(Some(&*stream)); } + new_response.is_body_empty.set(self.is_body_empty.get()); // Step 3 // TODO: This step relies on promises, which are still unimplemented. @@ -334,7 +341,8 @@ impl ResponseMethods for Response { /// fn BodyUsed(&self) -> bool { - self.is_disturbed() + // bodyUsed returns true only if body is non-null + !self.is_body_empty() && self.is_disturbed() } /// @@ -421,6 +429,7 @@ fn initialize_response( // 6.2 Set response’s body to body’s body. response.body_stream.set(Some(&*body.stream)); + response.is_body_empty.set(false); // 6.3 If body’s type is non-null and response’s header list does not contain `Content-Type`, // then append (`Content-Type`, body’s type) to response’s header list. @@ -509,6 +518,7 @@ impl Response { } pub(crate) fn stream_chunk(&self, chunk: Vec, can_gc: CanGc) { + self.is_body_empty.set(false); // Note, are these two actually mutually exclusive? if let Some(stream_consumer) = self.stream_consumer.borrow().as_ref() { stream_consumer.consume_chunk(chunk.as_slice()); @@ -527,4 +537,8 @@ impl Response { stream_consumer.stream_end(); } } + + pub(crate) fn is_body_empty(&self) -> bool { + self.is_body_empty.get() + } } diff --git a/tests/wpt/meta/fetch/api/response/response-consume-empty.any.js.ini b/tests/wpt/meta/fetch/api/response/response-consume-empty.any.js.ini index 1e555d38f07..bee1271ebb0 100644 --- a/tests/wpt/meta/fetch/api/response/response-consume-empty.any.js.ini +++ b/tests/wpt/meta/fetch/api/response/response-consume-empty.any.js.ini @@ -5,52 +5,10 @@ expected: ERROR [response-consume-empty.any.worker.html] - [Consume response's body as text] - expected: FAIL - - [Consume response's body as json (error case)] - expected: FAIL - - [Consume response's body as blob] - expected: FAIL - - [Consume response's body as formData without correct type (error case)] - expected: FAIL - - [Consume response's body as arrayBuffer] - expected: FAIL - [Consume empty FormData response body as text] expected: FAIL - [Consume response's body as formData with correct multipart type (error case)] - expected: FAIL - - [Consume response's body as formData with correct urlencoded type] - expected: FAIL - [response-consume-empty.any.html] - [Consume response's body as text] - expected: FAIL - - [Consume response's body as json (error case)] - expected: FAIL - - [Consume response's body as blob] - expected: FAIL - - [Consume response's body as formData without correct type (error case)] - expected: FAIL - - [Consume response's body as arrayBuffer] - expected: FAIL - [Consume empty FormData response body as text] expected: FAIL - - [Consume response's body as formData with correct multipart type (error case)] - expected: FAIL - - [Consume response's body as formData with correct urlencoded type] - expected: FAIL