Bug 707484 - Allow setting XHR.responseType before open(); r=smaug

This commit is contained in:
Tillmann Karras 2015-01-13 09:51:09 +01:00
parent 89a83cfb14
commit 7b41263a57
7 changed files with 25 additions and 67 deletions

View File

@ -288,7 +288,8 @@ nsXMLHttpRequest::sDontWarnAboutSyncXHR = false;
nsXMLHttpRequest::nsXMLHttpRequest()
: mResponseBodyDecodedPos(0),
mResponseType(XML_HTTP_RESPONSE_TYPE_DEFAULT),
mRequestObserver(nullptr), mState(XML_HTTP_REQUEST_UNSENT),
mRequestObserver(nullptr),
mState(XML_HTTP_REQUEST_UNSENT | XML_HTTP_REQUEST_ASYNC),
mUploadTransferred(0), mUploadTotal(0), mUploadComplete(true),
mProgressSinceLastProgressEvent(false),
mRequestSentTime(0), mTimeoutMilliseconds(0),
@ -915,10 +916,9 @@ void
nsXMLHttpRequest::SetResponseType(nsXMLHttpRequest::ResponseTypeEnum aResponseType,
ErrorResult& aRv)
{
// If the state is not OPENED or HEADERS_RECEIVED raise an
// INVALID_STATE_ERR exception and terminate these steps.
if (!(mState & (XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT |
XML_HTTP_REQUEST_HEADERS_RECEIVED))) {
// If the state is LOADING or DONE raise an INVALID_STATE_ERR exception
// and terminate these steps.
if ((mState & (XML_HTTP_REQUEST_LOADING | XML_HTTP_REQUEST_DONE))) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}

View File

@ -104,6 +104,12 @@ function checkResponseXMLAccessThrows(xhr) {
try { xhr.responseXML } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when accessing responseXML");
}
function checkSetResponseType(xhr, type) {
var didthrow = false;
try { xhr.responseType = type; } catch (e) { didthrow = true; }
ise(xhr.responseType, type, "responseType should be " + type);
ok(!didthrow, "should not have thrown when setting responseType");
}
function checkSetResponseTypeThrows(xhr, type) {
var didthrow = false;
try { xhr.responseType = type; } catch (e) { didthrow = true; }
@ -115,6 +121,18 @@ function checkOpenThrows(xhr, method, url, async) {
ok(didthrow, "should have thrown when open is called");
}
// test if setting responseType before calling open() works
xhr = new XMLHttpRequest();
checkSetResponseType(xhr, "");
checkSetResponseType(xhr, "text");
checkSetResponseType(xhr, "document");
checkSetResponseType(xhr, "arraybuffer");
checkSetResponseType(xhr, "blob");
checkSetResponseType(xhr, "json");
checkSetResponseType(xhr, "moz-chunked-text");
checkSetResponseType(xhr, "moz-chunked-arraybuffer");
checkOpenThrows(xhr, "GET", "file_XHR_pass2.txt", false);
// test response (sync, responseType is not changeable)
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);

View File

@ -29,7 +29,7 @@ var expected = [
{ name: "", message: "uncaught exception: [object Object]" },
{ name: "", message: "foo", filename: "baz", lineNumber: 123 },
{ name: "", message: "uncaught exception: [object Object]" },
{ name: "InvalidStateError", message: "An attempt was made to use an object that is not, or is no longer, usable", filename: location, lineNumber: 60 },
{ name: "InvalidStateError", message: "An attempt was made to use an object that is not, or is no longer, usable", filename: location, lineNumber: 62 },
{ name: "ReferenceError", message: "xxx is not defined", filename: location, lineNumber: 64 },
{ name: "ReferenceError", message: "xxx is not defined", filename: location, lineNumber: 66 }
];
@ -57,9 +57,9 @@ window.onmessage = function(e) {
throw {name:3,message:4,lineNumber:123};
} else if (e.data == 7) {
var x = new XMLHttpRequest();
x.responseType = "arraybuffer";
x.open("GET", location, false);
var a = x.send();
x.responseType = "arraybuffer";
} else if (e.data == 8) {
throw new ReferenceError("xxx is not defined");
} else if (e.data == 9) {

View File

@ -1,17 +0,0 @@
[open-method-responsetype-set-sync.htm]
type: testharness
[XMLHttpRequest: open() sync request not allowed if responseType is set (arraybuffer)]
expected: FAIL
[XMLHttpRequest: open() sync request not allowed if responseType is set (blob)]
expected: FAIL
[XMLHttpRequest: open() sync request not allowed if responseType is set (json)]
expected: FAIL
[XMLHttpRequest: open() sync request not allowed if responseType is set (text)]
expected: FAIL
[XMLHttpRequest: open() sync request not allowed if responseType is set (document)]
expected: FAIL

View File

@ -12,9 +12,3 @@
[XMLHttpRequest: responseText decoding (text/plain %C2)]
expected: FAIL
[XMLHttpRequest: responseText decoding (text/plain %E3%81%B2)]
expected: FAIL
[XMLHttpRequest: responseText decoding (text/xml %3C%3Fxml%20version%3D\'1.0\'%20encoding%3D\'windows-1252\'%3F%3E%3Cx%3E%E3%81%B2%3C%2Fx%3E)]
expected: FAIL

View File

@ -1,20 +0,0 @@
[responsetype.html]
type: testharness
[Set responseType to "" when readyState is UNSENT.]
expected: FAIL
[Set responseType to "json" when readyState is UNSENT.]
expected: FAIL
[Set responseType to "document" when readyState is UNSENT.]
expected: FAIL
[Set responseType to "arraybuffer" when readyState is UNSENT.]
expected: FAIL
[Set responseType to "blob" when readyState is UNSENT.]
expected: FAIL
[Set responseType to "text" when readyState is UNSENT.]
expected: FAIL

View File

@ -1,17 +0,0 @@
[responsexml-non-document-types.htm]
type: testharness
[XMLHttpRequest: responseXML/responseText on other responseType (arraybuffer)]
expected: FAIL
[XMLHttpRequest: responseXML/responseText on other responseType (blob)]
expected: FAIL
[XMLHttpRequest: responseXML/responseText on other responseType (json)]
expected: FAIL
[XMLHttpRequest: responseXML/responseText on other responseType (text)]
expected: FAIL
[XMLHttpRequest: responseXML/responseText on other responseType (document)]
expected: FAIL