1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/browser/browser_xhr_sandbox.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,50 @@ 1.4 +// This code is evaluated in a sandbox courtesy of toSource(); 1.5 +let sandboxCode = (function() { 1.6 + let req = new XMLHttpRequest(); 1.7 + req.open("GET", "http://mochi.test:8888/browser/dom/tests/browser/", true); 1.8 + req.onreadystatechange = function() { 1.9 + if (req.readyState === 4) { 1.10 + // If we get past the problem above, we end up with a req.status of zero 1.11 + // (ie, blocked due to CORS) even though we are fetching from the same 1.12 + // origin as the window itself. 1.13 + let result; 1.14 + if (req.status != 200) { 1.15 + result = "ERROR: got request status of " + req.status; 1.16 + } else if (req.responseText.length == 0) { 1.17 + result = "ERROR: got zero byte response text"; 1.18 + } else { 1.19 + result = "ok"; 1.20 + } 1.21 + postMessage({result: result}, "*"); 1.22 + } 1.23 + }; 1.24 + req.send(null); 1.25 +}).toSource() + "();"; 1.26 + 1.27 +function test() { 1.28 + waitForExplicitFinish(); 1.29 + let appShell = Cc["@mozilla.org/appshell/appShellService;1"] 1.30 + .getService(Ci.nsIAppShellService); 1.31 + let doc = appShell.hiddenDOMWindow.document; 1.32 + let frame = doc.createElement("iframe"); 1.33 + frame.setAttribute("type", "content"); 1.34 + frame.setAttribute("src", "http://mochi.test:8888/browser/dom/tests/browser/browser_xhr_sandbox.js"); 1.35 + 1.36 + frame.addEventListener("load", function () { 1.37 + let workerWindow = frame.contentWindow; 1.38 + workerWindow.addEventListener("message", function(evt) { 1.39 + is(evt.data.result, "ok", "check the sandbox code was happy"); 1.40 + frame.remove(); 1.41 + finish(); 1.42 + }, true); 1.43 + let sandbox = new Cu.Sandbox(workerWindow); 1.44 + // inject some functions from the window into the sandbox. 1.45 + // postMessage so the async code in the sandbox can report a result. 1.46 + sandbox.importFunction(workerWindow.postMessage.bind(workerWindow), "postMessage"); 1.47 + sandbox.importFunction(workerWindow.XMLHttpRequest, "XMLHttpRequest"); 1.48 + Cu.evalInSandbox(sandboxCode, sandbox, "1.8"); 1.49 + }, true); 1.50 + 1.51 + let container = doc.body ? doc.body : doc.documentElement; 1.52 + container.appendChild(frame); 1.53 +}