1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/test/xhr2_worker.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,185 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +onmessage = function(event) { 1.10 + const url = event.data; 1.11 + 1.12 + var xhr = new XMLHttpRequest(); 1.13 + xhr.open("GET", url, false); 1.14 + xhr.send(); 1.15 + 1.16 + const refText = xhr.responseText; 1.17 + 1.18 + function getResponse(type) { 1.19 + var xhr = new XMLHttpRequest(); 1.20 + xhr.open("GET", url, false); 1.21 + if (type !== undefined) { 1.22 + xhr.responseType = type; 1.23 + } 1.24 + xhr.send(); 1.25 + return xhr.response; 1.26 + } 1.27 + 1.28 + if (getResponse() != refText) { 1.29 + throw new Error("unset responseType failed"); 1.30 + } 1.31 + 1.32 + if (getResponse("") != refText) { 1.33 + throw new Error("'' responseType failed"); 1.34 + } 1.35 + 1.36 + if (getResponse("text") != refText) { 1.37 + throw new Error("'text' responseType failed"); 1.38 + } 1.39 + 1.40 + var array = new Uint8Array(getResponse("arraybuffer")); 1.41 + if (String.fromCharCode.apply(String, array) != refText) { 1.42 + throw new Error("'arraybuffer' responseType failed"); 1.43 + } 1.44 + 1.45 + var blob = getResponse("blob"); 1.46 + if (new FileReaderSync().readAsText(blob) != refText) { 1.47 + throw new Error("'blob' responseType failed"); 1.48 + } 1.49 + 1.50 + // Make sure that we get invalid state exceptions when getting the wrong 1.51 + // property. 1.52 + 1.53 + function testResponseTextException(type) { 1.54 + var xhr = new XMLHttpRequest(); 1.55 + xhr.open("GET", url, false); 1.56 + xhr.responseType = type; 1.57 + xhr.send(); 1.58 + 1.59 + var exception; 1.60 + 1.61 + try { 1.62 + xhr.responseText; 1.63 + } 1.64 + catch(e) { 1.65 + exception = e; 1.66 + } 1.67 + 1.68 + if (!exception) { 1.69 + throw new Error("Failed to throw when getting responseText on '" + type + 1.70 + "' type"); 1.71 + } 1.72 + 1.73 + if (exception.name != "InvalidStateError") { 1.74 + throw new Error("Unexpected error when getting responseText on '" + type + 1.75 + "' type"); 1.76 + } 1.77 + 1.78 + if (exception.code != DOMException.INVALID_STATE_ERR) { 1.79 + throw new Error("Unexpected error code when getting responseText on '" + type + 1.80 + "' type"); 1.81 + } 1.82 + } 1.83 + 1.84 + testResponseTextException("arraybuffer"); 1.85 + testResponseTextException("blob"); 1.86 + 1.87 + // Make sure "document" works, but returns text. 1.88 + xhr = new XMLHttpRequest(); 1.89 + 1.90 + if (xhr.responseType != "text") { 1.91 + throw new Error("Default value for responseType is wrong!"); 1.92 + } 1.93 + 1.94 + xhr.open("GET", url, false); 1.95 + xhr.responseType = "document"; 1.96 + xhr.send(); 1.97 + 1.98 + if (xhr.responseText != refText) { 1.99 + throw new Error("'document' type not working correctly"); 1.100 + } 1.101 + 1.102 + // Make sure setting responseType before open or after send fails. 1.103 + var exception; 1.104 + 1.105 + xhr = new XMLHttpRequest(); 1.106 + try { 1.107 + xhr.responseType = "arraybuffer"; 1.108 + } 1.109 + catch(e) { 1.110 + exception = e; 1.111 + } 1.112 + 1.113 + if (!exception) { 1.114 + throw new Error("Failed to throw when setting responseType before " + 1.115 + "calling open()"); 1.116 + } 1.117 + 1.118 + if (exception.name != "InvalidStateError") { 1.119 + throw new Error("Unexpected error when setting responseType before " + 1.120 + "calling open()"); 1.121 + } 1.122 + 1.123 + if (exception.code != DOMException.INVALID_STATE_ERR) { 1.124 + throw new Error("Unexpected error code when setting responseType before " + 1.125 + "calling open()"); 1.126 + } 1.127 + 1.128 + xhr.open("GET", url); 1.129 + xhr.responseType = "text"; 1.130 + xhr.onload = function(event) { 1.131 + if (event.target.response != refText) { 1.132 + throw new Error("Bad response!"); 1.133 + } 1.134 + 1.135 + xhr = new XMLHttpRequest(); 1.136 + xhr.open("GET", url); 1.137 + xhr.responseType = "moz-chunked-text"; 1.138 + 1.139 + var lastIndex = 0; 1.140 + xhr.onprogress = function(event) { 1.141 + if (refText.substr(lastIndex, xhr.response.length) != xhr.response) { 1.142 + throw new Error("Bad chunk!"); 1.143 + } 1.144 + 1.145 + lastIndex += xhr.response.length; 1.146 + }; 1.147 + 1.148 + xhr.onload = function(event) { 1.149 + if (lastIndex != refText.length) { 1.150 + throw new Error("Didn't see all the data!"); 1.151 + } 1.152 + 1.153 + setTimeout(function() { 1.154 + if (xhr.response !== null) { 1.155 + throw new Error("Should have gotten null response outside of event!"); 1.156 + } 1.157 + postMessage("done"); 1.158 + }, 0); 1.159 + } 1.160 + 1.161 + xhr.send(null); 1.162 + }; 1.163 + xhr.send(); 1.164 + 1.165 + exception = null; 1.166 + 1.167 + try { 1.168 + xhr.responseType = "arraybuffer"; 1.169 + } 1.170 + catch(e) { 1.171 + exception = e; 1.172 + } 1.173 + 1.174 + if (!exception) { 1.175 + throw new Error("Failed to throw when setting responseType after " + 1.176 + "calling send()"); 1.177 + } 1.178 + 1.179 + if (exception.name != "InvalidStateError") { 1.180 + throw new Error("Unexpected error when setting responseType after " + 1.181 + "calling send()"); 1.182 + } 1.183 + 1.184 + if (exception.code != DOMException.INVALID_STATE_ERR) { 1.185 + throw new Error("Unexpected error code when setting responseType after " + 1.186 + "calling send()"); 1.187 + } 1.188 +}