dom/workers/test/xhr2_worker.js

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

mercurial