Bug 1303025 - Accept null for body param in constructor of Response. r=bkelly

MozReview-Commit-ID: LeEFcQzPJlv

--HG--
extra : rebase_source : 37980f7601143692989d17a2e715f0c5284730a4
This commit is contained in:
Xidorn Quan 2018-01-13 14:20:50 +11:00
parent ddaf0ef84e
commit 2bf366e30a
4 changed files with 14 additions and 9 deletions

View File

@ -140,7 +140,7 @@ Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
return nullptr;
}
Optional<fetch::ResponseBodyInit> body;
Optional<Nullable<fetch::ResponseBodyInit>> body;
ResponseInit init;
init.mStatus = aStatus;
RefPtr<Response> r = Response::Constructor(aGlobal, body, init, aRv);
@ -161,7 +161,7 @@ Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
/*static*/ already_AddRefed<Response>
Response::Constructor(const GlobalObject& aGlobal,
const Optional<fetch::ResponseBodyInit>& aBody,
const Optional<Nullable<fetch::ResponseBodyInit>>& aBody,
const ResponseInit& aInit, ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
@ -227,7 +227,7 @@ Response::Constructor(const GlobalObject& aGlobal,
}
}
if (aBody.WasPassed()) {
if (aBody.WasPassed() && !aBody.Value().IsNull()) {
if (aInit.mStatus == 204 || aInit.mStatus == 205 || aInit.mStatus == 304) {
aRv.ThrowTypeError<MSG_RESPONSE_NULL_STATUS_WITH_BODY>();
return nullptr;
@ -237,9 +237,9 @@ Response::Constructor(const GlobalObject& aGlobal,
nsCOMPtr<nsIInputStream> bodyStream;
int64_t bodySize = InternalResponse::UNKNOWN_BODY_SIZE;
if (aBody.Value().IsReadableStream()) {
const ReadableStream& readableStream =
aBody.Value().GetAsReadableStream();
const fetch::ResponseBodyInit& body = aBody.Value().Value();
if (body.IsReadableStream()) {
const ReadableStream& readableStream = body.GetAsReadableStream();
JS::Rooted<JSObject*> readableStreamObj(aGlobal.Context(),
readableStream.Obj());
@ -288,7 +288,7 @@ Response::Constructor(const GlobalObject& aGlobal,
}
} else {
uint64_t size = 0;
aRv = ExtractByteStreamFromBody(aBody.Value(),
aRv = ExtractByteStreamFromBody(body,
getter_AddRefs(bodyStream),
contentTypeWithCharset,
size);

View File

@ -120,7 +120,7 @@ public:
static already_AddRefed<Response>
Constructor(const GlobalObject& aGlobal,
const Optional<fetch::ResponseBodyInit>& aBody,
const Optional<Nullable<fetch::ResponseBodyInit>>& aBody,
const ResponseInit& aInit, ErrorResult& rv);
nsIGlobalObject* GetParentObject() const

View File

@ -9,7 +9,7 @@
// This should be Constructor(optional BodyInit... but BodyInit doesn't include
// ReadableStream yet because we don't want to expose Streams API to Request.
[Constructor(optional (Blob or BufferSource or FormData or URLSearchParams or ReadableStream or USVString) body, optional ResponseInit init),
[Constructor(optional (Blob or BufferSource or FormData or URLSearchParams or ReadableStream or USVString)? body, optional ResponseInit init),
Exposed=(Window,Worker)]
interface Response {
[NewObject] static Response error();

View File

@ -65,6 +65,11 @@
});
}, "Testing empty Response Content-Type header");
test(function() {
var response = new Response(null, {status: 204});
assert_equals(response.body, null);
}, "Testing null Response body");
</script>
</body>
</html>