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/. */
7 const { NetUtil } = ChromeUtils.importESModule(
8 "resource://gre/modules/NetUtil.sys.mjs"
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.
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]);
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(
31 testHTMLFileStream.available()
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.
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");
58 await new Promise(resolve => {
59 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
60 timer.initWithCallback(
62 // to avoid garbage collection
67 Ci.nsITimer.TYPE_ONE_SHOT
72 response.setHeader("Cache-Control", "no-cache", false);
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);
85 const path = queryString.get("file");
86 const doc = loadHTMLFromFile(path);
89 path.endsWith(".xhtml") ? "application/xhtml+xml" : "text/html",
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"`));