michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: onmessage = function(event) { michael@0: const url = event.data; michael@0: michael@0: var xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", url, false); michael@0: xhr.send(); michael@0: michael@0: const refText = xhr.responseText; michael@0: michael@0: function getResponse(type) { michael@0: var xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", url, false); michael@0: if (type !== undefined) { michael@0: xhr.responseType = type; michael@0: } michael@0: xhr.send(); michael@0: return xhr.response; michael@0: } michael@0: michael@0: if (getResponse() != refText) { michael@0: throw new Error("unset responseType failed"); michael@0: } michael@0: michael@0: if (getResponse("") != refText) { michael@0: throw new Error("'' responseType failed"); michael@0: } michael@0: michael@0: if (getResponse("text") != refText) { michael@0: throw new Error("'text' responseType failed"); michael@0: } michael@0: michael@0: var array = new Uint8Array(getResponse("arraybuffer")); michael@0: if (String.fromCharCode.apply(String, array) != refText) { michael@0: throw new Error("'arraybuffer' responseType failed"); michael@0: } michael@0: michael@0: var blob = getResponse("blob"); michael@0: if (new FileReaderSync().readAsText(blob) != refText) { michael@0: throw new Error("'blob' responseType failed"); michael@0: } michael@0: michael@0: // Make sure that we get invalid state exceptions when getting the wrong michael@0: // property. michael@0: michael@0: function testResponseTextException(type) { michael@0: var xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", url, false); michael@0: xhr.responseType = type; michael@0: xhr.send(); michael@0: michael@0: var exception; michael@0: michael@0: try { michael@0: xhr.responseText; michael@0: } michael@0: catch(e) { michael@0: exception = e; michael@0: } michael@0: michael@0: if (!exception) { michael@0: throw new Error("Failed to throw when getting responseText on '" + type + michael@0: "' type"); michael@0: } michael@0: michael@0: if (exception.name != "InvalidStateError") { michael@0: throw new Error("Unexpected error when getting responseText on '" + type + michael@0: "' type"); michael@0: } michael@0: michael@0: if (exception.code != DOMException.INVALID_STATE_ERR) { michael@0: throw new Error("Unexpected error code when getting responseText on '" + type + michael@0: "' type"); michael@0: } michael@0: } michael@0: michael@0: testResponseTextException("arraybuffer"); michael@0: testResponseTextException("blob"); michael@0: michael@0: // Make sure "document" works, but returns text. michael@0: xhr = new XMLHttpRequest(); michael@0: michael@0: if (xhr.responseType != "text") { michael@0: throw new Error("Default value for responseType is wrong!"); michael@0: } michael@0: michael@0: xhr.open("GET", url, false); michael@0: xhr.responseType = "document"; michael@0: xhr.send(); michael@0: michael@0: if (xhr.responseText != refText) { michael@0: throw new Error("'document' type not working correctly"); michael@0: } michael@0: michael@0: // Make sure setting responseType before open or after send fails. michael@0: var exception; michael@0: michael@0: xhr = new XMLHttpRequest(); michael@0: try { michael@0: xhr.responseType = "arraybuffer"; michael@0: } michael@0: catch(e) { michael@0: exception = e; michael@0: } michael@0: michael@0: if (!exception) { michael@0: throw new Error("Failed to throw when setting responseType before " + michael@0: "calling open()"); michael@0: } michael@0: michael@0: if (exception.name != "InvalidStateError") { michael@0: throw new Error("Unexpected error when setting responseType before " + michael@0: "calling open()"); michael@0: } michael@0: michael@0: if (exception.code != DOMException.INVALID_STATE_ERR) { michael@0: throw new Error("Unexpected error code when setting responseType before " + michael@0: "calling open()"); michael@0: } michael@0: michael@0: xhr.open("GET", url); michael@0: xhr.responseType = "text"; michael@0: xhr.onload = function(event) { michael@0: if (event.target.response != refText) { michael@0: throw new Error("Bad response!"); michael@0: } michael@0: michael@0: xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", url); michael@0: xhr.responseType = "moz-chunked-text"; michael@0: michael@0: var lastIndex = 0; michael@0: xhr.onprogress = function(event) { michael@0: if (refText.substr(lastIndex, xhr.response.length) != xhr.response) { michael@0: throw new Error("Bad chunk!"); michael@0: } michael@0: michael@0: lastIndex += xhr.response.length; michael@0: }; michael@0: michael@0: xhr.onload = function(event) { michael@0: if (lastIndex != refText.length) { michael@0: throw new Error("Didn't see all the data!"); michael@0: } michael@0: michael@0: setTimeout(function() { michael@0: if (xhr.response !== null) { michael@0: throw new Error("Should have gotten null response outside of event!"); michael@0: } michael@0: postMessage("done"); michael@0: }, 0); michael@0: } michael@0: michael@0: xhr.send(null); michael@0: }; michael@0: xhr.send(); michael@0: michael@0: exception = null; michael@0: michael@0: try { michael@0: xhr.responseType = "arraybuffer"; michael@0: } michael@0: catch(e) { michael@0: exception = e; michael@0: } michael@0: michael@0: if (!exception) { michael@0: throw new Error("Failed to throw when setting responseType after " + michael@0: "calling send()"); michael@0: } michael@0: michael@0: if (exception.name != "InvalidStateError") { michael@0: throw new Error("Unexpected error when setting responseType after " + michael@0: "calling send()"); michael@0: } michael@0: michael@0: if (exception.code != DOMException.INVALID_STATE_ERR) { michael@0: throw new Error("Unexpected error code when setting responseType after " + michael@0: "calling send()"); michael@0: } michael@0: }