content/base/test/test_XHR_anon.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_XHR_anon.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,177 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <title>Test for XMLHttpRequest with system privileges</title>
     1.9 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.10 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +</head>
    1.12 +<body onload="setup();">
    1.13 +<p id="display">
    1.14 +<iframe id="loader"></iframe>
    1.15 +</p>
    1.16 +<div id="content" style="display: none">
    1.17 +  
    1.18 +</div>
    1.19 +<pre id="test">
    1.20 +<script class="testbody" type="application/javascript;version=1.8">
    1.21 +
    1.22 +// An XHR with the anon flag set will not send cookie and auth information.
    1.23 +const TEST_URL = "http://example.com/tests/content/base/test/file_XHR_anon.sjs";
    1.24 +document.cookie = "foo=bar";
    1.25 +
    1.26 +let am = {
    1.27 +  authMgr: null,
    1.28 +
    1.29 +  init: function() {
    1.30 +    this.authMgr = SpecialPowers.Cc["@mozilla.org/network/http-auth-manager;1"]
    1.31 +                                .getService(SpecialPowers.Ci.nsIHttpAuthManager)
    1.32 +  },
    1.33 +
    1.34 +  addIdentity: function() {
    1.35 +    this.authMgr.setAuthIdentity("http", "example.com", -1, "basic", "testrealm",
    1.36 +                                 "", "example.com", "user1", "password1");
    1.37 +  },
    1.38 +
    1.39 +  tearDown: function() {
    1.40 +    this.authMgr.clearAll();
    1.41 +  },
    1.42 +}
    1.43 +
    1.44 +var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];
    1.45 +
    1.46 +function runTests() {
    1.47 +  if (!tests.length) {
    1.48 +    am.tearDown();
    1.49 +    SimpleTest.finish();
    1.50 +    return;
    1.51 +  }
    1.52 +
    1.53 +  var test = tests.shift();
    1.54 +  test();
    1.55 +}
    1.56 +
    1.57 +function test1() {
    1.58 +  am.addIdentity();
    1.59 +
    1.60 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
    1.61 +  is(xhr.mozAnon, true, "test1: .mozAnon == true");
    1.62 +  xhr.open("GET", TEST_URL);
    1.63 +  xhr.onload = function onload() {
    1.64 +    is(xhr.status, 200, "test1: " + xhr.responseText);
    1.65 +    am.tearDown();
    1.66 +    runTests();
    1.67 +  };
    1.68 +  xhr.onerror = function onerror() {
    1.69 +    ok(false, "Got an error event!");
    1.70 +    am.tearDown();
    1.71 +    runTests();
    1.72 +  }
    1.73 +  xhr.send();
    1.74 +}
    1.75 +
    1.76 +function test2() {
    1.77 +  am.addIdentity();
    1.78 +
    1.79 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
    1.80 +  is(xhr.mozAnon, true, "test2: .mozAnon == true");
    1.81 +  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
    1.82 +           "user2name", "pass2word");
    1.83 +  xhr.onload = function onload() {
    1.84 +    is(xhr.status, 200, "test2: " + xhr.responseText);
    1.85 +    let response = JSON.parse(xhr.responseText);
    1.86 +    is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
    1.87 +    am.tearDown();
    1.88 +    runTests();
    1.89 +  };
    1.90 +  xhr.onerror = function onerror() {
    1.91 +    ok(false, "Got an error event!");
    1.92 +    am.tearDown();
    1.93 +    runTests();
    1.94 +  }
    1.95 +  xhr.send();
    1.96 +}
    1.97 +
    1.98 +function test2a() {
    1.99 +  am.addIdentity();
   1.100 +
   1.101 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
   1.102 +  is(xhr.mozAnon, true, "test2: .mozAnon == true");
   1.103 +  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
   1.104 +           "user1", "pass2word");
   1.105 +  xhr.onload = function onload() {
   1.106 +    is(xhr.status, 200, "test2: " + xhr.responseText);
   1.107 +    let response = JSON.parse(xhr.responseText);
   1.108 +    is(response.authorization, "Basic dXNlcjE6cGFzczJ3b3Jk");
   1.109 +    am.tearDown();
   1.110 +    runTests();
   1.111 +  };
   1.112 +  xhr.onerror = function onerror() {
   1.113 +    ok(false, "Got an error event!");
   1.114 +    am.tearDown();
   1.115 +    runTests();
   1.116 +  }
   1.117 +  xhr.send();
   1.118 +}
   1.119 +
   1.120 +function test3() {
   1.121 +  am.addIdentity();
   1.122 +
   1.123 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
   1.124 +  is(xhr.mozAnon, true, "test3: .mozAnon == true");
   1.125 +  xhr.open("GET", TEST_URL + "?expectAuth=true", true);
   1.126 +  xhr.onload = function onload() {
   1.127 +    is(xhr.status, 401, "test3: " + xhr.responseText);
   1.128 +    am.tearDown();
   1.129 +    runTests();
   1.130 +  };
   1.131 +  xhr.onerror = function onerror() {
   1.132 +    ok(false, "Got an error event!");
   1.133 +    am.tearDown();
   1.134 +    runTests();
   1.135 +  }
   1.136 +  xhr.send();
   1.137 +}
   1.138 +
   1.139 +function test4() {
   1.140 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
   1.141 +  is(xhr.mozAnon, true, "test4: .mozAnon == true");
   1.142 +  xhr.open("GET", TEST_URL + "?expectAuth=true", true);
   1.143 +  xhr.onload = function onload() {
   1.144 +    is(xhr.status, 401, "test4: " + xhr.responseText);
   1.145 +    runTests();
   1.146 +  };
   1.147 +  xhr.onerror = function onerror() {
   1.148 +    ok(false, "Got an error event!");
   1.149 +    runTests();
   1.150 +  }
   1.151 +  xhr.send();
   1.152 +}
   1.153 +
   1.154 +function test5() {
   1.155 +  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
   1.156 +  is(xhr.mozAnon, true, "test5: .mozAnon == true");
   1.157 +  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
   1.158 +           "user2name", "pass2word");
   1.159 +  xhr.onload = function onload() {
   1.160 +    is(xhr.status, 200, "test5: " + xhr.responseText);
   1.161 +    let response = JSON.parse(xhr.responseText);
   1.162 +    is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
   1.163 +    runTests();
   1.164 +  };
   1.165 +  xhr.onerror = function onerror() {
   1.166 +    ok(false, "Got an error event!");
   1.167 +    runTests();
   1.168 +  }
   1.169 +  xhr.send();
   1.170 +}
   1.171 +
   1.172 +function setup() {
   1.173 +  am.init();
   1.174 +  SimpleTest.waitForExplicitFinish();
   1.175 +  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);
   1.176 +}
   1.177 +</script>
   1.178 +</pre>
   1.179 +</body>
   1.180 +</html>

mercurial