tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_1331680_xhr.html (2660B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 -->
      5 <head>
      6  <title>Cookie changes from XHR requests are observed in content processes.</title>
      7  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      9 
     10 <script type="text/javascript">
     11 SimpleTest.waitForExplicitFinish();
     12 
     13 const XHR_COOKIE_NAMES = ["xhr1", "xhr2"];
     14 
     15 var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));
     16 gScript.addMessageListener("cookieName", confirmCookieName);
     17 gScript.addMessageListener("removeObserver:return", finishTest);
     18 gScript.sendAsyncMessage('createObserver');
     19 
     20 // Confirm the notify which represents the cookie is updating.
     21 var testsNum = 0;
     22 function confirmCookieName(name) {
     23  testsNum++;
     24  switch(testsNum) {
     25    case 1:
     26      is(name, "xhr1=xhr_val1", "The cookie which names " + name + " is update to db");
     27      break;
     28    case 2:
     29      is(document.cookie, "xhr1=xhr_val1", "Confirm the cookie string");
     30      for (var i = 0; i < XHR_COOKIE_NAMES.length; i++) {
     31        document.cookie = XHR_COOKIE_NAMES[i] + "=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
     32      }
     33      break;
     34    case 3:
     35      is(document.cookie, "", "Confirm the cookie string");
     36      gScript.sendAsyncMessage('removeObserver');
     37      break;
     38  }
     39 }
     40 
     41 function finishTest() {
     42  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
     43  SimpleTest.finish();
     44 }
     45 
     46 function createXHR(url) {
     47  return new Promise(function (resolve, reject) {
     48    var xhr = new XMLHttpRequest();
     49    xhr.open("GET", url, true); // async request
     50    xhr.onload = function () {
     51      if (this.status >= 200 && this.status < 300) {
     52        resolve(xhr.response);
     53      } else {
     54        reject({
     55          status: this.status,
     56          statusText: xhr.statusText
     57        });
     58      }
     59    };
     60    xhr.onerror = function () {
     61      reject({
     62        status: this.status,
     63        statusText: xhr.statusText
     64      });
     65    };
     66    xhr.send();
     67  });
     68 }
     69 
     70 /* Test XHR
     71 * 1. Create two XHR.
     72 * 2. One of the XHR create a cookie names "xhr1", and other one create a http-only cookie names "xhr2".
     73 * 3. Child process only set xhr1 to cookies hash table.
     74 * 4. Child process only can get the xhr1 cookie from cookies hash table.
     75 */
     76 SpecialPowers.pushPrefEnv({
     77  // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
     78  set: [["network.cookie.sameSite.laxByDefault", false]],
     79 }).then(_ => createXHR('set_cookie_xhr.sjs?xhr1'))
     80  .then(_ => createXHR('set_cookie_xhr.sjs?xhr2'));
     81 
     82 </script>
     83 </head>
     84 <body>
     85 <p id="display"></p>
     86 <div id="content" style="display: none"></div>
     87 <pre id="test">
     88 </pre>
     89 </body>
     90 </html>