2008-01-31 08:16:54 +00:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
2008-03-26 02:46:08 +00:00
|
|
|
<title>Test for XMLHttpRequest</title>
|
2009-05-06 20:46:04 +00:00
|
|
|
<script type="text/javascript" src="/MochiKit/packed.js"></script>
|
2008-01-31 08:16:54 +00:00
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p id="display"></p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript">
|
2011-05-10 23:18:55 +00:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
2008-01-31 08:16:54 +00:00
|
|
|
|
2008-03-26 02:46:08 +00:00
|
|
|
var path = "/tests/content/base/test/";
|
2008-01-31 08:16:54 +00:00
|
|
|
|
2008-03-26 02:46:08 +00:00
|
|
|
var passFiles = [['file_XHR_pass1.xml', 'GET'],
|
|
|
|
['file_XHR_pass2.txt', 'GET'],
|
|
|
|
['file_XHR_pass3.txt', 'GET'],
|
2008-01-31 08:16:54 +00:00
|
|
|
];
|
|
|
|
|
2008-03-26 02:46:08 +00:00
|
|
|
var failFiles = [['//example.com' + path + 'file_XHR_pass1.xml', 'GET'],
|
|
|
|
['ftp://localhost' + path + 'file_XHR_pass1.xml', 'GET'],
|
|
|
|
['file_XHR_fail1.txt', 'GET'],
|
2008-01-31 08:16:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (i = 0; i < passFiles.length; ++i) {
|
|
|
|
xhr = new XMLHttpRequest();
|
2011-05-19 05:11:51 +00:00
|
|
|
is(xhr.responseType, "", "wrong initial responseType");
|
2008-01-31 08:16:54 +00:00
|
|
|
xhr.open(passFiles[i][1], passFiles[i][0], false);
|
|
|
|
xhr.send(null);
|
|
|
|
is(xhr.status, 200, "wrong status");
|
|
|
|
if (xhr.responseXML) {
|
|
|
|
is((new XMLSerializer()).serializeToString(xhr.responseXML.documentElement),
|
|
|
|
"<res>hello</res>",
|
2011-05-10 23:18:55 +00:00
|
|
|
"wrong responseXML");
|
2011-05-19 05:11:51 +00:00
|
|
|
is(xhr.response, "<res>hello</res>\n", "wrong response");
|
2008-01-31 08:16:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-05-10 23:18:55 +00:00
|
|
|
is(xhr.responseText, "hello pass\n", "wrong responseText");
|
2011-05-19 05:11:51 +00:00
|
|
|
is(xhr.response, "hello pass\n", "wrong response");
|
2008-01-31 08:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < failFiles.length; ++i) {
|
|
|
|
xhr = new XMLHttpRequest();
|
2008-03-26 02:46:08 +00:00
|
|
|
didthrow = false;
|
2008-01-31 08:16:54 +00:00
|
|
|
try {
|
|
|
|
xhr.open(failFiles[i][1], failFiles[i][0], false);
|
|
|
|
xhr.send(null);
|
|
|
|
}
|
|
|
|
catch (e) {
|
2008-03-26 02:46:08 +00:00
|
|
|
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");
|
2008-01-31 08:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-14 17:52:19 +00:00
|
|
|
|
2011-05-10 23:18:55 +00:00
|
|
|
// test response (responseType='document')
|
|
|
|
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) {
|
|
|
|
var didthrow = false;
|
2011-05-19 05:11:51 +00:00
|
|
|
try { xhr.responseType = 'document'; } catch (e) { didthrow = true; }
|
2011-05-10 23:18:55 +00:00
|
|
|
ok(didthrow, "should have thrown when accessing responseType");
|
|
|
|
}
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
checkSetResponseTypeThrows(xhr);
|
|
|
|
xhr.open("GET", 'file_XHR_pass1.xml', false);
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'document';
|
2011-05-10 23:18:55 +00:00
|
|
|
xhr.send(null);
|
|
|
|
checkSetResponseTypeThrows(xhr);
|
|
|
|
is(xhr.status, 200, "wrong status");
|
|
|
|
checkResponseTextAccessThrows(xhr);
|
2011-05-19 05:11:51 +00:00
|
|
|
is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
|
2011-05-10 23:18:55 +00:00
|
|
|
"<res>hello</res>",
|
|
|
|
"wrong response");
|
|
|
|
|
|
|
|
// test response (responseType='text')
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'file_XHR_pass2.txt', false);
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'text';
|
2011-05-10 23:18:55 +00:00
|
|
|
xhr.send(null);
|
|
|
|
is(xhr.status, 200, "wrong status");
|
|
|
|
checkResponseXMLAccessThrows(xhr);
|
2011-05-19 05:11:51 +00:00
|
|
|
is(xhr.response, "hello pass\n", "wrong response");
|
2011-05-10 23:18:55 +00:00
|
|
|
|
|
|
|
// test response (responseType='arraybuffer')
|
|
|
|
function arraybuffer_equals_to(ab, s) {
|
|
|
|
is(ab.byteLength, s.length, "wrong arraybuffer byteLength");
|
|
|
|
|
|
|
|
u8v = new Uint8Array(ab);
|
|
|
|
is(String.fromCharCode.apply(String, u8v), s, "wrong values");
|
|
|
|
}
|
2010-08-14 17:52:19 +00:00
|
|
|
|
|
|
|
// with a simple text file
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'file_XHR_pass2.txt', false);
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'arraybuffer';
|
2010-08-14 17:52:19 +00:00
|
|
|
xhr.send(null);
|
|
|
|
is(xhr.status, 200, "wrong status");
|
2011-05-10 23:18:55 +00:00
|
|
|
checkResponseTextAccessThrows(xhr);
|
|
|
|
checkResponseXMLAccessThrows(xhr);
|
2011-05-19 05:11:51 +00:00
|
|
|
ab = xhr.response;
|
2010-08-14 17:52:19 +00:00
|
|
|
ok(ab != null, "should have a non-null arraybuffer");
|
2011-05-10 23:18:55 +00:00
|
|
|
arraybuffer_equals_to(ab, "hello pass\n");
|
2010-08-14 17:52:19 +00:00
|
|
|
|
|
|
|
// with a binary file
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'file_XHR_binary1.bin', false);
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'arraybuffer';
|
2010-08-14 17:52:19 +00:00
|
|
|
xhr.send(null)
|
|
|
|
is(xhr.status, 200, "wrong status");
|
2011-05-10 23:18:55 +00:00
|
|
|
checkResponseTextAccessThrows(xhr);
|
|
|
|
checkResponseXMLAccessThrows(xhr);
|
2011-05-19 05:11:51 +00:00
|
|
|
ab = xhr.response;
|
2010-08-14 17:52:19 +00:00
|
|
|
ok(ab != null, "should have a non-null arraybuffer");
|
2011-05-10 23:18:55 +00:00
|
|
|
arraybuffer_equals_to(ab, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
|
|
|
|
|
2011-05-24 01:09:28 +00:00
|
|
|
// test array buffer GetResult returns the same object
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'file_XHR_binary1.bin', false);
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
xhr.send(null)
|
|
|
|
is(xhr.status, 200, "wrong status");
|
|
|
|
checkResponseTextAccessThrows(xhr);
|
|
|
|
checkResponseXMLAccessThrows(xhr);
|
|
|
|
is(xhr.response, xhr.response, "returns the same ArrayBuffer");
|
|
|
|
|
2011-05-10 23:18:55 +00:00
|
|
|
// test response (responseType='blob')
|
|
|
|
var onloadCount = 0;
|
|
|
|
function checkOnloadCount() {
|
|
|
|
if (++onloadCount >= 2) SimpleTest.finish();
|
|
|
|
};
|
|
|
|
|
|
|
|
// with a simple text file
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", 'file_XHR_pass2.txt', false);
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'blob';
|
2011-05-10 23:18:55 +00:00
|
|
|
xhr.send(null);
|
|
|
|
is(xhr.status, 200, "wrong status");
|
|
|
|
checkResponseTextAccessThrows(xhr);
|
|
|
|
checkResponseXMLAccessThrows(xhr);
|
2011-05-19 05:11:51 +00:00
|
|
|
b = xhr.response;
|
2011-05-10 23:18:55 +00:00
|
|
|
ok(b, "should have a non-null blob");
|
2011-06-30 21:42:15 +00:00
|
|
|
ok(b instanceof Blob, "should be a Blob");
|
|
|
|
ok(!(b instanceof File), "should not be a File");
|
2011-05-10 23:18:55 +00:00
|
|
|
is(b.size, "hello pass\n".length, "wrong blob size");
|
|
|
|
|
2011-05-11 18:06:47 +00:00
|
|
|
var fr = new FileReader();
|
2011-05-10 23:18:55 +00:00
|
|
|
fr.onload = function() {
|
|
|
|
ok(fr.result, "hello pass\n", "wrong values");
|
|
|
|
checkOnloadCount();
|
|
|
|
};
|
|
|
|
fr.readAsBinaryString(b);
|
2010-08-14 17:52:19 +00:00
|
|
|
|
2011-05-10 23:18:55 +00:00
|
|
|
// with a binary file
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
switch (xhr.readyState) {
|
|
|
|
case 2:
|
|
|
|
is(xhr.status, 200, "wrong status");
|
2011-05-19 05:11:51 +00:00
|
|
|
xhr.responseType = 'blob';
|
2011-05-10 23:18:55 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2011-05-19 05:11:51 +00:00
|
|
|
b = xhr.response;
|
2011-05-10 23:18:55 +00:00
|
|
|
ok(b != null, "should have a non-null blob");
|
|
|
|
is(b.size, 12, "wrong blob size");
|
|
|
|
|
2011-05-11 18:06:47 +00:00
|
|
|
var fr = new FileReader();
|
2011-05-10 23:18:55 +00:00
|
|
|
fr.onload = function() {
|
|
|
|
is(fr.result, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb", "wrong values");
|
|
|
|
checkOnloadCount();
|
|
|
|
};
|
|
|
|
xhr = null; // kill the XHR object
|
|
|
|
SpecialPowers.gc();
|
|
|
|
fr.readAsBinaryString(b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open("GET", 'file_XHR_binary1.bin', true);
|
|
|
|
xhr.send(null);
|
2010-08-14 17:52:19 +00:00
|
|
|
|
2010-11-11 21:39:14 +00: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.");
|
2011-05-19 05:11:51 +00:00
|
|
|
is(client.response, "", "response should be empty string.");
|
2010-11-11 21:39:14 +00:00
|
|
|
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();
|
|
|
|
|
2008-01-31 08:16:54 +00:00
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|