Bug 1250987 - Make RequestInit.body nullable; r=bzbarsky

This commit is contained in:
Ehsan Akhgari 2016-02-24 11:34:33 -05:00
parent 92a85ec88d
commit 12746db443
3 changed files with 45 additions and 29 deletions

View File

@ -370,7 +370,8 @@ Request::Constructor(const GlobalObject& aGlobal,
return nullptr;
}
if (aInit.mBody.WasPassed() || temporaryBody) {
if ((aInit.mBody.WasPassed() && !aInit.mBody.Value().IsNull()) ||
temporaryBody) {
// HEAD and GET are not allowed to have a body.
nsAutoCString method;
request->GetMethod(method);
@ -382,7 +383,11 @@ Request::Constructor(const GlobalObject& aGlobal,
}
if (aInit.mBody.WasPassed()) {
const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& bodyInit = aInit.mBody.Value();
const Nullable<OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>& bodyInitNullable =
aInit.mBody.Value();
if (!bodyInitNullable.IsNull()) {
const OwningArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams& bodyInit =
bodyInitNullable.Value();
nsCOMPtr<nsIInputStream> stream;
nsAutoCString contentType;
aRv = ExtractByteStreamFromBody(bodyInit,
@ -406,6 +411,7 @@ Request::Constructor(const GlobalObject& aGlobal,
request->ClearCreatedByFetchEvent();
request->SetBody(temporaryBody);
}
}
RefPtr<Request> domRequest = new Request(global, request);
domRequest->SetMimeType();

View File

@ -38,7 +38,7 @@ Request implements Body;
dictionary RequestInit {
ByteString method;
HeadersInit headers;
BodyInit body;
BodyInit? body;
RequestMode mode;
RequestCredentials credentials;
RequestCache cache;

View File

@ -23,15 +23,23 @@
}
}, "Initialize Request with headers values");
function makeRequestInit(body, method) {
return {"method": method, "body": body};
}
function checkRequestInit(body, bodyType, expectedTextBody) {
promise_test(function(test) {
var request = new Request("", {"method": "POST", "body": body});
var request = new Request("", makeRequestInit(body, "POST"));
if (body) {
assert_throws(new TypeError(),
function() { new Request("", {"method": "GET", "body": body}); }
function() { new Request("", makeRequestInit(body, "GET")); }
);
} else {
new Request("", makeRequestInit(body, "GET")); // should not throw
}
var reqHeaders = request.headers;
var mime = reqHeaders.get("Content-Type");
assert_true(mime && mime.search(bodyType) > -1, "Content-Type header should be \"" + bodyType + "\", not \"" + mime + "\"");
assert_true(!body || (mime && mime.search(bodyType) > -1), "Content-Type header should be \"" + bodyType + "\", not \"" + mime + "\"");
return request.text().then(function(bodyAsText) {
//not equals: cannot guess formData exact value
assert_true( bodyAsText.search(expectedTextBody) > -1, "Retrieve and verify request body");
@ -44,6 +52,8 @@
formaData.append("name", "value");
var usvString = "This is a USVString"
checkRequestInit(undefined, undefined, "");
checkRequestInit(null, null, "");
checkRequestInit(blob, "application/octet-binary", "This is a blob");
checkRequestInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nvalue");
checkRequestInit(usvString, "text/plain;charset=UTF-8", "This is a USVString");