dom/workers/test/xhr2_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /**
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 4 */
michael@0 5
michael@0 6 onmessage = function(event) {
michael@0 7 const url = event.data;
michael@0 8
michael@0 9 var xhr = new XMLHttpRequest();
michael@0 10 xhr.open("GET", url, false);
michael@0 11 xhr.send();
michael@0 12
michael@0 13 const refText = xhr.responseText;
michael@0 14
michael@0 15 function getResponse(type) {
michael@0 16 var xhr = new XMLHttpRequest();
michael@0 17 xhr.open("GET", url, false);
michael@0 18 if (type !== undefined) {
michael@0 19 xhr.responseType = type;
michael@0 20 }
michael@0 21 xhr.send();
michael@0 22 return xhr.response;
michael@0 23 }
michael@0 24
michael@0 25 if (getResponse() != refText) {
michael@0 26 throw new Error("unset responseType failed");
michael@0 27 }
michael@0 28
michael@0 29 if (getResponse("") != refText) {
michael@0 30 throw new Error("'' responseType failed");
michael@0 31 }
michael@0 32
michael@0 33 if (getResponse("text") != refText) {
michael@0 34 throw new Error("'text' responseType failed");
michael@0 35 }
michael@0 36
michael@0 37 var array = new Uint8Array(getResponse("arraybuffer"));
michael@0 38 if (String.fromCharCode.apply(String, array) != refText) {
michael@0 39 throw new Error("'arraybuffer' responseType failed");
michael@0 40 }
michael@0 41
michael@0 42 var blob = getResponse("blob");
michael@0 43 if (new FileReaderSync().readAsText(blob) != refText) {
michael@0 44 throw new Error("'blob' responseType failed");
michael@0 45 }
michael@0 46
michael@0 47 // Make sure that we get invalid state exceptions when getting the wrong
michael@0 48 // property.
michael@0 49
michael@0 50 function testResponseTextException(type) {
michael@0 51 var xhr = new XMLHttpRequest();
michael@0 52 xhr.open("GET", url, false);
michael@0 53 xhr.responseType = type;
michael@0 54 xhr.send();
michael@0 55
michael@0 56 var exception;
michael@0 57
michael@0 58 try {
michael@0 59 xhr.responseText;
michael@0 60 }
michael@0 61 catch(e) {
michael@0 62 exception = e;
michael@0 63 }
michael@0 64
michael@0 65 if (!exception) {
michael@0 66 throw new Error("Failed to throw when getting responseText on '" + type +
michael@0 67 "' type");
michael@0 68 }
michael@0 69
michael@0 70 if (exception.name != "InvalidStateError") {
michael@0 71 throw new Error("Unexpected error when getting responseText on '" + type +
michael@0 72 "' type");
michael@0 73 }
michael@0 74
michael@0 75 if (exception.code != DOMException.INVALID_STATE_ERR) {
michael@0 76 throw new Error("Unexpected error code when getting responseText on '" + type +
michael@0 77 "' type");
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 testResponseTextException("arraybuffer");
michael@0 82 testResponseTextException("blob");
michael@0 83
michael@0 84 // Make sure "document" works, but returns text.
michael@0 85 xhr = new XMLHttpRequest();
michael@0 86
michael@0 87 if (xhr.responseType != "text") {
michael@0 88 throw new Error("Default value for responseType is wrong!");
michael@0 89 }
michael@0 90
michael@0 91 xhr.open("GET", url, false);
michael@0 92 xhr.responseType = "document";
michael@0 93 xhr.send();
michael@0 94
michael@0 95 if (xhr.responseText != refText) {
michael@0 96 throw new Error("'document' type not working correctly");
michael@0 97 }
michael@0 98
michael@0 99 // Make sure setting responseType before open or after send fails.
michael@0 100 var exception;
michael@0 101
michael@0 102 xhr = new XMLHttpRequest();
michael@0 103 try {
michael@0 104 xhr.responseType = "arraybuffer";
michael@0 105 }
michael@0 106 catch(e) {
michael@0 107 exception = e;
michael@0 108 }
michael@0 109
michael@0 110 if (!exception) {
michael@0 111 throw new Error("Failed to throw when setting responseType before " +
michael@0 112 "calling open()");
michael@0 113 }
michael@0 114
michael@0 115 if (exception.name != "InvalidStateError") {
michael@0 116 throw new Error("Unexpected error when setting responseType before " +
michael@0 117 "calling open()");
michael@0 118 }
michael@0 119
michael@0 120 if (exception.code != DOMException.INVALID_STATE_ERR) {
michael@0 121 throw new Error("Unexpected error code when setting responseType before " +
michael@0 122 "calling open()");
michael@0 123 }
michael@0 124
michael@0 125 xhr.open("GET", url);
michael@0 126 xhr.responseType = "text";
michael@0 127 xhr.onload = function(event) {
michael@0 128 if (event.target.response != refText) {
michael@0 129 throw new Error("Bad response!");
michael@0 130 }
michael@0 131
michael@0 132 xhr = new XMLHttpRequest();
michael@0 133 xhr.open("GET", url);
michael@0 134 xhr.responseType = "moz-chunked-text";
michael@0 135
michael@0 136 var lastIndex = 0;
michael@0 137 xhr.onprogress = function(event) {
michael@0 138 if (refText.substr(lastIndex, xhr.response.length) != xhr.response) {
michael@0 139 throw new Error("Bad chunk!");
michael@0 140 }
michael@0 141
michael@0 142 lastIndex += xhr.response.length;
michael@0 143 };
michael@0 144
michael@0 145 xhr.onload = function(event) {
michael@0 146 if (lastIndex != refText.length) {
michael@0 147 throw new Error("Didn't see all the data!");
michael@0 148 }
michael@0 149
michael@0 150 setTimeout(function() {
michael@0 151 if (xhr.response !== null) {
michael@0 152 throw new Error("Should have gotten null response outside of event!");
michael@0 153 }
michael@0 154 postMessage("done");
michael@0 155 }, 0);
michael@0 156 }
michael@0 157
michael@0 158 xhr.send(null);
michael@0 159 };
michael@0 160 xhr.send();
michael@0 161
michael@0 162 exception = null;
michael@0 163
michael@0 164 try {
michael@0 165 xhr.responseType = "arraybuffer";
michael@0 166 }
michael@0 167 catch(e) {
michael@0 168 exception = e;
michael@0 169 }
michael@0 170
michael@0 171 if (!exception) {
michael@0 172 throw new Error("Failed to throw when setting responseType after " +
michael@0 173 "calling send()");
michael@0 174 }
michael@0 175
michael@0 176 if (exception.name != "InvalidStateError") {
michael@0 177 throw new Error("Unexpected error when setting responseType after " +
michael@0 178 "calling send()");
michael@0 179 }
michael@0 180
michael@0 181 if (exception.code != DOMException.INVALID_STATE_ERR) {
michael@0 182 throw new Error("Unexpected error code when setting responseType after " +
michael@0 183 "calling send()");
michael@0 184 }
michael@0 185 }

mercurial