content/base/test/csp/file_CSP_bug888172.sjs

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial