Bug 1708422: part 14) Extend documentation of `mozInlineSpellChecker::SpellCheckerTim...
[gecko.git] / dom / xhr / tests / test_XHR_anon.html
blob6747b6a918b96363df66cf8ad63578671a58b287
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">
15 </div>
16 <pre id="test">
17 <script class="testbody" type="application/javascript">
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";
23 let am = {
24 authMgr: null,
26 init() {
27 this.authMgr = SpecialPowers.Cc["@mozilla.org/network/http-auth-manager;1"]
28 .getService(SpecialPowers.Ci.nsIHttpAuthManager)
31 addIdentity() {
32 this.authMgr.setAuthIdentity("http", "example.com", -1, "basic", "testrealm",
33 "", "example.com", "user1", "password1");
36 tearDown() {
37 this.authMgr.clearAll();
41 var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];
43 function runTests() {
44 if (!tests.length) {
45 am.tearDown();
47 // Resetting the cookie.
48 document.cookie = "foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
49 SimpleTest.finish();
50 return;
53 var test = tests.shift();
54 test();
57 function test1() {
58 am.addIdentity();
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();
68 xhr.onerror = function onerror() {
69 ok(false, "Got an error event!");
70 am.tearDown();
71 runTests();
73 xhr.send();
76 function test2() {
77 am.addIdentity();
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();
90 xhr.onerror = function onerror() {
91 ok(false, "Got an error event!");
92 am.tearDown();
93 runTests();
95 xhr.send();
98 function test2a() {
99 am.addIdentity();
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();
112 xhr.onerror = function onerror() {
113 ok(false, "Got an error event!");
114 am.tearDown();
115 runTests();
117 xhr.send();
120 function test3() {
121 am.addIdentity();
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();
131 xhr.onerror = function onerror() {
132 ok(false, "Got an error event!");
133 am.tearDown();
134 runTests();
136 xhr.send();
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();
147 xhr.onerror = function onerror() {
148 ok(false, "Got an error event!");
149 runTests();
151 xhr.send();
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();
165 xhr.onerror = function onerror() {
166 ok(false, "Got an error event!");
167 runTests();
169 xhr.send();
172 function setup() {
173 am.init();
174 SimpleTest.waitForExplicitFinish();
175 SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);
177 </script>
178 </pre>
179 </body>
180 </html>