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