Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 onmessage = function(event) {
7 const url = event.data;
9 var xhr = new XMLHttpRequest();
10 xhr.open("GET", url, false);
11 xhr.send();
13 const refText = xhr.responseText;
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 }
25 if (getResponse() != refText) {
26 throw new Error("unset responseType failed");
27 }
29 if (getResponse("") != refText) {
30 throw new Error("'' responseType failed");
31 }
33 if (getResponse("text") != refText) {
34 throw new Error("'text' responseType failed");
35 }
37 var array = new Uint8Array(getResponse("arraybuffer"));
38 if (String.fromCharCode.apply(String, array) != refText) {
39 throw new Error("'arraybuffer' responseType failed");
40 }
42 var blob = getResponse("blob");
43 if (new FileReaderSync().readAsText(blob) != refText) {
44 throw new Error("'blob' responseType failed");
45 }
47 // Make sure that we get invalid state exceptions when getting the wrong
48 // property.
50 function testResponseTextException(type) {
51 var xhr = new XMLHttpRequest();
52 xhr.open("GET", url, false);
53 xhr.responseType = type;
54 xhr.send();
56 var exception;
58 try {
59 xhr.responseText;
60 }
61 catch(e) {
62 exception = e;
63 }
65 if (!exception) {
66 throw new Error("Failed to throw when getting responseText on '" + type +
67 "' type");
68 }
70 if (exception.name != "InvalidStateError") {
71 throw new Error("Unexpected error when getting responseText on '" + type +
72 "' type");
73 }
75 if (exception.code != DOMException.INVALID_STATE_ERR) {
76 throw new Error("Unexpected error code when getting responseText on '" + type +
77 "' type");
78 }
79 }
81 testResponseTextException("arraybuffer");
82 testResponseTextException("blob");
84 // Make sure "document" works, but returns text.
85 xhr = new XMLHttpRequest();
87 if (xhr.responseType != "text") {
88 throw new Error("Default value for responseType is wrong!");
89 }
91 xhr.open("GET", url, false);
92 xhr.responseType = "document";
93 xhr.send();
95 if (xhr.responseText != refText) {
96 throw new Error("'document' type not working correctly");
97 }
99 // Make sure setting responseType before open or after send fails.
100 var exception;
102 xhr = new XMLHttpRequest();
103 try {
104 xhr.responseType = "arraybuffer";
105 }
106 catch(e) {
107 exception = e;
108 }
110 if (!exception) {
111 throw new Error("Failed to throw when setting responseType before " +
112 "calling open()");
113 }
115 if (exception.name != "InvalidStateError") {
116 throw new Error("Unexpected error when setting responseType before " +
117 "calling open()");
118 }
120 if (exception.code != DOMException.INVALID_STATE_ERR) {
121 throw new Error("Unexpected error code when setting responseType before " +
122 "calling open()");
123 }
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 }
132 xhr = new XMLHttpRequest();
133 xhr.open("GET", url);
134 xhr.responseType = "moz-chunked-text";
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 }
142 lastIndex += xhr.response.length;
143 };
145 xhr.onload = function(event) {
146 if (lastIndex != refText.length) {
147 throw new Error("Didn't see all the data!");
148 }
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 }
158 xhr.send(null);
159 };
160 xhr.send();
162 exception = null;
164 try {
165 xhr.responseType = "arraybuffer";
166 }
167 catch(e) {
168 exception = e;
169 }
171 if (!exception) {
172 throw new Error("Failed to throw when setting responseType after " +
173 "calling send()");
174 }
176 if (exception.name != "InvalidStateError") {
177 throw new Error("Unexpected error when setting responseType after " +
178 "calling send()");
179 }
181 if (exception.code != DOMException.INVALID_STATE_ERR) {
182 throw new Error("Unexpected error code when setting responseType after " +
183 "calling send()");
184 }
185 }