michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { PageMod } = require("sdk/page-mod"); michael@0: const tabs = require("sdk/tabs"); michael@0: const { startServerAsync } = require("sdk/test/httpd"); michael@0: michael@0: const serverPort = 8099; michael@0: const TEST_TAB_URL = "about:mozilla"; michael@0: michael@0: exports.testCrossDomainIframe = function(assert, done) { michael@0: let server = startServerAsync(serverPort); michael@0: server.registerPathHandler("/iframe", function handle(request, response) { michael@0: response.write("foo"); michael@0: }); michael@0: michael@0: let pageMod = PageMod({ michael@0: include: TEST_TAB_URL, michael@0: contentScript: "new " + function ContentScriptScope() { michael@0: self.on("message", function (url) { michael@0: let iframe = document.createElement("iframe"); michael@0: iframe.addEventListener("load", function onload() { michael@0: iframe.removeEventListener("load", onload, false); michael@0: self.postMessage(iframe.contentWindow.document.body.innerHTML); michael@0: }, false); michael@0: iframe.setAttribute("src", url); michael@0: document.documentElement.appendChild(iframe); michael@0: }); michael@0: }, michael@0: onAttach: function(w) { michael@0: w.on("message", function (body) { michael@0: assert.equal(body, "foo", "received iframe html content"); michael@0: pageMod.destroy(); michael@0: w.tab.close(function() { michael@0: server.stop(done); michael@0: }); michael@0: }); michael@0: michael@0: w.postMessage("http://localhost:" + serverPort + "/iframe"); michael@0: } michael@0: }); michael@0: michael@0: tabs.open({ michael@0: url: TEST_TAB_URL, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testCrossDomainXHR = function(assert, done) { michael@0: let server = startServerAsync(serverPort); michael@0: server.registerPathHandler("/xhr", function handle(request, response) { michael@0: response.write("foo"); michael@0: }); michael@0: michael@0: let pageMod = PageMod({ michael@0: include: TEST_TAB_URL, michael@0: contentScript: "new " + function ContentScriptScope() { michael@0: self.on("message", function (url) { michael@0: let request = new XMLHttpRequest(); michael@0: request.overrideMimeType("text/plain"); michael@0: request.open("GET", url, true); michael@0: request.onload = function () { michael@0: self.postMessage(request.responseText); michael@0: }; michael@0: request.send(null); michael@0: }); michael@0: }, michael@0: onAttach: function(w) { michael@0: w.on("message", function (body) { michael@0: assert.equal(body, "foo", "received XHR content"); michael@0: pageMod.destroy(); michael@0: w.tab.close(function() { michael@0: server.stop(done); michael@0: }); michael@0: }); michael@0: michael@0: w.postMessage("http://localhost:" + serverPort + "/xhr"); michael@0: } michael@0: }); michael@0: michael@0: tabs.open({ michael@0: url: TEST_TAB_URL, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: require("sdk/test/runner").runTestsFromModule(module);