gecko/content/base/test/test_XHR.html

362 lines
11 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<head>
<title>Test for XMLHttpRequest</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
2011-12-08 05:55:20 -08:00
<body onload="gen.next();">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
2011-12-08 05:55:20 -08:00
<script class="testbody" type="application/javascript;version=1.7">
"use strict";
SimpleTest.waitForExplicitFinish();
2011-12-08 05:55:20 -08:00
var gen = runTests();
function continueTest() { gen.next(); }
function runTests() {
var path = "/tests/content/base/test/";
var passFiles = [['file_XHR_pass1.xml', 'GET', 200, 'text/xml'],
['file_XHR_pass2.txt', 'GET', 200, 'text/plain'],
['file_XHR_pass3.txt', 'GET', 200, 'text/plain'],
['data:text/xml,%3Cres%3Ehello%3C/res%3E%0A', 'GET', 0, 'text/xml'],
['data:text/plain,hello%20pass%0A', 'GET', 0, 'text/plain'],
['data:,foo', 'GET', 0, 'text/plain;charset=US-ASCII', 'foo'],
['data:text/plain;base64,Zm9v', 'GET', 0, 'text/plain', 'foo'],
['data:text/plain,foo#bar', 'GET', 0, 'text/plain', 'foo'],
['data:text/plain,foo%23bar', 'GET', 0, 'text/plain', 'foo#bar'],
];
var failFiles = [['//example.com' + path + 'file_XHR_pass1.xml', 'GET'],
['ftp://localhost' + path + 'file_XHR_pass1.xml', 'GET'],
['file_XHR_fail1.txt', 'GET'],
];
for (i = 0; i < passFiles.length; ++i) {
xhr = new XMLHttpRequest();
is(xhr.getResponseHeader("Content-Type"), null, "should be null");
is(xhr.getAllResponseHeaders(), "", "should be empty string");
is(xhr.responseType, "", "wrong initial responseType");
xhr.open(passFiles[i][1], passFiles[i][0], false);
xhr.send(null);
is(xhr.status, passFiles[i][2], "wrong status");
is(xhr.getResponseHeader("Content-Type"), passFiles[i][3], "wrong content type");
var headers = xhr.getAllResponseHeaders();
ok(/(?:^|\n)Content-Type:\s*([^\n]*)\n/i.test(headers) &&
RegExp.$1 === passFiles[i][3], "wrong response headers");
if (xhr.responseXML) {
is((new XMLSerializer()).serializeToString(xhr.responseXML.documentElement),
passFiles[i][4] || "<res>hello</res>", "wrong responseXML");
is(xhr.response, passFiles[i][4] || "<res>hello</res>\n", "wrong response");
}
else {
is(xhr.responseText, passFiles[i][4] || "hello pass\n", "wrong responseText");
is(xhr.response, passFiles[i][4] || "hello pass\n", "wrong response");
}
}
for (i = 0; i < failFiles.length; ++i) {
xhr = new XMLHttpRequest();
var didthrow = false;
try {
xhr.open(failFiles[i][1], failFiles[i][0], false);
xhr.send(null);
}
catch (e) {
didthrow = true;
}
if (!didthrow) {
is(xhr.status, 301, "wrong status");
is(xhr.responseText, "redirect file\n", "wrong response");
}
else {
ok(1, "should have thrown or given incorrect result");
}
}
function checkResponseTextAccessThrows(xhr) {
var didthrow = false;
try { xhr.responseText } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when accessing responseText");
}
function checkResponseXMLAccessThrows(xhr) {
var didthrow = false;
try { xhr.responseXML } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when accessing responseXML");
}
function checkSetResponseTypeThrows(xhr, type) {
var didthrow = false;
try { xhr.responseType = type; } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when setting responseType");
}
2011-12-08 05:55:20 -08:00
function checkOpenThrows(xhr, method, url, async) {
var didthrow = false;
try { xhr.open(method, url, async); } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when open is called");
}
2011-12-08 05:55:20 -08:00
// test response (sync, responseType is not changeable)
xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("GET", 'file_XHR_pass2.txt', false);
checkSetResponseTypeThrows(xhr, "");
checkSetResponseTypeThrows(xhr, "text");
checkSetResponseTypeThrows(xhr, "document");
2011-12-08 05:55:20 -08:00
checkSetResponseTypeThrows(xhr, "arraybuffer");
checkSetResponseTypeThrows(xhr, "blob");
checkSetResponseTypeThrows(xhr, "json");
checkSetResponseTypeThrows(xhr, "moz-chunked-text");
checkSetResponseTypeThrows(xhr, "moz-chunked-arraybuffer");
2011-12-08 05:55:20 -08:00
xhr.send(null);
checkSetResponseTypeThrows(xhr, "document");
is(xhr.status, 200, "wrong status");
is(xhr.response, "hello pass\n", "wrong response");
// test response (responseType='document')
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass1.xml');
xhr.responseType = 'document';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send(null);
2011-12-08 05:55:20 -08:00
yield;
checkSetResponseTypeThrows(xhr, "document");
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
"<res>hello</res>",
"wrong response");
// test response (responseType='text')
xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'text';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send(null);
2011-12-08 05:55:20 -08:00
yield;
is(xhr.status, 200, "wrong status");
checkResponseXMLAccessThrows(xhr);
is(xhr.response, "hello pass\n", "wrong response");
// test response (responseType='arraybuffer')
function arraybuffer_equals_to(ab, s) {
is(ab.byteLength, s.length, "wrong arraybuffer byteLength");
var u8v = new Uint8Array(ab);
is(String.fromCharCode.apply(String, u8v), s, "wrong values");
}
// with a simple text file
xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'arraybuffer';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send(null);
2011-12-08 05:55:20 -08:00
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
var ab = xhr.response;
ok(ab != null, "should have a non-null arraybuffer");
arraybuffer_equals_to(ab, "hello pass\n");
2011-08-24 09:33:31 -07:00
// test reusing the same XHR (Bug 680816)
2011-12-08 05:55:20 -08:00
xhr.open("GET", 'file_XHR_binary1.bin');
2011-08-24 09:33:31 -07:00
xhr.responseType = 'arraybuffer';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
2011-08-24 09:33:31 -07:00
xhr.send(null);
2011-12-08 05:55:20 -08:00
yield;
2011-08-24 09:33:31 -07:00
is(xhr.status, 200, "wrong status");
var ab2 = xhr.response;
2011-08-24 09:33:31 -07:00
ok(ab2 != null, "should have a non-null arraybuffer");
ok(ab2 != ab, "arraybuffer on XHR reuse should be distinct");
arraybuffer_equals_to(ab, "hello pass\n");
arraybuffer_equals_to(ab2, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
// with a binary file
xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("GET", 'file_XHR_binary1.bin');
xhr.responseType = 'arraybuffer';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
ab = xhr.response;
ok(ab != null, "should have a non-null arraybuffer");
arraybuffer_equals_to(ab, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
is(xhr.response, xhr.response, "returns the same ArrayBuffer");
// test response (responseType='json')
var xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'json';
var jsonObjStr = JSON.stringify({title: "aBook", author: "john"});
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send(jsonObjStr);
2011-12-08 05:55:20 -08:00
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
is(JSON.stringify(xhr.response), jsonObjStr, "correct result");
is(xhr.response, xhr.response, "returning the same object on each access");
// with invalid json
var xhr = new XMLHttpRequest();
2011-12-08 05:55:20 -08:00
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'json';
2011-12-08 05:55:20 -08:00
xhr.onloadend = continueTest;
xhr.send("{");
2011-12-08 05:55:20 -08:00
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
is(xhr.response, null, "Bad JSON should result in null response.");
is(xhr.response, null, "Bad JSON should result in null response even 2nd time.");
// Test status/statusText in all readyStates
xhr = new XMLHttpRequest();
function checkXHRStatus() {
if (xhr.readyState == xhr.UNSENT || xhr.readyState == xhr.OPENED) {
is(xhr.status, 0, "should be 0 before getting data");
is(xhr.statusText, "", "should be empty before getting data");
}
else {
is(xhr.status, 200, "should be 200 when we have data");
is(xhr.statusText, "OK", "should be OK when we have data");
}
}
checkXHRStatus();
xhr.open("GET", 'file_XHR_binary1.bin');
checkXHRStatus();
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = continueTest;
xhr.send(null);
while (xhr.readyState != 4) {
checkXHRStatus();
yield;
}
checkXHRStatus();
// test response (responseType='blob')
2012-01-30 02:34:15 -08:00
var responseTypes = ['blob', 'moz-blob'];
for (var i = 0; i < responseTypes.length; i++) {
var t = responseTypes[i];
// with a simple text file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = t;
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
var b = xhr.response;
ok(b, "should have a non-null blob");
ok(b instanceof Blob, "should be a Blob");
ok(!(b instanceof File), "should not be a File");
is(b.size, "hello pass\n".length, "wrong blob size");
var fr = new FileReader();
fr.onload = continueTest;
fr.readAsBinaryString(b);
yield;
ok(fr.result, "hello pass\n", "wrong values");
// with a binary file
xhr = new XMLHttpRequest();
xhr.onreadystatechange = continueTest;
xhr.open("GET", 'file_XHR_binary1.bin', true);
xhr.send(null);
while(xhr.readyState != 2)
yield;
is(xhr.status, 200, "wrong status");
xhr.responseType = t;
while(xhr.readyState != 4)
yield;
xhr.onreadystatechange = null;
b = xhr.response;
ok(b != null, "should have a non-null blob");
is(b.size, 12, "wrong blob size");
fr = new FileReader();
fr.readAsBinaryString(b);
xhr = null; // kill the XHR object
b = null;
SpecialPowers.gc();
fr.onload = continueTest;
yield;
is(fr.result, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb", "wrong values");
// with a larger binary file
xhr = new XMLHttpRequest();
xhr.onreadystatechange = continueTest;
xhr.open("GET", 'file_XHR_binary2.bin', true);
xhr.responseType = t;
xhr.send(null);
while (xhr.readyState != 4)
yield;
xhr.onreadystatechange = null;
var b = xhr.response;
ok(b != null, "should have a non-null blob");
is(b.size, 65536, "wrong blob size");
fr = new FileReader();
fr.readAsArrayBuffer(b);
fr.onload = continueTest;
xhr = null; // kill the XHR object
b = null;
SpecialPowers.gc();
yield;
var u8 = new Uint8Array(fr.result);
for (var i = 0; i < 65536; i++) {
if (u8[i] !== (i & 255)) {
break;
}
}
is(i, 65536, "wrong value at offset " + i);
2012-01-30 02:34:15 -08:00
}
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if(client.readyState == 4) {
try {
is(client.responseXML, null, "responseXML should be null.");
is(client.responseText, "", "responseText should be empty string.");
is(client.response, "", "response should be empty string.");
is(client.status, 0, "status should be 0.");
is(client.statusText, "", "statusText should be empty string.");
is(client.getAllResponseHeaders(), "",
"getAllResponseHeaders() should return empty string.");
} catch(ex) {
ok(false, "Shouldn't throw! [" + ex + "]");
}
}
}
client.open("GET", "file_XHR_pass1.xml", true);
client.send();
client.abort();
SimpleTest.finish();
2011-12-08 05:55:20 -08:00
yield;
2011-12-08 05:55:20 -08:00
} /* runTests */
</script>
</pre>
</body>
</html>