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