gecko-dev/dom/xhr/tests/xhr2_worker.js
Boris Zbarsky 1e48d9386f Bug 1478743 part 1. Add some test coverage for setting xhr.responseType on workers. r=baku
Most of the change is just making the responseType wpt run on workers and
annotating the resulting failures.

The change to the initial value of mResponseType is a drive-by fix for an
easy-to-fix issue the test caught.  There is a corresponding mochitest fix to
fix our incorrect test for the behavior.

--HG--
rename : testing/web-platform/tests/xhr/responsetype.html => testing/web-platform/tests/xhr/responsetype.any.js
2018-07-30 11:51:47 -04:00

101 lines
2.4 KiB
JavaScript

/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
onmessage = function(event) {
const url = event.data;
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send();
const refText = xhr.responseText;
function getResponse(type) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
if (type !== undefined) {
xhr.responseType = type;
}
xhr.send();
return xhr.response;
}
if (getResponse() != refText) {
throw new Error("unset responseType failed");
}
if (getResponse("") != refText) {
throw new Error("'' responseType failed");
}
if (getResponse("text") != refText) {
throw new Error("'text' responseType failed");
}
var array = new Uint8Array(getResponse("arraybuffer"));
if (String.fromCharCode.apply(String, array) != refText) {
throw new Error("'arraybuffer' responseType failed");
}
var blob = getResponse("blob");
if (new FileReaderSync().readAsText(blob) != refText) {
throw new Error("'blob' responseType failed");
}
// Make sure that we get invalid state exceptions when getting the wrong
// property.
function testResponseTextException(type) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.responseType = type;
xhr.send();
var exception;
try {
xhr.responseText;
}
catch(e) {
exception = e;
}
if (!exception) {
throw new Error("Failed to throw when getting responseText on '" + type +
"' type");
}
if (exception.name != "InvalidStateError") {
throw new Error("Unexpected error when getting responseText on '" + type +
"' type");
}
if (exception.code != DOMException.INVALID_STATE_ERR) {
throw new Error("Unexpected error code when getting responseText on '" + type +
"' type");
}
}
testResponseTextException("arraybuffer");
testResponseTextException("blob");
// Make sure "document" works, but returns text.
xhr = new XMLHttpRequest();
if (xhr.responseType != "") {
throw new Error("Default value for responseType is wrong!");
}
xhr.open("GET", url, false);
xhr.responseType = "document";
xhr.send();
if (xhr.responseText != refText) {
throw new Error("'document' type not working correctly");
}
postMessage("done");
}