test_XHR_parameters.html 2.4 KB

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