test_worker_xhr_parameters.js (1854B)
1 function ok(what, msg) { 2 postMessage({ event: msg, test: "ok", a: what }); 3 } 4 5 function is(a, b, msg) { 6 postMessage({ event: msg, test: "is", a, b }); 7 } 8 9 // This is a copy of dom/xhr/tests/test_XHR_parameters.js 10 var validParameters = [ 11 undefined, 12 null, 13 {}, 14 { mozSystem: "" }, 15 { mozSystem: 0 }, 16 { mozAnon: 1 }, 17 { mozAnon: [] }, 18 { 19 get mozAnon() { 20 return true; 21 }, 22 }, 23 0, 24 7, 25 Math.PI, 26 "string", 27 true, 28 false, 29 ]; 30 31 var invalidParameters = [ 32 { 33 get mozSystem() { 34 throw new Error("Bla"); 35 }, 36 }, 37 ]; 38 39 function testParameters(havePrivileges) { 40 function testValidParameter(value) { 41 var xhr; 42 try { 43 xhr = new XMLHttpRequest(value); 44 } catch (ex) { 45 ok(false, "Got unexpected exception: " + ex); 46 return; 47 } 48 ok(!!xhr, "passed " + JSON.stringify(value)); 49 50 // If the page doesnt have privileges to create a system or anon XHR, 51 // these flags will always be false no matter what is passed. 52 var expectedAnon = false; 53 var expectedSystem = false; 54 if (havePrivileges) { 55 expectedAnon = Boolean(value && value.mozAnon); 56 expectedSystem = Boolean(value && value.mozSystem); 57 } 58 is(xhr.mozAnon, expectedAnon, "testing mozAnon"); 59 is(xhr.mozSystem, expectedSystem, "testing mozSystem"); 60 } 61 62 function testInvalidParameter(value) { 63 try { 64 new XMLHttpRequest(value); 65 ok( 66 false, 67 "invalid parameter did not cause exception: " + JSON.stringify(value) 68 ); 69 } catch (ex) { 70 ok( 71 true, 72 "invalid parameter raised exception as expected: " + JSON.stringify(ex) 73 ); 74 } 75 } 76 77 validParameters.forEach(testValidParameter); 78 invalidParameters.forEach(testInvalidParameter); 79 } 80 81 self.onmessage = function onmessage(event) { 82 testParameters(event.data); 83 postMessage({ test: "finish" }); 84 };