// we test:
// 1) the EventSource behaviour
// 2) if the events are trusted
// 3) possible invalid eventsources
// 4) the close method when the object is just been used
// 5) access-control
// 6) the data parameter
// 7) delayed server responses
// --
var gTestsHaveFinished = [];
function setTestHasFinished(test_id)
{
if (gTestsHaveFinished[test_id]) {
return;
}
gTestsHaveFinished[test_id] = true;
for (var i=0; i < gTestsHaveFinished.length; ++i) {
if (!gTestsHaveFinished[i]) {
return;
}
}
SimpleTest.finish();
}
function runAllTests() {
// these tests run asynchronously, and they will take 8000 ms var all_tests = [
doTest1, doTest1_e, doTest1_f, doTest2, doTest3, doTest3_b, doTest3_c, doTest3_d,
doTest3_e, doTest3_f, doTest3_g, doTest3_h, doTest4, doTest4_b,
doTest5, doTest5_b, doTest5_c, doTest5_e, doTest6, doTest7
];
for (let test_id=0; test_id < all_tests.length; ++test_id) {
gTestsHaveFinished[test_id] = false; var fn = all_tests[test_id];
fn(test_id);
}
setTimeout(function() {
for (let test_id=0; test_id < all_tests.length; ++test_id) {
if (!gTestsHaveFinished[test_id]) {
ok(false, "Test " + test_id + " took too long");
setTestHasFinished(test_id);
}
}
}, 60000 * stress_factor); // all tests together are supposed to take less than 1 minute
}
function fn_onmessage(e) {
if (e.currentTarget == e.target && e.target.hits != null)
e.target.hits.fn_onmessage++;
}
function fn_event_listener_message(e) {
if (e.currentTarget == e.target && e.target.hits != null)
e.target.hits.fn_event_listener_message++;
}
function fn_other_event_name(e) {
if (e.currentTarget == e.target && e.target.hits != null)
e.target.hits.fn_other_event_name++;
}
var gEventSourceObj1 = null, gEventSourceObj1_e, gEventSourceObj1_f; var gEventSourceObj2 = null; var gEventSourceObj3_a = null, gEventSourceObj3_b = null,
gEventSourceObj3_c = null, gEventSourceObj3_d = null,
gEventSourceObj3_e = null, gEventSourceObj3_f = null,
gEventSourceObj3_g = null, gEventSourceObj3_h = null; var gEventSourceObj4_a = null, gEventSourceObj4_b = null; var gEventSourceObj5_a = null, gEventSourceObj5_b = null,
gEventSourceObj5_c = null, gEventSourceObj5_d = null,
gEventSourceObj5_e = null, gEventSourceObj5_f = null; var gEventSourceObj6 = null; var gEventSourceObj7 = null; var stress_factor; // used in the setTimeouts in order to help
// the test when running in slow machines
function hasBeenHitFor1And2(obj, min) {
if (obj.hits.fn_onmessage < min ||
obj.hits.fn_event_listener_message < min ||
obj.hits.fn_other_event_name < min)
return false;
return true;
}
// in order to test (1):
// a) if the EventSource constructor parameter is equal to its url attribute
// b) let its fn_onmessage, fn_event_listener_message, and fn_other_event_name functions listeners be hit four times each
// c) the close method (we expect readyState == CLOSED)
// d) the close method (we expect no message events anymore)
// e) use the default for withCredentials when passing dictionary arguments that don't explicitly set it
// f) if a 204 HTTP response closes (interrupts) connections. See bug 869432.
// the eventsources.res always use a retry of 0.5 second, so for four hits a timeout of 6 seconds is enough
setTimeout(function(){
bhits = hasBeenHitFor1And2(gEventSourceObj1, 4);
ok(bhits, "Test 1.b failed.");
function doTest1_e(test_id) {
try {
for (var options of [null, undefined, {}]) {
gEventSourceObj1_e = new EventSource("eventsource.resource", options);
is(gEventSourceObj1_e.withCredentials, false, "withCredentials should default to false");
gEventSourceObj1_e.close();
}
} catch (e) {
ok(false, "Test 1.e failed");
}
setTestHasFinished(test_id);
}
function doTest1_f(test_id) { var called_on_error = false;
gEventSourceObj1_f = new EventSource("file_bug869432.eventsource");
gEventSourceObj1_f.onopen = function(e) {
ok(false, "Test 1.f failed: onopen was called");
};
gEventSourceObj1_f.onmessage = function(e) {
ok(false, "Test 1.f failed: onmessage was called");
};
gEventSourceObj1_f.onerror = function(e) {
if (called_on_error) {
ok(false, "Test 1.f failed: onerror was called twice");
}
called_on_error = true;
ok(gEventSourceObj1_f.readyState == 2, "Test 1.f failed: onerror was called with readyState = " + gEventSourceObj1_f.readyState);
};
setTimeout(function() { // just to clean...
ok(called_on_error, "Test 1.f failed: onerror was not called");
setTestHasFinished(test_id);
}, parseInt(5000*stress_factor));
}
// in order to test (2)
// a) set a eventsource that give the dom events messages
// b) expect trusted events
function doTest2(test_id) { var func = function(e) {
ok(e.isTrusted, "Test 2 failed");
gEventSourceObj2.close();
};
gEventSourceObj2 = new EventSource("eventsource.resource");
gEventSourceObj2.onmessage = func;
setTimeout(function() { // just to clean...
gEventSourceObj2.close();
setTestHasFinished(test_id);
}, parseInt(5000*stress_factor));
}
// in order to test (3)
// a) XSite domain error test
// b) protocol file:// test
// c) protocol javascript: test
// d) wrong Content-Type test
// e) bad http response code test
// f) message eventsource without a data test
// g) DNS error
// h) EventSource which last message doesn't end with an empty line. See bug 710546
function doTest3_b(test_id) {
// currently no support yet for local files for b2g/Android mochitest, see bug 838726
if (navigator.appVersion.includes("Android") || SpecialPowers.Services.appinfo.name == "B2G") {
setTestHasFinished(test_id);
return;
}
var xhr = new XMLHttpRequest;
xhr.open("GET", "/dynamic/getMyDirectory.sjs", false);
xhr.send(); var basePath = xhr.responseText;
gEventSourceObj3_b = new EventSource("file://" + basePath + "eventsource.resource");
// in order to test (4)
// a) close the object when it is in use, which is being processed and that is expected
// to dispatch more eventlisteners
// b) remove an eventlistener in use
function fn_onmessage4_a(e)
{
if (e.data > gEventSourceObj4_a.lastData)
gEventSourceObj4_a.lastData = e.data;
if (e.data == 2)
gEventSourceObj4_a.close();
}
function fn_onmessage4_b(e)
{
if (e.data > gEventSourceObj4_b.lastData)
gEventSourceObj4_b.lastData = e.data;
if (e.data == 2)
gEventSourceObj4_b.removeEventListener('message', fn_onmessage4_b, true);
}
function doTest4(test_id) {
gEventSourceObj4_a = new EventSource("forRemoval.resource");
gEventSourceObj4_a.lastData = 0;
gEventSourceObj4_a.onmessage = fn_onmessage4_a;
// in order to test (5)
// a) valid access-control xsite request
// b) invalid access-control xsite request
// c) valid access-control xsite request on a restricted page with credentials
// d) valid access-control xsite request on a restricted page without credentials
// e) valid access-control xsite request on a restricted page when the parameter withCredentials is a getter
// f) valid access-control xsite request on a restricted page when the parameter withCredentials is missing
function doTest5_c(test_id)
{
// credentials using the auth cache var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
// also, test mixed mode UI
xhr.open("GET", "https://example.com/tests/dom/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1");
xhr.send();
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.c");
function doTest5_e(test_id)
{
// credentials using the auth cache var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
xhr.open("GET", "http://example.org/tests/dom/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1");
xhr.send();
xhr.onloadend = function() {
ok(xhr.status == 200, "Failed to set credentials in test 5.e");
function doTest()
{
// Allow all cookies, then run the actual test
SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]},
function() {
SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}],
doTestCallback);
});
}
function doTestCallback()
{
// we get a good stress_factor by testing 10 setTimeouts and some float
// arithmetic taking my machine as stress_factor==1 (time=589)
var begin_time = (new Date()).getTime();
var f = function() {
for (var j=0; j<f.i; ++j)
// eslint-disable-next-line no-eval
eval("Math.log(Math.atan(Math.sqrt(Math.pow(3.1415, 13.1415))/0.0007))");
if (f.i < 10) {
f.i++;
setTimeout(f, 10 + 10*f.i);
} else {
stress_factor = ((new Date()).getTime()-begin_time)*1/589;
stress_factor *= 1.50; // also, a margin of 50%
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.