content/base/test/test_XHR.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_XHR.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,361 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for XMLHttpRequest</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.10 +</head>
    1.11 +<body onload="gen.next();">
    1.12 +<p id="display"></p>
    1.13 +<div id="content" style="display: none">
    1.14 +
    1.15 +</div>
    1.16 +<pre id="test">
    1.17 +<script class="testbody" type="application/javascript;version=1.7">
    1.18 +"use strict";
    1.19 +SimpleTest.waitForExplicitFinish();
    1.20 +
    1.21 +var gen = runTests();
    1.22 +function continueTest() { gen.next(); }
    1.23 +
    1.24 +function runTests() {
    1.25 +
    1.26 +var path = "/tests/content/base/test/";
    1.27 +
    1.28 +var passFiles = [['file_XHR_pass1.xml', 'GET', 200, 'text/xml'],
    1.29 +                 ['file_XHR_pass2.txt', 'GET', 200, 'text/plain'],
    1.30 +                 ['file_XHR_pass3.txt', 'GET', 200, 'text/plain'],
    1.31 +                 ['data:text/xml,%3Cres%3Ehello%3C/res%3E%0A', 'GET', 0, 'text/xml'],
    1.32 +                 ['data:text/plain,hello%20pass%0A', 'GET', 0, 'text/plain'],
    1.33 +                 ['data:,foo', 'GET', 0, 'text/plain;charset=US-ASCII', 'foo'],
    1.34 +                 ['data:text/plain;base64,Zm9v', 'GET', 0, 'text/plain', 'foo'],
    1.35 +                 ['data:text/plain,foo#bar', 'GET', 0, 'text/plain', 'foo'],
    1.36 +                 ['data:text/plain,foo%23bar', 'GET', 0, 'text/plain', 'foo#bar'],
    1.37 +                 ];
    1.38 +
    1.39 +var failFiles = [['//example.com' + path + 'file_XHR_pass1.xml', 'GET'],
    1.40 +                 ['ftp://localhost' + path + 'file_XHR_pass1.xml', 'GET'],
    1.41 +                 ['file_XHR_fail1.txt', 'GET'],
    1.42 +                 ];
    1.43 +
    1.44 +for (i = 0; i < passFiles.length; ++i) {
    1.45 +  xhr = new XMLHttpRequest();
    1.46 +  is(xhr.getResponseHeader("Content-Type"), null, "should be null");
    1.47 +  is(xhr.getAllResponseHeaders(), "", "should be empty string");
    1.48 +  is(xhr.responseType, "", "wrong initial responseType");
    1.49 +  xhr.open(passFiles[i][1], passFiles[i][0], false); 
    1.50 +  xhr.send(null);
    1.51 +  is(xhr.status, passFiles[i][2], "wrong status");
    1.52 +  is(xhr.getResponseHeader("Content-Type"), passFiles[i][3], "wrong content type");
    1.53 +  var headers = xhr.getAllResponseHeaders();
    1.54 +  ok(/(?:^|\n)Content-Type:\s*([^\r\n]*)\r\n/i.test(headers) &&
    1.55 +     RegExp.$1 === passFiles[i][3], "wrong response headers");
    1.56 +  if (xhr.responseXML) {
    1.57 +    is((new XMLSerializer()).serializeToString(xhr.responseXML.documentElement),
    1.58 +       passFiles[i][4] || "<res>hello</res>", "wrong responseXML");
    1.59 +    is(xhr.response, passFiles[i][4] || "<res>hello</res>\n", "wrong response");
    1.60 +  }
    1.61 +  else {
    1.62 +    is(xhr.responseText, passFiles[i][4] || "hello pass\n", "wrong responseText");
    1.63 +    is(xhr.response, passFiles[i][4] || "hello pass\n", "wrong response");
    1.64 +  }
    1.65 +}
    1.66 +
    1.67 +for (i = 0; i < failFiles.length; ++i) {
    1.68 +  xhr = new XMLHttpRequest();
    1.69 +  var didthrow = false;
    1.70 +  try {
    1.71 +    xhr.open(failFiles[i][1], failFiles[i][0], false); 
    1.72 +    xhr.send(null);
    1.73 +  }
    1.74 +  catch (e) {
    1.75 +    didthrow = true;
    1.76 +  }
    1.77 +  if (!didthrow) {
    1.78 +    is(xhr.status, 301, "wrong status");
    1.79 +    is(xhr.responseText, "redirect file\n", "wrong response");
    1.80 +  }
    1.81 +  else {
    1.82 +    ok(1, "should have thrown or given incorrect result");
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +function checkResponseTextAccessThrows(xhr) {
    1.87 +  var didthrow = false;
    1.88 +  try { xhr.responseText } catch (e) { didthrow = true; }
    1.89 +  ok(didthrow, "should have thrown when accessing responseText");
    1.90 +}
    1.91 +function checkResponseXMLAccessThrows(xhr) {
    1.92 +  var didthrow = false;
    1.93 +  try { xhr.responseXML } catch (e) { didthrow = true; }
    1.94 +  ok(didthrow, "should have thrown when accessing responseXML");
    1.95 +}
    1.96 +function checkSetResponseTypeThrows(xhr, type) {
    1.97 +  var didthrow = false;
    1.98 +  try { xhr.responseType = type; } catch (e) { didthrow = true; }
    1.99 +  ok(didthrow, "should have thrown when setting responseType");
   1.100 +}
   1.101 +function checkOpenThrows(xhr, method, url, async) {
   1.102 +  var didthrow = false;
   1.103 +  try { xhr.open(method, url, async); } catch (e) { didthrow = true; }
   1.104 +  ok(didthrow, "should have thrown when open is called");
   1.105 +}
   1.106 +
   1.107 +// test response (sync, responseType is not changeable)
   1.108 +xhr = new XMLHttpRequest();
   1.109 +xhr.open("GET", 'file_XHR_pass2.txt', false); 
   1.110 +checkSetResponseTypeThrows(xhr, "");
   1.111 +checkSetResponseTypeThrows(xhr, "text");
   1.112 +checkSetResponseTypeThrows(xhr, "document");
   1.113 +checkSetResponseTypeThrows(xhr, "arraybuffer");
   1.114 +checkSetResponseTypeThrows(xhr, "blob");
   1.115 +checkSetResponseTypeThrows(xhr, "json");
   1.116 +checkSetResponseTypeThrows(xhr, "moz-chunked-text");
   1.117 +checkSetResponseTypeThrows(xhr, "moz-chunked-arraybuffer");
   1.118 +xhr.send(null);
   1.119 +checkSetResponseTypeThrows(xhr, "document");
   1.120 +is(xhr.status, 200, "wrong status");
   1.121 +is(xhr.response, "hello pass\n", "wrong response");
   1.122 +
   1.123 +// test response (responseType='document')
   1.124 +xhr = new XMLHttpRequest();
   1.125 +xhr.open("GET", 'file_XHR_pass1.xml'); 
   1.126 +xhr.responseType = 'document';
   1.127 +xhr.onloadend = continueTest;
   1.128 +xhr.send(null);
   1.129 +yield undefined;
   1.130 +checkSetResponseTypeThrows(xhr, "document");
   1.131 +is(xhr.status, 200, "wrong status");
   1.132 +checkResponseTextAccessThrows(xhr);
   1.133 +is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
   1.134 +   "<res>hello</res>",
   1.135 +   "wrong response");
   1.136 +
   1.137 +// test response (responseType='text')
   1.138 +xhr = new XMLHttpRequest();
   1.139 +xhr.open("GET", 'file_XHR_pass2.txt'); 
   1.140 +xhr.responseType = 'text';
   1.141 +xhr.onloadend = continueTest;
   1.142 +xhr.send(null);
   1.143 +yield undefined;
   1.144 +is(xhr.status, 200, "wrong status");
   1.145 +checkResponseXMLAccessThrows(xhr);
   1.146 +is(xhr.response, "hello pass\n", "wrong response");
   1.147 +
   1.148 +// test response (responseType='arraybuffer')
   1.149 +function arraybuffer_equals_to(ab, s) {
   1.150 +  is(ab.byteLength, s.length, "wrong arraybuffer byteLength");
   1.151 +
   1.152 +  var u8v = new Uint8Array(ab);
   1.153 +  is(String.fromCharCode.apply(String, u8v), s, "wrong values");
   1.154 +}
   1.155 +
   1.156 +// with a simple text file
   1.157 +xhr = new XMLHttpRequest();
   1.158 +xhr.open("GET", 'file_XHR_pass2.txt'); 
   1.159 +xhr.responseType = 'arraybuffer';
   1.160 +xhr.onloadend = continueTest;
   1.161 +xhr.send(null);
   1.162 +yield undefined;
   1.163 +is(xhr.status, 200, "wrong status");
   1.164 +checkResponseTextAccessThrows(xhr);
   1.165 +checkResponseXMLAccessThrows(xhr);
   1.166 +var ab = xhr.response;
   1.167 +ok(ab != null, "should have a non-null arraybuffer");
   1.168 +arraybuffer_equals_to(ab, "hello pass\n");
   1.169 +
   1.170 +// test reusing the same XHR (Bug 680816)
   1.171 +xhr.open("GET", 'file_XHR_binary1.bin');
   1.172 +xhr.responseType = 'arraybuffer';
   1.173 +xhr.onloadend = continueTest;
   1.174 +xhr.send(null);
   1.175 +yield undefined;
   1.176 +is(xhr.status, 200, "wrong status");
   1.177 +var ab2 = xhr.response;
   1.178 +ok(ab2 != null, "should have a non-null arraybuffer");
   1.179 +ok(ab2 != ab, "arraybuffer on XHR reuse should be distinct");
   1.180 +arraybuffer_equals_to(ab, "hello pass\n");
   1.181 +arraybuffer_equals_to(ab2, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
   1.182 +
   1.183 +// with a binary file
   1.184 +xhr = new XMLHttpRequest();
   1.185 +xhr.open("GET", 'file_XHR_binary1.bin'); 
   1.186 +xhr.responseType = 'arraybuffer';
   1.187 +xhr.onloadend = continueTest;
   1.188 +xhr.send(null);
   1.189 +yield undefined;
   1.190 +is(xhr.status, 200, "wrong status");
   1.191 +checkResponseTextAccessThrows(xhr);
   1.192 +checkResponseXMLAccessThrows(xhr);
   1.193 +ab = xhr.response;
   1.194 +ok(ab != null, "should have a non-null arraybuffer");
   1.195 +arraybuffer_equals_to(ab, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
   1.196 +is(xhr.response, xhr.response, "returns the same ArrayBuffer");
   1.197 +
   1.198 +// test response (responseType='json')
   1.199 +var xhr = new XMLHttpRequest();
   1.200 +xhr.open("POST", 'responseIdentical.sjs');
   1.201 +xhr.responseType = 'json';
   1.202 +var jsonObjStr = JSON.stringify({title: "aBook", author: "john"});
   1.203 +xhr.onloadend = continueTest;
   1.204 +xhr.send(jsonObjStr);
   1.205 +yield undefined;
   1.206 +is(xhr.status, 200, "wrong status");
   1.207 +checkResponseTextAccessThrows(xhr);
   1.208 +checkResponseXMLAccessThrows(xhr);
   1.209 +is(JSON.stringify(xhr.response), jsonObjStr, "correct result");
   1.210 +is(xhr.response, xhr.response, "returning the same object on each access");
   1.211 +
   1.212 +// with invalid json
   1.213 +var xhr = new XMLHttpRequest();
   1.214 +xhr.open("POST", 'responseIdentical.sjs');
   1.215 +xhr.responseType = 'json';
   1.216 +xhr.onloadend = continueTest;
   1.217 +xhr.send("{");
   1.218 +yield undefined;
   1.219 +is(xhr.status, 200, "wrong status");
   1.220 +checkResponseTextAccessThrows(xhr);
   1.221 +checkResponseXMLAccessThrows(xhr);
   1.222 +is(xhr.response, null, "Bad JSON should result in null response.");
   1.223 +is(xhr.response, null, "Bad JSON should result in null response even 2nd time.");
   1.224 +
   1.225 +// Test status/statusText in all readyStates
   1.226 +xhr = new XMLHttpRequest();
   1.227 +function checkXHRStatus() {
   1.228 +  if (xhr.readyState == xhr.UNSENT || xhr.readyState == xhr.OPENED) {
   1.229 +    is(xhr.status, 0, "should be 0 before getting data");
   1.230 +    is(xhr.statusText, "", "should be empty before getting data");
   1.231 +  }
   1.232 +  else {
   1.233 +    is(xhr.status, 200, "should be 200 when we have data");
   1.234 +    is(xhr.statusText, "OK", "should be OK when we have data");
   1.235 +  }
   1.236 +}
   1.237 +checkXHRStatus();
   1.238 +xhr.open("GET", 'file_XHR_binary1.bin'); 
   1.239 +checkXHRStatus();
   1.240 +xhr.responseType = 'arraybuffer';
   1.241 +xhr.send(null);
   1.242 +xhr.onreadystatechange = continueTest;
   1.243 +while (xhr.readyState != 4) {
   1.244 +  checkXHRStatus();
   1.245 +  yield undefined;
   1.246 +}
   1.247 +checkXHRStatus();
   1.248 +
   1.249 +// test response (responseType='blob')
   1.250 +var responseTypes = ['blob', 'moz-blob'];
   1.251 +for (var i = 0; i < responseTypes.length; i++) {
   1.252 +  var t = responseTypes[i];
   1.253 +  // with a simple text file
   1.254 +  xhr = new XMLHttpRequest();
   1.255 +  xhr.open("GET", 'file_XHR_pass2.txt'); 
   1.256 +  xhr.responseType = t;
   1.257 +  xhr.onloadend = continueTest;
   1.258 +  xhr.send(null);
   1.259 +  yield undefined;
   1.260 +  is(xhr.status, 200, "wrong status");
   1.261 +  checkResponseTextAccessThrows(xhr);
   1.262 +  checkResponseXMLAccessThrows(xhr);
   1.263 +  var b = xhr.response;
   1.264 +  ok(b, "should have a non-null blob");
   1.265 +  ok(b instanceof Blob, "should be a Blob");
   1.266 +  ok(!(b instanceof File), "should not be a File");
   1.267 +  is(b.size, "hello pass\n".length, "wrong blob size");
   1.268 +
   1.269 +  var fr = new FileReader();
   1.270 +  fr.onload = continueTest;
   1.271 +  fr.readAsBinaryString(b);
   1.272 +  yield undefined;
   1.273 +  ok(fr.result, "hello pass\n", "wrong values");
   1.274 +
   1.275 +  // with a binary file
   1.276 +  xhr = new XMLHttpRequest();
   1.277 +  xhr.open("GET", 'file_XHR_binary1.bin', true);
   1.278 +  xhr.send(null);
   1.279 +  xhr.onreadystatechange = continueTest;
   1.280 +  while(xhr.readyState != 2)
   1.281 +    yield undefined;
   1.282 +
   1.283 +  is(xhr.status, 200, "wrong status");
   1.284 +  xhr.responseType = t;
   1.285 +
   1.286 +  while(xhr.readyState != 4)
   1.287 +    yield undefined;
   1.288 +  
   1.289 +  xhr.onreadystatechange = null;
   1.290 +
   1.291 +  b = xhr.response;
   1.292 +  ok(b != null, "should have a non-null blob");
   1.293 +  is(b.size, 12, "wrong blob size");
   1.294 +
   1.295 +  fr = new FileReader();
   1.296 +  fr.readAsBinaryString(b);
   1.297 +  xhr = null; // kill the XHR object
   1.298 +  b = null;
   1.299 +  SpecialPowers.gc();
   1.300 +  fr.onload = continueTest;
   1.301 +  yield undefined;
   1.302 +  is(fr.result, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb", "wrong values");
   1.303 +
   1.304 +  // with a larger binary file
   1.305 +  xhr = new XMLHttpRequest();
   1.306 +  xhr.open("GET", 'file_XHR_binary2.bin', true);
   1.307 +  xhr.responseType = t;
   1.308 +  xhr.send(null);
   1.309 +  xhr.onreadystatechange = continueTest;
   1.310 +
   1.311 +  while (xhr.readyState != 4)
   1.312 +    yield undefined;
   1.313 +
   1.314 +  xhr.onreadystatechange = null;
   1.315 +
   1.316 +  var b = xhr.response;
   1.317 +  ok(b != null, "should have a non-null blob");
   1.318 +  is(b.size, 65536, "wrong blob size");
   1.319 +
   1.320 +  fr = new FileReader();
   1.321 +  fr.readAsArrayBuffer(b);
   1.322 +  fr.onload = continueTest;
   1.323 +  xhr = null; // kill the XHR object
   1.324 +  b = null;
   1.325 +  SpecialPowers.gc();
   1.326 +  yield undefined;
   1.327 +
   1.328 +  var u8 = new Uint8Array(fr.result);
   1.329 +  for (var i = 0; i < 65536; i++) {
   1.330 +    if (u8[i] !== (i & 255)) {
   1.331 +      break;
   1.332 +    }
   1.333 +  }
   1.334 +  is(i, 65536, "wrong value at offset " + i);
   1.335 +}
   1.336 +
   1.337 +var client = new XMLHttpRequest();
   1.338 +client.open("GET", "file_XHR_pass1.xml", true);
   1.339 +client.send();
   1.340 +client.onreadystatechange = function() {
   1.341 +  if(client.readyState == 4) {
   1.342 +    try {
   1.343 +      is(client.responseXML, null, "responseXML should be null.");
   1.344 +      is(client.responseText, "", "responseText should be empty string.");
   1.345 +      is(client.response, "", "response should be empty string.");
   1.346 +      is(client.status, 0, "status should be 0.");
   1.347 +      is(client.statusText, "", "statusText should be empty string.");
   1.348 +      is(client.getAllResponseHeaders(), "",
   1.349 +         "getAllResponseHeaders() should return empty string.");
   1.350 +    } catch(ex) {
   1.351 +      ok(false, "Shouldn't throw! [" + ex + "]");
   1.352 +    }
   1.353 +  }
   1.354 +}
   1.355 +client.abort();
   1.356 +
   1.357 +SimpleTest.finish();
   1.358 +yield undefined;
   1.359 +
   1.360 +} /* runTests */
   1.361 +</script>
   1.362 +</pre>
   1.363 +</body>
   1.364 +</html>

mercurial