test_XHR_anon.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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="setup();">
  10. <p id="display">
  11. <iframe id="loader"></iframe>
  12. </p>
  13. <div id="content" style="display: none">
  14. </div>
  15. <pre id="test">
  16. <script class="testbody" type="application/javascript;version=1.8">
  17. // An XHR with the anon flag set will not send cookie and auth information.
  18. const TEST_URL = "http://example.com/tests/dom/xhr/tests/file_XHR_anon.sjs";
  19. document.cookie = "foo=bar";
  20. let am = {
  21. authMgr: null,
  22. init: function() {
  23. this.authMgr = SpecialPowers.Cc["@mozilla.org/network/http-auth-manager;1"]
  24. .getService(SpecialPowers.Ci.nsIHttpAuthManager)
  25. },
  26. addIdentity: function() {
  27. this.authMgr.setAuthIdentity("http", "example.com", -1, "basic", "testrealm",
  28. "", "example.com", "user1", "password1");
  29. },
  30. tearDown: function() {
  31. this.authMgr.clearAll();
  32. },
  33. }
  34. var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];
  35. function runTests() {
  36. if (!tests.length) {
  37. am.tearDown();
  38. // Resetting the cookie.
  39. document.cookie = "foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
  40. SimpleTest.finish();
  41. return;
  42. }
  43. var test = tests.shift();
  44. test();
  45. }
  46. function test1() {
  47. am.addIdentity();
  48. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  49. is(xhr.mozAnon, true, "test1: .mozAnon == true");
  50. xhr.open("GET", TEST_URL);
  51. xhr.onload = function onload() {
  52. is(xhr.status, 200, "test1: " + xhr.responseText);
  53. am.tearDown();
  54. runTests();
  55. };
  56. xhr.onerror = function onerror() {
  57. ok(false, "Got an error event!");
  58. am.tearDown();
  59. runTests();
  60. }
  61. xhr.send();
  62. }
  63. function test2() {
  64. am.addIdentity();
  65. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  66. is(xhr.mozAnon, true, "test2: .mozAnon == true");
  67. xhr.open("GET", TEST_URL + "?expectAuth=true", true,
  68. "user2name", "pass2word");
  69. xhr.onload = function onload() {
  70. is(xhr.status, 200, "test2: " + xhr.responseText);
  71. let response = JSON.parse(xhr.responseText);
  72. is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
  73. am.tearDown();
  74. runTests();
  75. };
  76. xhr.onerror = function onerror() {
  77. ok(false, "Got an error event!");
  78. am.tearDown();
  79. runTests();
  80. }
  81. xhr.send();
  82. }
  83. function test2a() {
  84. am.addIdentity();
  85. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  86. is(xhr.mozAnon, true, "test2: .mozAnon == true");
  87. xhr.open("GET", TEST_URL + "?expectAuth=true", true,
  88. "user1", "pass2word");
  89. xhr.onload = function onload() {
  90. is(xhr.status, 200, "test2: " + xhr.responseText);
  91. let response = JSON.parse(xhr.responseText);
  92. is(response.authorization, "Basic dXNlcjE6cGFzczJ3b3Jk");
  93. am.tearDown();
  94. runTests();
  95. };
  96. xhr.onerror = function onerror() {
  97. ok(false, "Got an error event!");
  98. am.tearDown();
  99. runTests();
  100. }
  101. xhr.send();
  102. }
  103. function test3() {
  104. am.addIdentity();
  105. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  106. is(xhr.mozAnon, true, "test3: .mozAnon == true");
  107. xhr.open("GET", TEST_URL + "?expectAuth=true", true);
  108. xhr.onload = function onload() {
  109. is(xhr.status, 401, "test3: " + xhr.responseText);
  110. am.tearDown();
  111. runTests();
  112. };
  113. xhr.onerror = function onerror() {
  114. ok(false, "Got an error event!");
  115. am.tearDown();
  116. runTests();
  117. }
  118. xhr.send();
  119. }
  120. function test4() {
  121. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  122. is(xhr.mozAnon, true, "test4: .mozAnon == true");
  123. xhr.open("GET", TEST_URL + "?expectAuth=true", true);
  124. xhr.onload = function onload() {
  125. is(xhr.status, 401, "test4: " + xhr.responseText);
  126. runTests();
  127. };
  128. xhr.onerror = function onerror() {
  129. ok(false, "Got an error event!");
  130. runTests();
  131. }
  132. xhr.send();
  133. }
  134. function test5() {
  135. let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  136. is(xhr.mozAnon, true, "test5: .mozAnon == true");
  137. xhr.open("GET", TEST_URL + "?expectAuth=true", true,
  138. "user2name", "pass2word");
  139. xhr.onload = function onload() {
  140. is(xhr.status, 200, "test5: " + xhr.responseText);
  141. let response = JSON.parse(xhr.responseText);
  142. is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
  143. runTests();
  144. };
  145. xhr.onerror = function onerror() {
  146. ok(false, "Got an error event!");
  147. runTests();
  148. }
  149. xhr.send();
  150. }
  151. function setup() {
  152. am.init();
  153. SimpleTest.waitForExplicitFinish();
  154. SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);
  155. }
  156. </script>
  157. </pre>
  158. </body>
  159. </html>