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