test_XHR_parameters.html (2409B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test for XMLHttpRequest with system privileges</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body onload="runTests();"> 10 <p id="display"> 11 </p> 12 <div id="content" style="display: none"> 13 14 </div> 15 <pre id="test"> 16 <script class="testbody" type="application/javascript"> 17 18 function runTests() { 19 SimpleTest.waitForExplicitFinish(); 20 21 let validParameters = [ 22 undefined, 23 null, 24 {}, 25 {mozSystem: ""}, 26 {mozSystem: 0}, 27 {mozAnon: 1}, 28 {mozAnon: []}, 29 {get mozAnon() { return true; }}, 30 0, 31 7, 32 Math.PI, 33 "string", 34 true, 35 false, 36 ]; 37 38 let invalidParameters = [ 39 {get mozSystem() { throw new Error("Bla"); } }, 40 ]; 41 42 let havePrivileges = false; 43 44 function testValidParameter(value) { 45 let xhr; 46 try { 47 xhr = new XMLHttpRequest(value); 48 } catch (ex) { 49 ok(false, "Got unexpected exception: " + ex); 50 return; 51 } 52 ok(xhr instanceof XMLHttpRequest, "passed " + JSON.stringify(value)); 53 54 // If the page doesnt have privileges to create a system XHR, 55 // this flag will always be false no matter what is passed. 56 let expectedAnon = Boolean(value && value.mozAnon); 57 let expectedSystem = false; 58 if (havePrivileges) { 59 expectedSystem = Boolean(value && value.mozSystem); 60 } 61 is(xhr.mozAnon, expectedAnon, "testing mozAnon"); 62 is(xhr.mozSystem, expectedSystem, "testing mozSystem"); 63 } 64 65 function testInvalidParameter(value) { 66 let expectedError; 67 try { 68 new XMLHttpRequest(value); 69 ok(false, "invalid parameter did not cause exception: " + 70 JSON.stringify(value)); 71 } catch (ex) { 72 expectedError = ex; 73 } 74 ok(expectedError, "invalid parameter raised exception as expected: " + 75 JSON.stringify(expectedError)) 76 } 77 78 // Run the tests once without API privileges... 79 validParameters.forEach(testValidParameter); 80 invalidParameters.forEach(testInvalidParameter); 81 82 // ...and once with privileges. 83 havePrivileges = true; 84 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], function() { 85 validParameters.forEach(testValidParameter); 86 invalidParameters.forEach(testInvalidParameter); 87 88 SimpleTest.finish(); 89 }); 90 } 91 92 </script> 93 </pre> 94 </body> 95 </html>