file_testserver.sjs (1734B)
1 "use strict"; 2 let { NetUtil } = ChromeUtils.importESModule( 3 "resource://gre/modules/NetUtil.sys.mjs" 4 ); 5 6 function loadHTMLFromFile(path) { 7 // Load the HTML to return in the response from file. 8 // Since it's relative to the cwd of the test runner, we start there and 9 // append to get to the actual path of the file. 10 const testHTMLFile = Services.dirsvc.get("CurWorkD", Ci.nsIFile); 11 12 const testHTMLFileStream = Cc[ 13 "@mozilla.org/network/file-input-stream;1" 14 ].createInstance(Ci.nsIFileInputStream); 15 16 path 17 .split("/") 18 .filter(path_1 => path_1) 19 .reduce((file, path_2) => { 20 testHTMLFile.append(path_2); 21 return testHTMLFile; 22 }, testHTMLFile); 23 testHTMLFileStream.init(testHTMLFile, -1, 0, 0); 24 const isAvailable = testHTMLFileStream.available(); 25 return NetUtil.readInputStreamToString(testHTMLFileStream, isAvailable); 26 } 27 28 function handleRequest(request, response) { 29 const query = new URLSearchParams(request.queryString); 30 31 // avoid confusing cache behaviors 32 response.setHeader("Cache-Control", "no-cache", false); 33 34 // Deliver the CSP policy encoded in the URL 35 if (query.has("csp")) { 36 response.setHeader("Content-Security-Policy", query.get("csp"), false); 37 } 38 39 // Deliver the CSPRO policy encoded in the URL 40 if (query.has("cspro")) { 41 response.setHeader( 42 "Content-Security-Policy-Report-Only", 43 query.get("cspro"), 44 false 45 ); 46 } 47 48 // Deliver the CORS header in the URL 49 if (query.has("cors")) { 50 response.setHeader("Access-Control-Allow-Origin", query.get("cors"), false); 51 } 52 53 // Send HTML to test allowed/blocked behaviors 54 response.setHeader("Content-Type", "text/html", false); 55 response.write(loadHTMLFromFile(query.get("file"))); 56 }