Bug 701787 - Part 2: Tests. r=smaug.

This commit is contained in:
Masatoshi Kimura 2011-12-08 15:55:20 +02:00
parent c65521aa7c
commit 169dbb1e3d
6 changed files with 183 additions and 89 deletions

View File

@ -5,15 +5,20 @@
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<body onload="gen.next();">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
<script class="testbody" type="application/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
var gen = runTests();
function continueTest() { gen.next(); }
function runTests() {
var path = "/tests/content/base/test/";
var passFiles = [['file_XHR_pass1.xml', 'GET'],
@ -63,7 +68,6 @@ for (i = 0; i < failFiles.length; ++i) {
}
}
// test response (responseType='document')
function checkResponseTextAccessThrows(xhr) {
var didthrow = false;
try { xhr.responseText } catch (e) { didthrow = true; }
@ -84,16 +88,37 @@ function checkSetResponseTypeThrows(xhr, type) {
try { xhr.responseType = type; } catch (e) { didthrow = true; }
ok(didthrow, "should have thrown when setting responseType");
}
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");
}
// test response (sync, responseType is not changeable)
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
checkSetResponseTypeThrows(xhr, "");
checkSetResponseTypeThrows(xhr, "text");
checkSetResponseTypeThrows(xhr, "document");
xhr.open("GET", 'file_XHR_pass1.xml', false);
checkSetResponseTypeThrows(xhr, "arraybuffer");
checkSetResponseTypeThrows(xhr, "blob");
checkSetResponseTypeThrows(xhr, "moz-json");
checkSetResponseTypeThrows(xhr, "moz-chunked-text");
checkSetResponseTypeThrows(xhr, "moz-chunked-arraybuffer");
xhr.responseType = 'document';
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';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
checkSetResponseTypeThrows(xhr, "document");
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
"<res>hello</res>",
@ -101,9 +126,11 @@ is((new XMLSerializer()).serializeToString(xhr.response.documentElement),
// test response (responseType='text')
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'text';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseXMLAccessThrows(xhr);
is(xhr.response, "hello pass\n", "wrong response");
@ -118,9 +145,11 @@ function arraybuffer_equals_to(ab, s) {
// with a simple text file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'arraybuffer';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -129,9 +158,11 @@ ok(ab != null, "should have a non-null arraybuffer");
arraybuffer_equals_to(ab, "hello pass\n");
// test reusing the same XHR (Bug 680816)
xhr.open("GET", 'file_XHR_binary1.bin', false);
xhr.open("GET", 'file_XHR_binary1.bin');
xhr.responseType = 'arraybuffer';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
ab2 = xhr.response;
ok(ab2 != null, "should have a non-null arraybuffer");
@ -141,9 +172,11 @@ arraybuffer_equals_to(ab2, "\xaa\xee\0\x03\xff\xff\xff\xff\xbb\xbb\xbb\xbb");
// with a binary file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_binary1.bin', false);
xhr.open("GET", 'file_XHR_binary1.bin');
xhr.responseType = 'arraybuffer';
xhr.send(null)
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -154,10 +187,12 @@ is(xhr.response, xhr.response, "returns the same ArrayBuffer");
// test response (responseType='moz-json')
var xhr = new XMLHttpRequest();
xhr.open("POST", 'responseIdentical.sjs', false);
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'moz-json';
jsonObjStr = JSON.stringify({title: "aBook", author: "john"});
xhr.onloadend = continueTest;
xhr.send(jsonObjStr);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -166,9 +201,11 @@ is(xhr.response, xhr.response, "returning the same object on each access");
// with invalid json
var xhr = new XMLHttpRequest();
xhr.open("POST", 'responseIdentical.sjs', false);
xhr.open("POST", 'responseIdentical.sjs');
xhr.responseType = 'moz-json';
xhr.onloadend = continueTest;
xhr.send("{");
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -183,9 +220,11 @@ function checkOnloadCount() {
// with a simple text file
xhr = new XMLHttpRequest();
xhr.open("GET", 'file_XHR_pass2.txt', false);
xhr.open("GET", 'file_XHR_pass2.txt');
xhr.responseType = 'blob';
xhr.onloadend = continueTest;
xhr.send(null);
yield;
is(xhr.status, 200, "wrong status");
checkResponseTextAccessThrows(xhr);
checkResponseXMLAccessThrows(xhr);
@ -281,6 +320,8 @@ client.open("GET", "file_XHR_pass1.xml", true);
client.send();
client.abort();
yield;
} /* runTests */
</script>
</pre>
</body>

View File

@ -8,7 +8,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=464848
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<body onload="gen.next();">
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
<p id="display">
@ -19,6 +19,12 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=464848
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
var gen = runTests();
function continueTest() { gen.next(); }
function runTests() {
xhr = new XMLHttpRequest();
xhr.open("GET", "file_XHRSendData_doc.xml", false);
@ -176,12 +182,16 @@ for (var i = 0; i < testDOMFiles.length; i++) {
try {
for each(test in tests) {
xhr = new XMLHttpRequest;
xhr.open("POST", "file_XHRSendData.sjs", false);
xhr.open("POST", "file_XHRSendData.sjs", !!test.resType);
if (test.contentType)
xhr.setRequestHeader("Content-Type", test.contentType);
if (test.resType)
if (test.resType) {
xhr.responseType = test.resType;
xhr.onloadend = continueTest;
}
xhr.send(test.body);
if (test.resType)
yield;
if (test.resContentType) {
is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
@ -234,6 +244,10 @@ function is_identical_arraybuffer(ab1, ab2) {
String.fromCharCode.apply(String, u8v2), "arraybuffer values not equal");
}
SimpleTest.finish();
yield;
} /* runTests */
</script>
</pre>
</body>

View File

@ -424,51 +424,55 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583
var xhr = SpecialPowers.createSystemXHR();
xhr.withCredentials = true;
// also, test mixed mode UI
xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", false, "user 1", "password 1");
xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1");
xhr.send();
ok(xhr.status == 200, "Failed to set credentials in test 5.c");
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.c");
gEventSourceObj5_c = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc",
{ withCredentials: true } );
ok(gEventSourceObj5_c.withCredentials, "Wrong withCredentials in test 5.c");
gEventSourceObj5_c = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc",
{ withCredentials: true } );
ok(gEventSourceObj5_c.withCredentials, "Wrong withCredentials in test 5.c");
gEventSourceObj5_c.onmessage = function(e) {
ok(e.origin == "https://example.com", "Wrong Origin in test 5.c");
fn_onmessage(e);
gEventSourceObj5_c.onmessage = function(e) {
ok(e.origin == "https://example.com", "Wrong Origin in test 5.c");
fn_onmessage(e);
};
gEventSourceObj5_c.hits = [];
gEventSourceObj5_c.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_c.hits['fn_onmessage'] > 0, "Test 5.c failed");
gEventSourceObj5_c.close();
doTest5_d(test_id);
}, parseInt(3000*stress_factor));
};
gEventSourceObj5_c.hits = [];
gEventSourceObj5_c.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_c.hits['fn_onmessage'] > 0, "Test 5.c failed");
gEventSourceObj5_c.close();
doTest5_d(test_id);
}, parseInt(3000*stress_factor));
}
function doTest5_d(test_id)
{
var xhr = SpecialPowers.createSystemXHR();
xhr.withCredentials = true;
xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", false, "user 2", "password 2");
xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2");
xhr.send();
ok(xhr.status == 200, "Failed to set credentials in test 5.d");
gEventSourceObj5_d = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc");
ok(!gEventSourceObj5_d.withCredentials, "Wrong withCredentials in test 5.d");
gEventSourceObj5_d.onmessage = function(e) {
ok(e.origin == "https://example.com", "Wrong Origin in test 5.d");
fn_onmessage(e);
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.d");
gEventSourceObj5_d = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc");
ok(!gEventSourceObj5_d.withCredentials, "Wrong withCredentials in test 5.d");
gEventSourceObj5_d.onmessage = function(e) {
ok(e.origin == "https://example.com", "Wrong Origin in test 5.d");
fn_onmessage(e);
};
gEventSourceObj5_d.hits = [];
gEventSourceObj5_d.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_d.hits['fn_onmessage'] == 0, "Test 5.d failed");
gEventSourceObj5_d.close();
setTestHasFinished(test_id);
}, parseInt(3000*stress_factor));
};
gEventSourceObj5_d.hits = [];
gEventSourceObj5_d.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_d.hits['fn_onmessage'] == 0, "Test 5.d failed");
gEventSourceObj5_d.close();
setTestHasFinished(test_id);
}, parseInt(3000*stress_factor));
}
function doTest5_e(test_id)
@ -476,52 +480,56 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583
// credentials using the auth cache and cookies
var xhr = SpecialPowers.createSystemXHR();
xhr.withCredentials = true;
xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", false, "user 1", "password 1");
xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1");
xhr.send();
ok(xhr.status == 200, "Failed to set credentials in test 5.e");
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.e");
gEventSourceObj5_e = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc",
{ get withCredentials() { return true; } } );
ok(gEventSourceObj5_e.withCredentials, "Wrong withCredentials in test 5.e");
gEventSourceObj5_e = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc",
{ get withCredentials() { return true; } } );
ok(gEventSourceObj5_e.withCredentials, "Wrong withCredentials in test 5.e");
gEventSourceObj5_e.onmessage = function(e) {
ok(e.origin == "http://example.org", "Wrong Origin in test 5.e");
fn_onmessage(e);
gEventSourceObj5_e.onmessage = function(e) {
ok(e.origin == "http://example.org", "Wrong Origin in test 5.e");
fn_onmessage(e);
};
gEventSourceObj5_e.hits = [];
gEventSourceObj5_e.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_e.hits['fn_onmessage'] > 0, "Test 5.e failed");
gEventSourceObj5_e.close();
doTest5_f(test_id);
}, parseInt(5000*stress_factor));
};
gEventSourceObj5_e.hits = [];
gEventSourceObj5_e.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_e.hits['fn_onmessage'] > 0, "Test 5.e failed");
gEventSourceObj5_e.close();
doTest5_f(test_id);
}, parseInt(5000*stress_factor));
}
function doTest5_f(test_id)
{
var xhr = SpecialPowers.createSystemXHR();
xhr.withCredentials = true;
xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", false, "user 2", "password 2");
xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2");
xhr.send();
ok(xhr.status == 200, "Failed to set credentials in test 5.f");
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.f");
gEventSourceObj5_f = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc",
{ });
ok(!gEventSourceObj5_f.withCredentials, "Wrong withCredentials in test 5.f");
gEventSourceObj5_f = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc",
{ });
ok(!gEventSourceObj5_f.withCredentials, "Wrong withCredentials in test 5.f");
gEventSourceObj5_f.onmessage = function(e) {
ok(e.origin == "http://example.org", "Wrong Origin in test 5.f");
fn_onmessage(e);
gEventSourceObj5_f.onmessage = function(e) {
ok(e.origin == "http://example.org", "Wrong Origin in test 5.f");
fn_onmessage(e);
};
gEventSourceObj5_f.hits = [];
gEventSourceObj5_f.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_f.hits['fn_onmessage'] == 0, "Test 5.f failed");
gEventSourceObj5_f.close();
setTestHasFinished(test_id);
}, parseInt(3000*stress_factor));
};
gEventSourceObj5_f.hits = [];
gEventSourceObj5_f.hits['fn_onmessage'] = 0;
setTimeout(function() {
ok(gEventSourceObj5_f.hits['fn_onmessage'] == 0, "Test 5.f failed");
gEventSourceObj5_f.close();
setTestHasFinished(test_id);
}, parseInt(3000*stress_factor));
}
function doTest6(test_id)

View File

@ -86,14 +86,14 @@ function onWindowLoad() {
ok(document.iframeWasLoaded, "Loading resource via src-attribute");
for each (test in alltests) {
function runTest(test) {
var xhr = new XMLHttpRequest();
var method = "GET";
if (test.method != null) { method = test.method; }
xhr.open(method, test.url, false);
xhr.open(method, test.url);
xhr.withCredentials = test.withCredentials;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@ -104,11 +104,20 @@ function onWindowLoad() {
xhr.send();
} catch(e) {
}
var success = eval(xhr.status + test.status_check);
ok(success, test.error);
xhr.onloadend = function() {
var success = eval(xhr.status + test.status_check);
ok(success, test.error);
if (alltests.length == 0) {
SimpleTest.finish();
} else {
runTest(alltests.shift());
}
};
}
SimpleTest.finish();
runTest(alltests.shift());
}
SimpleTest.waitForExplicitFinish();

View File

@ -0,0 +1,21 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Test setting .responseType and .withCredentials is allowed
// in non-window non-Worker context
function run_test()
{
var xhr = Components.classes['@mozilla.org/xmlextras/xmlhttprequest;1'].
createInstance(Components.interfaces.nsIXMLHttpRequest);
xhr.open('GET', 'data:,', false);
var exceptionThrown = false;
try {
xhr.responseType = '';
xhr.withCredentials = false;
} catch (e) {
exceptionThrown = true;
}
do_check_eq(false, exceptionThrown);
}

View File

@ -7,4 +7,5 @@ tail =
[test_csputils.js]
[test_error_codes.js]
[test_thirdpartyutil.js]
[test_xhr_standalone.js]
[test_xmlserializer.js]