1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_bug338583.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,665 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=338583 1.8 +--> 1.9 +<head> 1.10 + <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> 1.11 + <title>Test for Bug 338583</title> 1.12 + <script type="text/javascript" src="/MochiKit/MochiKit.js"></script> 1.13 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.14 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.15 + 1.16 +</head> 1.17 +<body bgColor=white> 1.18 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=338583">Mozilla Bug 338583</a> 1.19 +<p id="display"></p> 1.20 +<div id="content" style="display: none"> 1.21 + 1.22 +</div> 1.23 +<pre id="test"> 1.24 +<script class="testbody" type="text/javascript"> 1.25 +/** Tests for Bug 338583 **/ 1.26 + 1.27 +// we test: 1.28 +// 1) the EventSource behaviour 1.29 +// 2) if the events are trusted 1.30 +// 3) possible invalid eventsources 1.31 +// 4) the close method when the object is just been used 1.32 +// 5) access-control 1.33 +// 6) the data parameter 1.34 +// 7) delayed server responses 1.35 + 1.36 +// -- 1.37 + 1.38 + var gTestsHaveFinished = []; 1.39 + 1.40 + function setTestHasFinished(test_id) 1.41 + { 1.42 + if (gTestsHaveFinished[test_id]) { 1.43 + return; 1.44 + } 1.45 + 1.46 + gTestsHaveFinished[test_id] = true; 1.47 + for (var i=0; i < gTestsHaveFinished.length; ++i) { 1.48 + if (!gTestsHaveFinished[i]) { 1.49 + return; 1.50 + } 1.51 + } 1.52 + 1.53 + SimpleTest.finish(); 1.54 + } 1.55 + 1.56 + function runAllTests() { 1.57 + // these tests run asynchronously, and they will take 8000 ms 1.58 + var all_tests = [ 1.59 + doTest1, doTest1_e, doTest1_f, doTest2, doTest3, doTest3_b, doTest3_c, doTest3_d, 1.60 + doTest3_e, doTest3_f, doTest3_g, doTest3_h, doTest4, doTest4_b, 1.61 + doTest5, doTest5_b, doTest5_c, doTest5_e, doTest6, doTest7 1.62 + ]; 1.63 + 1.64 + for (var test_id=0; test_id < all_tests.length; ++test_id) { 1.65 + gTestsHaveFinished[test_id] = false; 1.66 + var fn = all_tests[test_id]; 1.67 + fn(test_id); 1.68 + } 1.69 + 1.70 + setTimeout(function() { 1.71 + for (var test_id=0; test_id < all_tests.length; ++test_id) { 1.72 + if (!gTestsHaveFinished[test_id]) { 1.73 + ok(false, "Test " + test_id + " took too long"); 1.74 + setTestHasFinished(test_id); 1.75 + } 1.76 + } 1.77 + }, 60000 * stress_factor); // all tests together are supposed to take less than 1 minute 1.78 + } 1.79 + 1.80 + function fn_onmessage(e) { 1.81 + if (e.currentTarget == e.target && e.target.hits != null) 1.82 + e.target.hits['fn_onmessage']++; 1.83 + } 1.84 + 1.85 + function fn_event_listener_message(e) { 1.86 + if (e.currentTarget == e.target && e.target.hits != null) 1.87 + e.target.hits['fn_event_listener_message']++; 1.88 + } 1.89 + 1.90 + function fn_other_event_name(e) { 1.91 + if (e.currentTarget == e.target && e.target.hits != null) 1.92 + e.target.hits['fn_other_event_name']++; 1.93 + } 1.94 + 1.95 + var gEventSourceObj1 = null, gEventSourceObj1_e, gEventSourceObj1_f; 1.96 + var gEventSourceObj2 = null; 1.97 + var gEventSourceObj3_a = null, gEventSourceObj3_b = null, 1.98 + gEventSourceObj3_c = null, gEventSourceObj3_d = null, 1.99 + gEventSourceObj3_e = null, gEventSourceObj3_f = null, 1.100 + gEventSourceObj3_g = null, gEventSourceObj3_h = null; 1.101 + var gEventSourceObj4_a = null, gEventSourceObj4_b = null; 1.102 + var gEventSourceObj5_a = null, gEventSourceObj5_b = null, 1.103 + gEventSourceObj5_c = null, gEventSourceObj5_d = null, 1.104 + gEventSourceObj5_e = null, gEventSourceObj5_f = null; 1.105 + var gEventSourceObj6 = null; 1.106 + var gEventSourceObj7 = null; 1.107 + var stress_factor; // used in the setTimeouts in order to help 1.108 + // the test when running in slow machines 1.109 + 1.110 + function hasBeenHitFor1And2(obj, min) { 1.111 + if (obj.hits['fn_onmessage'] < min || 1.112 + obj.hits['fn_event_listener_message'] < min || 1.113 + obj.hits['fn_other_event_name'] < min) 1.114 + return false; 1.115 + return true; 1.116 + } 1.117 + 1.118 +// in order to test (1): 1.119 +// a) if the EventSource constructor parameter is equal to its url attribute 1.120 +// b) let its fn_onmessage, fn_event_listener_message, and fn_other_event_name functions listeners be hit four times each 1.121 +// c) the close method (we expect readyState == CLOSED) 1.122 +// d) the close method (we expect no message events anymore) 1.123 +// e) use the default for withCredentials when passing dictionary arguments that don't explicitly set it 1.124 +// f) if a 204 HTTP response closes (interrupts) connections. See bug 869432. 1.125 + 1.126 + function doTest1(test_id) { 1.127 + gEventSourceObj1 = new EventSource("eventsource.resource"); 1.128 + ok(gEventSourceObj1.url == "http://mochi.test:8888/tests/content/base/test/eventsource.resource", "Test 1.a failed."); 1.129 + ok(gEventSourceObj1.readyState == 0 || gEventSourceObj1.readyState == 1, "Test 1.a failed."); 1.130 + 1.131 + doTest1_b(test_id); 1.132 + } 1.133 + 1.134 + function doTest1_b(test_id) { 1.135 + gEventSourceObj1.hits = []; 1.136 + gEventSourceObj1.hits['fn_onmessage'] = 0; 1.137 + gEventSourceObj1.onmessage = fn_onmessage; 1.138 + gEventSourceObj1.hits['fn_event_listener_message'] = 0; 1.139 + gEventSourceObj1.addEventListener('message', fn_event_listener_message, true); 1.140 + gEventSourceObj1.hits['fn_other_event_name'] = 0; 1.141 + gEventSourceObj1.addEventListener('other_event_name', fn_other_event_name, true); 1.142 + 1.143 + // the eventsources.res always use a retry of 0.5 second, so for four hits a timeout of 6 seconds is enough 1.144 + setTimeout(function(){ 1.145 + bhits = hasBeenHitFor1And2(gEventSourceObj1, 4); 1.146 + ok(bhits, "Test 1.b failed."); 1.147 + 1.148 + doTest1_c(test_id); 1.149 + }, parseInt(6000*stress_factor)); 1.150 + } 1.151 + 1.152 + function doTest1_c(test_id) { 1.153 + gEventSourceObj1.close(); 1.154 + ok(gEventSourceObj1.readyState == 2, "Test 1.c failed."); 1.155 + 1.156 + doTest1_d(test_id); 1.157 + } 1.158 + 1.159 + function doTest1_d(test_id) { 1.160 + gEventSourceObj1.hits['fn_onmessage'] = 0; 1.161 + gEventSourceObj1.hits['fn_event_listener_message'] = 0; 1.162 + gEventSourceObj1.hits['fn_other_event_name'] = 0; 1.163 + 1.164 + setTimeout(function(){ 1.165 + bhits = hasBeenHitFor1And2(gEventSourceObj1, 1); 1.166 + ok(!bhits, "Test 1.d failed."); 1.167 + gEventSourceObj1.close(); 1.168 + setTestHasFinished(test_id); 1.169 + }, parseInt(2000*stress_factor)); 1.170 + } 1.171 + 1.172 + function doTest1_e(test_id) { 1.173 + try { 1.174 + for (var options of [null, undefined, {}]) { 1.175 + gEventSourceObj1_e = new EventSource("eventsource.resource", options); 1.176 + is(gEventSourceObj1_e.withCredentials, false, "withCredentials should default to false"); 1.177 + gEventSourceObj1_e.close(); 1.178 + } 1.179 + } catch (e) { 1.180 + ok(false, "Test 1.e failed"); 1.181 + } 1.182 + setTestHasFinished(test_id); 1.183 + } 1.184 + 1.185 + function doTest1_f(test_id) { 1.186 + var called_on_error = false; 1.187 + 1.188 + gEventSourceObj1_f = new EventSource("file_bug869432.eventsource"); 1.189 + gEventSourceObj1_f.onopen = function(e) { 1.190 + ok(false, "Test 1.f failed: onopen was called"); 1.191 + }; 1.192 + gEventSourceObj1_f.onmessage = function(e) { 1.193 + ok(false, "Test 1.f failed: onmessage was called"); 1.194 + }; 1.195 + gEventSourceObj1_f.onerror = function(e) { 1.196 + if (called_on_error) { 1.197 + ok(false, "Test 1.f failed: onerror was called twice"); 1.198 + } 1.199 + called_on_error = true; 1.200 + ok(gEventSourceObj1_f.readyState == 2, "Test 1.f failed: onerror was called with readyState = " + gEventSourceObj1_f.readyState); 1.201 + }; 1.202 + 1.203 + setTimeout(function() { // just to clean... 1.204 + ok(called_on_error, "Test 1.f failed: onerror was not called"); 1.205 + setTestHasFinished(test_id); 1.206 + }, parseInt(5000*stress_factor)); 1.207 + } 1.208 + 1.209 +// in order to test (2) 1.210 +// a) set a eventsource that give the dom events messages 1.211 +// b) expect trusted events 1.212 + 1.213 + function doTest2(test_id) { 1.214 + var func = function(e) { 1.215 + ok(e.isTrusted, "Test 2 failed"); 1.216 + gEventSourceObj2.close(); 1.217 + }; 1.218 + 1.219 + gEventSourceObj2 = new EventSource("eventsource.resource"); 1.220 + gEventSourceObj2.onmessage = func; 1.221 + 1.222 + setTimeout(function() { // just to clean... 1.223 + gEventSourceObj2.close(); 1.224 + setTestHasFinished(test_id); 1.225 + }, parseInt(5000*stress_factor)); 1.226 + } 1.227 + 1.228 +// in order to test (3) 1.229 +// a) XSite domain error test 1.230 +// b) protocol file:// test 1.231 +// c) protocol javascript: test 1.232 +// d) wrong Content-Type test 1.233 +// e) bad http response code test 1.234 +// f) message eventsource without a data test 1.235 +// g) DNS error 1.236 +// h) EventSource which last message doesn't end with an empty line. See bug 710546 1.237 + 1.238 + function doTest3(test_id) { 1.239 + gEventSourceObj3_a = new EventSource("http://example.org/tests/content/base/test/eventsource.resource"); 1.240 + 1.241 + gEventSourceObj3_a.onmessage = fn_onmessage; 1.242 + gEventSourceObj3_a.hits = []; 1.243 + gEventSourceObj3_a.hits['fn_onmessage'] = 0; 1.244 + 1.245 + setTimeout(function() { 1.246 + ok(gEventSourceObj3_a.hits['fn_onmessage'] == 0, "Test 3.a failed"); 1.247 + gEventSourceObj3_a.close(); 1.248 + setTestHasFinished(test_id); 1.249 + }, parseInt(1500*stress_factor)); 1.250 + } 1.251 + 1.252 + function doTest3_b(test_id) { 1.253 + // currently no support yet for local files for b2g/Android mochitest, see bug 838726 1.254 + if (navigator.appVersion.indexOf("Android") != -1 || SpecialPowers.Services.appinfo.name == "B2G") { 1.255 + setTestHasFinished(test_id); 1.256 + return; 1.257 + } 1.258 + 1.259 + var xhr = new XMLHttpRequest; 1.260 + xhr.open("GET", "/dynamic/getMyDirectory.sjs", false); 1.261 + xhr.send(); 1.262 + var basePath = xhr.responseText; 1.263 + 1.264 + gEventSourceObj3_b = new EventSource("file://" + basePath + "eventsource.resource"); 1.265 + 1.266 + gEventSourceObj3_b.onmessage = fn_onmessage; 1.267 + gEventSourceObj3_b.hits = []; 1.268 + gEventSourceObj3_b.hits['fn_onmessage'] = 0; 1.269 + 1.270 + setTimeout(function() { 1.271 + ok(gEventSourceObj3_b.hits['fn_onmessage'] == 0, "Test 3.b failed"); 1.272 + gEventSourceObj3_b.close(); 1.273 + setTestHasFinished(test_id); 1.274 + }, parseInt(1500*stress_factor)); 1.275 + } 1.276 + 1.277 + function jsEvtSource() 1.278 + { 1.279 + return "event: message\n" + 1.280 + "data: 1\n\n"; 1.281 + } 1.282 + 1.283 + function doTest3_c(test_id) { 1.284 + gEventSourceObj3_c = new EventSource("javascript: return jsEvtSource()"); 1.285 + 1.286 + gEventSourceObj3_c.onmessage = fn_onmessage; 1.287 + gEventSourceObj3_c.hits = []; 1.288 + gEventSourceObj3_c.hits['fn_onmessage'] = 0; 1.289 + 1.290 + setTimeout(function() { 1.291 + ok(gEventSourceObj3_c.hits['fn_onmessage'] == 0, "Test 3.c failed"); 1.292 + gEventSourceObj3_c.close(); 1.293 + setTestHasFinished(test_id); 1.294 + }, parseInt(1500*stress_factor)); 1.295 + } 1.296 + 1.297 + function doTest3_d(test_id) { 1.298 + gEventSourceObj3_d = new EventSource("badContentType.eventsource"); 1.299 + 1.300 + gEventSourceObj3_d.onmessage = fn_onmessage; 1.301 + gEventSourceObj3_d.hits = []; 1.302 + gEventSourceObj3_d.hits['fn_onmessage'] = 0; 1.303 + 1.304 + setTimeout(function() { 1.305 + ok(gEventSourceObj3_d.hits['fn_onmessage'] == 0, "Test 3.d failed"); 1.306 + gEventSourceObj3_d.close(); 1.307 + setTestHasFinished(test_id); 1.308 + }, parseInt(1500*stress_factor)); 1.309 + } 1.310 + 1.311 + function doTest3_e(test_id) { 1.312 + gEventSourceObj3_e = new EventSource("badHTTPResponseCode.eventsource"); 1.313 + 1.314 + gEventSourceObj3_e.onmessage = fn_onmessage; 1.315 + gEventSourceObj3_e.hits = []; 1.316 + gEventSourceObj3_e.hits['fn_onmessage'] = 0; 1.317 + 1.318 + setTimeout(function() { 1.319 + ok(gEventSourceObj3_e.hits['fn_onmessage'] == 0, "Test 3.e failed"); 1.320 + gEventSourceObj3_e.close(); 1.321 + setTestHasFinished(test_id); 1.322 + }, parseInt(1500*stress_factor)); 1.323 + } 1.324 + 1.325 + function doTest3_f(test_id) { 1.326 + gEventSourceObj3_f = new EventSource("badMessageEvent.eventsource"); 1.327 + 1.328 + gEventSourceObj3_f.onmessage = fn_onmessage; 1.329 + gEventSourceObj3_f.hits = []; 1.330 + gEventSourceObj3_f.hits['fn_onmessage'] = 0; 1.331 + 1.332 + setTimeout(function() { 1.333 + ok(gEventSourceObj3_f.hits['fn_onmessage'] == 0, "Test 3.f failed"); 1.334 + gEventSourceObj3_f.close(); 1.335 + setTestHasFinished(test_id); 1.336 + }, parseInt(1500*stress_factor)); 1.337 + } 1.338 + 1.339 + function fnInvalidNCName() { 1.340 + fnInvalidNCName.hits++; 1.341 + } 1.342 + 1.343 + function doTest3_g(test_id) { 1.344 + gEventSourceObj3_g = new EventSource("http://hdfskjghsbg.jtiyoejowe.dafsgbhjab.com"); 1.345 + 1.346 + gEventSourceObj3_g.onmessage = fn_onmessage; 1.347 + gEventSourceObj3_g.hits = []; 1.348 + gEventSourceObj3_g.hits['fn_onmessage'] = 0; 1.349 + 1.350 + setTimeout(function() { 1.351 + ok(gEventSourceObj3_g.hits['fn_onmessage'] == 0, "Test 3.g failed"); 1.352 + gEventSourceObj3_g.close(); 1.353 + setTestHasFinished(test_id); 1.354 + }, parseInt(1500*stress_factor)); 1.355 + } 1.356 + 1.357 + function fnMessageListenerTest3h(e) { 1.358 + fnMessageListenerTest3h.msg_ok = (fnMessageListenerTest3h.msg_ok && e.data == "ok"); 1.359 + fnMessageListenerTest3h.id_ok = (fnMessageListenerTest3h.msg_ok && e.lastEventId == ""); 1.360 + } 1.361 + 1.362 + function doTest3_h(test_id) { 1.363 + gEventSourceObj3_h = new EventSource("badMessageEvent2.eventsource"); 1.364 + 1.365 + gEventSourceObj3_h.addEventListener('message event', fnMessageListenerTest3h, true); 1.366 + fnMessageListenerTest3h.msg_ok = true; 1.367 + fnMessageListenerTest3h.id_ok = true; 1.368 + 1.369 + gEventSourceObj3_h.onmessage = fn_onmessage; 1.370 + gEventSourceObj3_h.hits = []; 1.371 + gEventSourceObj3_h.hits['fn_onmessage'] = 0; 1.372 + 1.373 + setTimeout(function() { 1.374 + ok(gEventSourceObj3_h.hits['fn_onmessage'] > 1, "Test 3.h.1 failed"); 1.375 + if (gEventSourceObj3_h.hits['fn_onmessage'] > 1) { 1.376 + ok(fnMessageListenerTest3h.msg_ok, "Test 3.h.2 failed"); 1.377 + ok(fnMessageListenerTest3h.id_ok, "Test 3.h.3 failed"); 1.378 + } 1.379 + gEventSourceObj3_h.close(); 1.380 + setTestHasFinished(test_id); 1.381 + }, parseInt(3000*stress_factor)); 1.382 + } 1.383 + 1.384 +// in order to test (4) 1.385 +// a) close the object when it is in use, which is being processed and that is expected 1.386 +// to dispatch more eventlisteners 1.387 +// b) remove an eventlistener in use 1.388 + 1.389 + function fn_onmessage4_a(e) 1.390 + { 1.391 + if (e.data > gEventSourceObj4_a.lastData) 1.392 + gEventSourceObj4_a.lastData = e.data; 1.393 + if (e.data == 2) 1.394 + gEventSourceObj4_a.close(); 1.395 + } 1.396 + 1.397 + function fn_onmessage4_b(e) 1.398 + { 1.399 + if (e.data > gEventSourceObj4_b.lastData) 1.400 + gEventSourceObj4_b.lastData = e.data; 1.401 + if (e.data == 2) 1.402 + gEventSourceObj4_b.removeEventListener('message', fn_onmessage4_b, true); 1.403 + } 1.404 + 1.405 + function doTest4(test_id) { 1.406 + gEventSourceObj4_a = new EventSource("forRemoval.resource"); 1.407 + gEventSourceObj4_a.lastData = 0; 1.408 + gEventSourceObj4_a.onmessage = fn_onmessage4_a; 1.409 + 1.410 + setTimeout(function() { 1.411 + ok(gEventSourceObj4_a.lastData == 2, "Test 4.a failed"); 1.412 + gEventSourceObj4_a.close(); 1.413 + setTestHasFinished(test_id); 1.414 + }, parseInt(3000*stress_factor)); 1.415 + } 1.416 + 1.417 + function doTest4_b(test_id) 1.418 + { 1.419 + gEventSourceObj4_b = new EventSource("forRemoval.resource"); 1.420 + gEventSourceObj4_b.lastData = 0; 1.421 + gEventSourceObj4_b.addEventListener('message', fn_onmessage4_b, true); 1.422 + 1.423 + setTimeout(function() { 1.424 + ok(gEventSourceObj4_b.lastData == 2, "Test 4.b failed"); 1.425 + gEventSourceObj4_b.close(); 1.426 + setTestHasFinished(test_id); 1.427 + }, parseInt(3000*stress_factor)); 1.428 + } 1.429 + 1.430 +// in order to test (5) 1.431 +// a) valid access-control xsite request 1.432 +// b) invalid access-control xsite request 1.433 +// c) valid access-control xsite request on a restricted page with credentials 1.434 +// d) valid access-control xsite request on a restricted page without credentials 1.435 +// e) valid access-control xsite request on a restricted page when the parameter withCredentials is a getter 1.436 +// f) valid access-control xsite request on a restricted page when the parameter withCredentials is missing 1.437 + 1.438 + function doTest5(test_id) 1.439 + { 1.440 + gEventSourceObj5_a = new EventSource("http://example.org/tests/content/base/test/accesscontrol.resource"); 1.441 + 1.442 + gEventSourceObj5_a.onmessage = fn_onmessage; 1.443 + gEventSourceObj5_a.hits = []; 1.444 + gEventSourceObj5_a.hits['fn_onmessage'] = 0; 1.445 + 1.446 + setTimeout(function() { 1.447 + ok(gEventSourceObj5_a.hits['fn_onmessage'] != 0, "Test 5.a failed"); 1.448 + gEventSourceObj5_a.close(); 1.449 + setTestHasFinished(test_id); 1.450 + }, parseInt(3000*stress_factor)); 1.451 + } 1.452 + 1.453 + function doTest5_b(test_id) 1.454 + { 1.455 + gEventSourceObj5_b = new EventSource("http://example.org/tests/content/base/test/invalid_accesscontrol.resource"); 1.456 + 1.457 + gEventSourceObj5_b.onmessage = fn_onmessage; 1.458 + gEventSourceObj5_b.hits = []; 1.459 + gEventSourceObj5_b.hits['fn_onmessage'] = 0; 1.460 + 1.461 + setTimeout(function() { 1.462 + ok(gEventSourceObj5_b.hits['fn_onmessage'] == 0, "Test 5.b failed"); 1.463 + gEventSourceObj5_b.close(); 1.464 + setTestHasFinished(test_id); 1.465 + }, parseInt(3000*stress_factor)); 1.466 + } 1.467 + 1.468 + function doTest5_c(test_id) 1.469 + { 1.470 + // credentials using the auth cache 1.471 + var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); 1.472 + xhr.withCredentials = true; 1.473 + // also, test mixed mode UI 1.474 + xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1"); 1.475 + xhr.send(); 1.476 + xhr.onloadend = function() { 1.477 + ok(xhr.status == 200, "Failed to set credentials in test 5.c"); 1.478 + 1.479 + gEventSourceObj5_c = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc", 1.480 + { withCredentials: true } ); 1.481 + ok(gEventSourceObj5_c.withCredentials, "Wrong withCredentials in test 5.c"); 1.482 + 1.483 + gEventSourceObj5_c.onmessage = function(e) { 1.484 + ok(e.origin == "https://example.com", "Wrong Origin in test 5.c"); 1.485 + fn_onmessage(e); 1.486 + }; 1.487 + gEventSourceObj5_c.hits = []; 1.488 + gEventSourceObj5_c.hits['fn_onmessage'] = 0; 1.489 + 1.490 + setTimeout(function() { 1.491 + ok(gEventSourceObj5_c.hits['fn_onmessage'] > 0, "Test 5.c failed"); 1.492 + gEventSourceObj5_c.close(); 1.493 + doTest5_d(test_id); 1.494 + }, parseInt(3000*stress_factor)); 1.495 + }; 1.496 + } 1.497 + 1.498 + function doTest5_d(test_id) 1.499 + { 1.500 + var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); 1.501 + xhr.withCredentials = true; 1.502 + xhr.open("GET", "https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2"); 1.503 + xhr.send(); 1.504 + xhr.onloadend = function() { 1.505 + ok(xhr.status == 200, "Failed to set credentials in test 5.d"); 1.506 + 1.507 + gEventSourceObj5_d = new EventSource("https://example.com/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc"); 1.508 + ok(!gEventSourceObj5_d.withCredentials, "Wrong withCredentials in test 5.d"); 1.509 + 1.510 + gEventSourceObj5_d.onmessage = function(e) { 1.511 + ok(e.origin == "https://example.com", "Wrong Origin in test 5.d"); 1.512 + fn_onmessage(e); 1.513 + }; 1.514 + gEventSourceObj5_d.hits = []; 1.515 + gEventSourceObj5_d.hits['fn_onmessage'] = 0; 1.516 + 1.517 + setTimeout(function() { 1.518 + ok(gEventSourceObj5_d.hits['fn_onmessage'] == 0, "Test 5.d failed"); 1.519 + gEventSourceObj5_d.close(); 1.520 + setTestHasFinished(test_id); 1.521 + }, parseInt(3000*stress_factor)); 1.522 + }; 1.523 + } 1.524 + 1.525 + function doTest5_e(test_id) 1.526 + { 1.527 + // credentials using the auth cache 1.528 + var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); 1.529 + xhr.withCredentials = true; 1.530 + xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_xhr", true, "user 1", "password 1"); 1.531 + xhr.send(); 1.532 + xhr.onloadend = function() { 1.533 + ok(xhr.status == 200, "Failed to set credentials in test 5.e"); 1.534 + 1.535 + gEventSourceObj5_e = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user1_evtsrc", 1.536 + { get withCredentials() { return true; } } ); 1.537 + ok(gEventSourceObj5_e.withCredentials, "Wrong withCredentials in test 5.e"); 1.538 + 1.539 + gEventSourceObj5_e.onmessage = function(e) { 1.540 + ok(e.origin == "http://example.org", "Wrong Origin in test 5.e"); 1.541 + fn_onmessage(e); 1.542 + }; 1.543 + gEventSourceObj5_e.hits = []; 1.544 + gEventSourceObj5_e.hits['fn_onmessage'] = 0; 1.545 + 1.546 + setTimeout(function() { 1.547 + ok(gEventSourceObj5_e.hits['fn_onmessage'] > 0, "Test 5.e failed"); 1.548 + gEventSourceObj5_e.close(); 1.549 + doTest5_f(test_id); 1.550 + }, parseInt(5000*stress_factor)); 1.551 + }; 1.552 + } 1.553 + 1.554 + function doTest5_f(test_id) 1.555 + { 1.556 + var xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true}); 1.557 + xhr.withCredentials = true; 1.558 + xhr.open("GET", "http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_xhr", true, "user 2", "password 2"); 1.559 + xhr.send(); 1.560 + xhr.onloadend = function() { 1.561 + ok(xhr.status == 200, "Failed to set credentials in test 5.f"); 1.562 + 1.563 + gEventSourceObj5_f = new EventSource("http://example.org/tests/content/base/test/file_restrictedEventSource.sjs?test=user2_evtsrc", 1.564 + { }); 1.565 + ok(!gEventSourceObj5_f.withCredentials, "Wrong withCredentials in test 5.f"); 1.566 + 1.567 + gEventSourceObj5_f.onmessage = function(e) { 1.568 + ok(e.origin == "http://example.org", "Wrong Origin in test 5.f"); 1.569 + fn_onmessage(e); 1.570 + }; 1.571 + gEventSourceObj5_f.hits = []; 1.572 + gEventSourceObj5_f.hits['fn_onmessage'] = 0; 1.573 + 1.574 + setTimeout(function() { 1.575 + ok(gEventSourceObj5_f.hits['fn_onmessage'] == 0, "Test 5.f failed"); 1.576 + gEventSourceObj5_f.close(); 1.577 + setTestHasFinished(test_id); 1.578 + }, parseInt(3000*stress_factor)); 1.579 + }; 1.580 + } 1.581 + 1.582 + function doTest6(test_id) 1.583 + { 1.584 + gEventSourceObj6 = new EventSource("somedatas.resource"); 1.585 + var fn_somedata = function(e) { 1.586 + if (fn_somedata.expected == 0) { 1.587 + ok(e.data == "123456789\n123456789123456789\n123456789123456789123456789123456789\n 123456789123456789123456789123456789123456789123456789123456789123456789\nçãá\"\'@`~Ý Ḿyyyy", 1.588 + "Test 6.a failed"); 1.589 + } else if (fn_somedata.expected == 1) { 1.590 + ok(e.data == " :xxabcdefghij\nçãá\"\'@`~Ý Ḿyyyy : zz", 1.591 + "Test 6.b failed"); 1.592 + gEventSourceObj6.close(); 1.593 + } else { 1.594 + ok(false, "Test 6 failed (unexpected message event)"); 1.595 + } 1.596 + fn_somedata.expected++; 1.597 + } 1.598 + fn_somedata.expected = 0; 1.599 + gEventSourceObj6.onmessage = fn_somedata; 1.600 + 1.601 + setTimeout(function() { 1.602 + gEventSourceObj6.close(); 1.603 + setTestHasFinished(test_id); 1.604 + }, parseInt(2500*stress_factor)); 1.605 + } 1.606 + 1.607 + function doTest7(test_id) 1.608 + { 1.609 + gEventSourceObj7 = new EventSource("delayedServerEvents.sjs"); 1.610 + gEventSourceObj7.msg_received = []; 1.611 + gEventSourceObj7.onmessage = function(e) 1.612 + { 1.613 + e.target.msg_received.push(e.data); 1.614 + } 1.615 + 1.616 + setTimeout(function() { 1.617 + gEventSourceObj7.close(); 1.618 + 1.619 + ok(gEventSourceObj7.msg_received[0] == "" && 1.620 + gEventSourceObj7.msg_received[1] == "delayed1" && 1.621 + gEventSourceObj7.msg_received[2] == "delayed2", "Test 7 failed"); 1.622 + 1.623 + document.getElementById('waitSpan').innerHTML = ''; 1.624 + setTestHasFinished(test_id); 1.625 + }, parseInt(8000*stress_factor)); 1.626 + } 1.627 + 1.628 + function doTest() 1.629 + { 1.630 + // Allow all cookies, then run the actual test 1.631 + SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0], ["dom.server-events.enabled", true]]}, function() { SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], doTestCallback);}); 1.632 + } 1.633 + 1.634 + function doTestCallback() 1.635 + { 1.636 + 1.637 + // we get a good stress_factor by testing 10 setTimeouts and some float 1.638 + // arithmetic taking my machine as stress_factor==1 (time=589) 1.639 + 1.640 + var begin_time = (new Date()).getTime(); 1.641 + 1.642 + var f = function() { 1.643 + for (var j=0; j<f.i; ++j) 1.644 + eval("Math.log(Math.atan(Math.sqrt(Math.pow(3.1415, 13.1415))/0.0007))"); 1.645 + if (f.i < 10) { 1.646 + f.i++; 1.647 + setTimeout(f, 10 + 10*f.i); 1.648 + } else { 1.649 + stress_factor = ((new Date()).getTime()-begin_time)*1/589; 1.650 + stress_factor *= 1.10; // also, a margin of 10% 1.651 + 1.652 + runAllTests(); 1.653 + } 1.654 + } 1.655 + f.i = 0; 1.656 + 1.657 + setTimeout(f, 10); 1.658 + } 1.659 + 1.660 + SimpleTest.waitForExplicitFinish(); 1.661 + addLoadEvent(doTest); 1.662 + 1.663 +</script> 1.664 +</pre> 1.665 + <span id=waitSpan>Wait please...</span> 1.666 +</body> 1.667 +</html> 1.668 +