summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/test_1421324.html
blob: 627a339e0a4b29162fe3d31c78826c3372b396a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
  <title>Cookie changes from XHR requests are observed in content processes.</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script type="text/javascript">
SimpleTest.waitForExplicitFinish();

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));
gScript.addMessageListener("cookieName", confirmCookieName);
gScript.addMessageListener("removeObserver:return", finishTest);
gScript.sendAsyncMessage('createObserver');

// Confirm the notify which represents the cookie is updating.
var testsNum = 0;
function confirmCookieName(name) {
  testsNum++;
  switch(testsNum) {
    case 1:
      is(name, "testXHR1=xhr_val1", "The cookie which names " + name + " is update to db");
      break;
    case 2:
      document.cookie = "testXHR2=xhr_val2; path=/";
      break;
    case 3:
      is(document.cookie, "testXHR2=xhr_val2", "Confirm the cookie string");
      document.cookie = "testXHR1=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
      document.cookie = "testXHR2=; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
      gScript.sendAsyncMessage('removeObserver');
      break;
  }
}

function finishTest() {
  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
  SimpleTest.finish();
}

function createXHR(url) {
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true); // async request
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    xhr.send();
  });
}


/* Test XHR
 * 1. Create two XHR.
 * 2. One of the XHR create a cookie names "set_cookie", and other one create a http-only cookie names "modify_cookie".
 * 3. Child process only set testXHR1 to cookies hash table.
 * 4. Child process only can get the testXHR1 cookie from cookies hash table.
 */
SpecialPowers.pushPrefEnv({
  // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
  set: [["network.cookie.sameSite.laxByDefault", false]],
}).then(_ => createXHR('reset_cookie_xhr.sjs?set_cookie'))
  .then(_ => createXHR('reset_cookie_xhr.sjs?modify_cookie'));

</script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
</pre>
</body>
</html>