1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/unit/test_allowedDomainsXHR.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 + 1.5 +var cu = Components.utils; 1.6 +cu.import("resource://testing-common/httpd.js"); 1.7 + 1.8 +var httpserver = new HttpServer(); 1.9 +var httpserver2 = new HttpServer(); 1.10 +var testpath = "/simple"; 1.11 +var negativetestpath = "/negative"; 1.12 +var httpbody = "<?xml version='1.0' ?><root>0123456789</root>"; 1.13 + 1.14 +var sb = cu.Sandbox(["http://www.example.com", 1.15 + "http://localhost:4444/simple"], 1.16 + { wantGlobalProperties: ["XMLHttpRequest"] }); 1.17 + 1.18 +function createXHR(loc, async) 1.19 +{ 1.20 + var xhr = new XMLHttpRequest(); 1.21 + xhr.open("GET", "http://localhost:" + loc, async); 1.22 + return xhr; 1.23 +} 1.24 + 1.25 +function checkResults(xhr) 1.26 +{ 1.27 + if (xhr.readyState != 4) 1.28 + return false; 1.29 + 1.30 + do_check_eq(xhr.status, 200); 1.31 + do_check_eq(xhr.responseText, httpbody); 1.32 + 1.33 + var root_node = xhr.responseXML.getElementsByTagName('root').item(0); 1.34 + do_check_eq(root_node.firstChild.data, "0123456789"); 1.35 + return true; 1.36 +} 1.37 + 1.38 +var httpServersClosed = 0; 1.39 +function finishIfDone() 1.40 +{ 1.41 + if (++httpServersClosed == 2) 1.42 + do_test_finished(); 1.43 +} 1.44 + 1.45 +function run_test() 1.46 +{ 1.47 + do_test_pending(); 1.48 + 1.49 + httpserver.registerPathHandler(testpath, serverHandler); 1.50 + httpserver.start(4444); 1.51 + 1.52 + httpserver2.registerPathHandler(negativetestpath, serverHandler); 1.53 + httpserver2.start(4445); 1.54 + 1.55 + // Test sync XHR sending 1.56 + cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); 1.57 + var res = cu.evalInSandbox('var sync = createXHR("4444/simple"); sync.send(null); sync', sb); 1.58 + do_check_true(checkResults(res)); 1.59 + 1.60 + // negative test sync XHR sending (to ensure that the xhr do not have chrome caps, see bug 779821) 1.61 + try { 1.62 + cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); 1.63 + var res = cu.evalInSandbox('var sync = createXHR("4445/negative"); sync.send(null); sync', sb); 1.64 + do_check_false(true, "XHR created from sandbox should not have chrome caps"); 1.65 + } catch (e) { 1.66 + do_check_true(true); 1.67 + } 1.68 + 1.69 + httpserver2.stop(finishIfDone); 1.70 + 1.71 + // Test async XHR sending 1.72 + sb.finish = function(){ 1.73 + httpserver.stop(finishIfDone); 1.74 + } 1.75 + 1.76 + // We want to execute checkResults from the scope of the sandbox as well to 1.77 + // make sure that there are no permission errors related to nsEP. For that 1.78 + // we need to clone the function into the sandbox and make a few things 1.79 + // available for it. 1.80 + cu.evalInSandbox('var checkResults = ' + checkResults.toSource(), sb); 1.81 + sb.do_check_eq = do_check_eq; 1.82 + sb.httpbody = httpbody; 1.83 + 1.84 + function changeListener(event) { 1.85 + if (checkResults(async)) 1.86 + finish(); 1.87 + } 1.88 + 1.89 + var async = cu.evalInSandbox('var async = createXHR("4444/simple", true);' + 1.90 + 'async.addEventListener("readystatechange", ' + 1.91 + changeListener.toString() + ', false);' + 1.92 + 'async', sb); 1.93 + async.send(null); 1.94 +} 1.95 + 1.96 +function serverHandler(metadata, response) 1.97 +{ 1.98 + response.setHeader("Content-Type", "text/xml", false); 1.99 + response.bodyOutputStream.write(httpbody, httpbody.length); 1.100 +}