michael@0: // SJS file for CSP mochitests michael@0: michael@0: Components.utils.import("resource://gre/modules/NetUtil.jsm"); michael@0: michael@0: function loadHTMLFromFile(path) { michael@0: // Load the HTML to return in the response from file. michael@0: // Since it's relative to the cwd of the test runner, we start there and michael@0: // append to get to the actual path of the file. michael@0: var testHTMLFile = michael@0: Components.classes["@mozilla.org/file/directory_service;1"]. michael@0: getService(Components.interfaces.nsIProperties). michael@0: get("CurWorkD", Components.interfaces.nsILocalFile); michael@0: var dirs = path.split("/"); michael@0: for (var i = 0; i < dirs.length; i++) { michael@0: testHTMLFile.append(dirs[i]); michael@0: } michael@0: var testHTMLFileStream = michael@0: Components.classes["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Components.interfaces.nsIFileInputStream); michael@0: testHTMLFileStream.init(testHTMLFile, -1, 0, 0); michael@0: var testHTML = NetUtil.readInputStreamToString(testHTMLFileStream, testHTMLFileStream.available()); michael@0: return testHTML; michael@0: } michael@0: michael@0: function handleRequest(request, response) michael@0: { michael@0: var query = {}; michael@0: request.queryString.split('&').forEach(function (val) { michael@0: var [name, value] = val.split('='); michael@0: query[name] = unescape(value); michael@0: }); michael@0: michael@0: // avoid confusing cache behaviors michael@0: response.setHeader("Cache-Control", "no-cache", false); michael@0: michael@0: // Deliver the CSP policy encoded in the URI michael@0: if (query['csp']) michael@0: response.setHeader("Content-Security-Policy", unescape(query['csp']), false); michael@0: michael@0: // Send HTML to test allowed/blocked behaviors michael@0: response.setHeader("Content-Type", "text/html", false); michael@0: response.write(loadHTMLFromFile("tests/content/base/test/csp/file_CSP_bug888172.html")); michael@0: }