summaryrefslogtreecommitdiffstats
path: root/dom/security/test/sec-fetch/test_websocket.html
blob: 33053ff20a091717b3fc8cde11725f7ee5e0f280 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1628605: Test Sec-Fetch-* header for websockets</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="application/javascript">

SimpleTest.waitForExplicitFinish();

let testsSucceeded = 0;

function checkTestsDone() {
  testsSucceeded++;
  if (testsSucceeded == 2) {
    SimpleTest.finish();
  }
}

var script = SpecialPowers.loadChromeScript(() => {
  /* eslint-env mozilla/chrome-script */
  Services.obs.addObserver(function onExamResp(subject, topic, data) {
    let channel = subject.QueryInterface(Ci.nsIHttpChannel);
    if (!channel.URI.spec.startsWith("https://example.com/tests/dom/security/test/sec-fetch/file_websocket")) {
      return;
    }

    // Sec-Fetch-* Headers should be present for Dest, Mode, Site
    try {
      let secFetchDest = channel.getRequestHeader("Sec-Fetch-Dest");
      is(secFetchDest, "websocket", "testing sec-fetch-dest");

      let secFetchMode = channel.getRequestHeader("Sec-Fetch-Mode");
      is(secFetchMode, "websocket", "testing sec-fetch-mode");

      let secFetchSite = channel.getRequestHeader("Sec-Fetch-Site");
      is(secFetchSite, "cross-site", "testing sec-fetch-site");
    }
    catch (e) {
      ok(false, "testing sec-fetch-*");
    }

    // Sec-Fetch-User should not be present
    try {
      channel.getRequestHeader("Sec-Fetch-User");
      ok(false, "testing sec-fetch-user");
    }
    catch (e) {
      ok(true, "testing sec-fetch-user");
    }
    Services.obs.removeObserver(onExamResp, "http-on-stop-request");

    sendAsyncMessage("test-end");
  }, "http-on-stop-request");
});

script.addMessageListener("test-end", () => {
  checkTestsDone();
});

var wssSocket = new WebSocket("wss://example.com/tests/dom/security/test/sec-fetch/file_websocket");
wssSocket.onopen = function(e) {
  ok(true, "sanity: wssSocket onopen");
  checkTestsDone();
};
wssSocket.onerror = function(e) {
  ok(false, "sanity: wssSocket onerror");
};

</script>
</body>
</html>