michael@0: michael@0: var cu = Components.utils; michael@0: cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserver = new HttpServer(); michael@0: var httpserver2 = new HttpServer(); michael@0: var testpath = "/simple"; michael@0: var negativetestpath = "/negative"; michael@0: var httpbody = "0123456789"; michael@0: michael@0: var sb = cu.Sandbox(["http://www.example.com", michael@0: "http://localhost:4444/simple"], michael@0: { wantGlobalProperties: ["XMLHttpRequest"] }); michael@0: michael@0: function createXHR(loc, async) michael@0: { michael@0: var xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", "http://localhost:" + loc, async); michael@0: return xhr; michael@0: } michael@0: michael@0: function checkResults(xhr) michael@0: { michael@0: if (xhr.readyState != 4) michael@0: return false; michael@0: michael@0: do_check_eq(xhr.status, 200); michael@0: do_check_eq(xhr.responseText, httpbody); michael@0: michael@0: var root_node = xhr.responseXML.getElementsByTagName('root').item(0); michael@0: do_check_eq(root_node.firstChild.data, "0123456789"); michael@0: return true; michael@0: } michael@0: michael@0: var httpServersClosed = 0; michael@0: function finishIfDone() michael@0: { michael@0: if (++httpServersClosed == 2) michael@0: do_test_finished(); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: do_test_pending(); michael@0: michael@0: httpserver.registerPathHandler(testpath, serverHandler); michael@0: httpserver.start(4444); michael@0: michael@0: httpserver2.registerPathHandler(negativetestpath, serverHandler); michael@0: httpserver2.start(4445); michael@0: michael@0: // Test sync XHR sending michael@0: cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); michael@0: var res = cu.evalInSandbox('var sync = createXHR("4444/simple"); sync.send(null); sync', sb); michael@0: do_check_true(checkResults(res)); michael@0: michael@0: // negative test sync XHR sending (to ensure that the xhr do not have chrome caps, see bug 779821) michael@0: try { michael@0: cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); michael@0: var res = cu.evalInSandbox('var sync = createXHR("4445/negative"); sync.send(null); sync', sb); michael@0: do_check_false(true, "XHR created from sandbox should not have chrome caps"); michael@0: } catch (e) { michael@0: do_check_true(true); michael@0: } michael@0: michael@0: httpserver2.stop(finishIfDone); michael@0: michael@0: // Test async XHR sending michael@0: sb.finish = function(){ michael@0: httpserver.stop(finishIfDone); michael@0: } michael@0: michael@0: // We want to execute checkResults from the scope of the sandbox as well to michael@0: // make sure that there are no permission errors related to nsEP. For that michael@0: // we need to clone the function into the sandbox and make a few things michael@0: // available for it. michael@0: cu.evalInSandbox('var checkResults = ' + checkResults.toSource(), sb); michael@0: sb.do_check_eq = do_check_eq; michael@0: sb.httpbody = httpbody; michael@0: michael@0: function changeListener(event) { michael@0: if (checkResults(async)) michael@0: finish(); michael@0: } michael@0: michael@0: var async = cu.evalInSandbox('var async = createXHR("4444/simple", true);' + michael@0: 'async.addEventListener("readystatechange", ' + michael@0: changeListener.toString() + ', false);' + michael@0: 'async', sb); michael@0: async.send(null); michael@0: } michael@0: michael@0: function serverHandler(metadata, response) michael@0: { michael@0: response.setHeader("Content-Type", "text/xml", false); michael@0: response.bodyOutputStream.write(httpbody, httpbody.length); michael@0: }