summaryrefslogtreecommitdiffstats
path: root/toolkit/components/remotebrowserutils/tests/browser/coop_header.sjs
blob: c6b537d7706a86befd216464a39d737a9dafbab6 (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
function handleRequest(request, response) {
  Cu.importGlobalProperties(["URLSearchParams"]);
  let query = new URLSearchParams(request.queryString);

  response.setStatusLine(request.httpVersion, 200, "OK");

  // The tests for Cross-Origin-Opener-Policy unfortunately depend on
  // BFCacheInParent not kicking in, as with that enabled, it is not possible to
  // tell whether the BrowsingContext switch was caused by the BFCache
  // navigation or by the COOP mismatch. This header disables BFCache for the
  // coop documents, and should avoid the issue.
  response.setHeader("Cache-Control", "no-store", false);

  let isDownloadPage = false;
  let isDownloadFile = false;

  query.forEach((value, name) => {
    if (name === "downloadPage") {
      isDownloadPage = true;
    } else if (name === "downloadFile") {
      isDownloadFile = true;
    } else if (name == "coop") {
      response.setHeader("Cross-Origin-Opener-Policy", unescape(value), false);
    } else if (name == "coep") {
      response.setHeader(
        "Cross-Origin-Embedder-Policy",
        unescape(value),
        false
      );
    }
  });

  let downloadHTML = "";
  if (isDownloadPage) {
    ["no-coop", "same-origin", "same-origin-allow-popups"].forEach(coop => {
      downloadHTML +=
        '<a href="https://example.com/browser/toolkit/components/remotebrowserutils/tests/browser/coop_header.sjs?downloadFile&' +
        (coop === "no-coop" ? "" : coop) +
        '" id="' +
        coop +
        '" download>' +
        unescape(coop) +
        "</a> <br>";
    });
  }

  if (isDownloadFile) {
    response.setHeader("Content-Type", "application/octet-stream", false);
    response.write("BINARY_DATA");
  } else {
    response.setHeader("Content-Type", "text/html; charset=utf-8", false);
    response.write(
      "<!DOCTYPE html><html><body><p>Hello world</p> " +
        downloadHTML +
        "</body></html>"
    );
  }
}