Bug 1892041 - Part 3: Update test exclusions. r=spidermonkey-reviewers,dminor
[gecko.git] / testing / mochitest / document-builder.sjs
blob3ad3a9457912709ef469fe1692e9d41c789f242c
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const { NetUtil } = ChromeUtils.importESModule(
8   "resource://gre/modules/NetUtil.sys.mjs"
9 );
11 function loadHTMLFromFile(path) {
12   // Load the HTML to return in the response from file.
13   // Since it's relative to the cwd of the test runner, we start there and
14   // append to get to the actual path of the file.
15   const testHTMLFile =
16     // eslint-disable-next-line mozilla/use-services
17     Cc["@mozilla.org/file/directory_service;1"]
18       .getService(Ci.nsIProperties)
19       .get("CurWorkD", Ci.nsIFile);
20   const dirs = path.split("/");
21   for (let i = 0; i < dirs.length; i++) {
22     testHTMLFile.append(dirs[i]);
23   }
25   const testHTMLFileStream = Cc[
26     "@mozilla.org/network/file-input-stream;1"
27   ].createInstance(Ci.nsIFileInputStream);
28   testHTMLFileStream.init(testHTMLFile, -1, 0, 0);
29   const testHTML = NetUtil.readInputStreamToString(
30     testHTMLFileStream,
31     testHTMLFileStream.available()
32   );
34   return testHTML;
37 /**
38  * document-builder.sjs can be used to dynamically build documents that will be used in
39  * mochitests. It does handle the following GET parameters:
40  * - file: The path to an (X)HTML file whose content will be used as a response.
41  *        Example: document-builder.sjs?file=/tests/dom/security/test/csp/file_web_manifest_mixed_content.html
42  * - html: A string representation of the HTML document you want to get.
43  *        Example: document-builder.sjs?html=<h1>Hello</h1>
44  * - headers: A <key:value> string representation of headers that will be set on the response
45  *            This is only applied when the html GET parameter is passed as well
46  *        Example: document-builder.sjs?headers=Cross-Origin-Opener-Policy:same-origin&html=<h1>Hello</h1>
47  *                 document-builder.sjs?headers=X-Header1:a&headers=X-Header2:b&html=<h1>Multiple headers</h1>
48  * - delay: Delay the response by X millisecond.
49  */
50 async function handleRequest(request, response) {
51   response.processAsync();
53   const queryString = new URLSearchParams(request.queryString);
54   const html = queryString.get("html");
55   const delay = queryString.get("delay");
57   if (delay) {
58     await new Promise(resolve => {
59       let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
60       timer.initWithCallback(
61         () => {
62           // to avoid garbage collection
63           timer = null;
64           resolve();
65         },
66         delay,
67         Ci.nsITimer.TYPE_ONE_SHOT
68       );
69     });
70   }
72   response.setHeader("Cache-Control", "no-cache", false);
73   if (html) {
74     response.setHeader("Content-Type", "text/html", false);
76     if (queryString.has("headers")) {
77       for (const header of queryString.getAll("headers")) {
78         const [key, value] = header.split(":");
79         response.setHeader(key, value, false);
80       }
81     }
83     response.write(html);
84   } else {
85     const path = queryString.get("file");
86     const doc = loadHTMLFromFile(path);
87     response.setHeader(
88       "Content-Type",
89       path.endsWith(".xhtml") ? "application/xhtml+xml" : "text/html",
90       false
91     );
92     // This is a hack to set the correct id for the content document that is to be
93     // loaded in the iframe.
94     response.write(doc.replace(`id="body"`, `id="default-iframe-body-id"`));
95   }
97   response.finish();