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