Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 // SJS file for CSP mochitests
3 Components.utils.import("resource://gre/modules/NetUtil.jsm");
5 function loadHTMLFromFile(path) {
6 // Load the HTML to return in the response from file.
7 // Since it's relative to the cwd of the test runner, we start there and
8 // append to get to the actual path of the file.
9 var testHTMLFile =
10 Components.classes["@mozilla.org/file/directory_service;1"].
11 getService(Components.interfaces.nsIProperties).
12 get("CurWorkD", Components.interfaces.nsILocalFile);
13 var dirs = path.split("/");
14 for (var i = 0; i < dirs.length; i++) {
15 testHTMLFile.append(dirs[i]);
16 }
17 var testHTMLFileStream =
18 Components.classes["@mozilla.org/network/file-input-stream;1"].
19 createInstance(Components.interfaces.nsIFileInputStream);
20 testHTMLFileStream.init(testHTMLFile, -1, 0, 0);
21 var testHTML = NetUtil.readInputStreamToString(testHTMLFileStream, testHTMLFileStream.available());
22 return testHTML;
23 }
25 function handleRequest(request, response)
26 {
27 var query = {};
28 request.queryString.split('&').forEach(function (val) {
29 var [name, value] = val.split('=');
30 query[name] = unescape(value);
31 });
33 // avoid confusing cache behaviors
34 response.setHeader("Cache-Control", "no-cache", false);
36 // Deliver the CSP policy encoded in the URI
37 if (query['csp'])
38 response.setHeader("Content-Security-Policy", unescape(query['csp']), false);
40 // Send HTML to test allowed/blocked behaviors
41 response.setHeader("Content-Type", "text/html", false);
42 response.write(loadHTMLFromFile("tests/content/base/test/csp/file_CSP_bug888172.html"));
43 }