dom/workers/test/xhrAbort_worker.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 function runTest() {
michael@0 6 var xhr = new XMLHttpRequest();
michael@0 7
michael@0 8 var events = [];
michael@0 9 function pushEvent(event) {
michael@0 10 var readyState, responseText, status, statusText;
michael@0 11
michael@0 12 try {
michael@0 13 readyState = xhr.readyState;
michael@0 14 }
michael@0 15 catch (e) {
michael@0 16 readyState = "[exception]";
michael@0 17 }
michael@0 18
michael@0 19 try {
michael@0 20 responseText = xhr.responseText;
michael@0 21 }
michael@0 22 catch (e) {
michael@0 23 responseText = "[exception]";
michael@0 24 }
michael@0 25
michael@0 26 try {
michael@0 27 status = xhr.status;
michael@0 28 }
michael@0 29 catch (e) {
michael@0 30 status = "[exception]";
michael@0 31 }
michael@0 32
michael@0 33 try {
michael@0 34 statusText = xhr.statusText;
michael@0 35 }
michael@0 36 catch (e) {
michael@0 37 statusText = "[exception]";
michael@0 38 }
michael@0 39
michael@0 40 var str = event.type + "(" + readyState + ", '" + responseText + "', " +
michael@0 41 status + ", '" + statusText + "'";
michael@0 42 if ((("ProgressEvent" in this) && event instanceof ProgressEvent) ||
michael@0 43 (("WorkerProgressEvent" in this) && event instanceof WorkerProgressEvent)) {
michael@0 44 str += ", progressEvent";
michael@0 45 }
michael@0 46 str += ")";
michael@0 47
michael@0 48 events.push(str);
michael@0 49 }
michael@0 50
michael@0 51 xhr.onerror = function(event) {
michael@0 52 throw new Error("Error: " + xhr.statusText);
michael@0 53 }
michael@0 54
michael@0 55 xhr.onload = function(event) {
michael@0 56 throw new Error("Shouldn't have gotten load event!");
michael@0 57 };
michael@0 58
michael@0 59 var seenAbort;
michael@0 60 xhr.onabort = function(event) {
michael@0 61 if (seenAbort) {
michael@0 62 throw new Error("Already seen the abort event!");
michael@0 63 }
michael@0 64 seenAbort = true;
michael@0 65
michael@0 66 pushEvent(event);
michael@0 67 postMessage(events);
michael@0 68 };
michael@0 69
michael@0 70 xhr.onreadystatechange = function(event) {
michael@0 71 pushEvent(event);
michael@0 72 if (xhr.readyState == xhr.HEADERS_RECEIVED) {
michael@0 73 xhr.abort();
michael@0 74 }
michael@0 75 };
michael@0 76
michael@0 77 xhr.open("GET", "testXHR.txt");
michael@0 78 xhr.overrideMimeType("text/plain");
michael@0 79 xhr.send(null);
michael@0 80 }
michael@0 81
michael@0 82 function messageListener(event) {
michael@0 83 switch (event.data) {
michael@0 84 case "start":
michael@0 85 runTest();
michael@0 86 break;
michael@0 87 default:
michael@0 88 throw new Error("Bad message!");
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 addEventListener("message", messageListener, false);

mercurial