|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 const { PageMod } = require("sdk/page-mod"); |
|
7 const tabs = require("sdk/tabs"); |
|
8 const { startServerAsync } = require("sdk/test/httpd"); |
|
9 |
|
10 const serverPort = 8099; |
|
11 const TEST_TAB_URL = "about:mozilla"; |
|
12 |
|
13 exports.testCrossDomainIframe = function(assert, done) { |
|
14 let server = startServerAsync(serverPort); |
|
15 server.registerPathHandler("/iframe", function handle(request, response) { |
|
16 response.write("<html><body>foo</body></html>"); |
|
17 }); |
|
18 |
|
19 let pageMod = PageMod({ |
|
20 include: TEST_TAB_URL, |
|
21 contentScript: "new " + function ContentScriptScope() { |
|
22 self.on("message", function (url) { |
|
23 let iframe = document.createElement("iframe"); |
|
24 iframe.addEventListener("load", function onload() { |
|
25 iframe.removeEventListener("load", onload, false); |
|
26 self.postMessage(iframe.contentWindow.document.body.innerHTML); |
|
27 }, false); |
|
28 iframe.setAttribute("src", url); |
|
29 document.documentElement.appendChild(iframe); |
|
30 }); |
|
31 }, |
|
32 onAttach: function(w) { |
|
33 w.on("message", function (body) { |
|
34 assert.equal(body, "foo", "received iframe html content"); |
|
35 pageMod.destroy(); |
|
36 w.tab.close(function() { |
|
37 server.stop(done); |
|
38 }); |
|
39 }); |
|
40 |
|
41 w.postMessage("http://localhost:" + serverPort + "/iframe"); |
|
42 } |
|
43 }); |
|
44 |
|
45 tabs.open({ |
|
46 url: TEST_TAB_URL, |
|
47 inBackground: true |
|
48 }); |
|
49 }; |
|
50 |
|
51 exports.testCrossDomainXHR = function(assert, done) { |
|
52 let server = startServerAsync(serverPort); |
|
53 server.registerPathHandler("/xhr", function handle(request, response) { |
|
54 response.write("foo"); |
|
55 }); |
|
56 |
|
57 let pageMod = PageMod({ |
|
58 include: TEST_TAB_URL, |
|
59 contentScript: "new " + function ContentScriptScope() { |
|
60 self.on("message", function (url) { |
|
61 let request = new XMLHttpRequest(); |
|
62 request.overrideMimeType("text/plain"); |
|
63 request.open("GET", url, true); |
|
64 request.onload = function () { |
|
65 self.postMessage(request.responseText); |
|
66 }; |
|
67 request.send(null); |
|
68 }); |
|
69 }, |
|
70 onAttach: function(w) { |
|
71 w.on("message", function (body) { |
|
72 assert.equal(body, "foo", "received XHR content"); |
|
73 pageMod.destroy(); |
|
74 w.tab.close(function() { |
|
75 server.stop(done); |
|
76 }); |
|
77 }); |
|
78 |
|
79 w.postMessage("http://localhost:" + serverPort + "/xhr"); |
|
80 } |
|
81 }); |
|
82 |
|
83 tabs.open({ |
|
84 url: TEST_TAB_URL, |
|
85 inBackground: true |
|
86 }); |
|
87 }; |
|
88 |
|
89 require("sdk/test/runner").runTestsFromModule(module); |