summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/content-permissions/main.js
blob: b476ccb74b9bdc5ec32abccd869d716427320449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { PageMod } = require("sdk/page-mod");
const tabs = require("sdk/tabs");
const { startServerAsync } = require("./httpd");

const serverPort = 8099;
const TEST_TAB_URL = "about:mozilla";

exports.testCrossDomainIframe = function(assert, done) {
  let server = startServerAsync(serverPort);
  server.registerPathHandler("/iframe", function handle(request, response) {
    response.write("<html><body>foo</body></html>");
  });

  let pageMod = PageMod({
    include: TEST_TAB_URL,
    contentScript: "new " + function ContentScriptScope() {
      self.on("message", function (url) {
        let iframe = document.createElement("iframe");
        iframe.addEventListener("load", function onload() {
          iframe.removeEventListener("load", onload, false);
          self.postMessage(iframe.contentWindow.document.body.innerHTML);
        }, false);
        iframe.setAttribute("src", url);
        document.documentElement.appendChild(iframe);
      });
    },
    onAttach: function(w) {
      w.on("message", function (body) {
        assert.equal(body, "foo", "received iframe html content");
        pageMod.destroy();
        w.tab.close(function() {
          server.stop(done);
        });
      });

      w.postMessage("http://localhost:" + serverPort + "/iframe");
    }
  });

  tabs.open({
    url: TEST_TAB_URL,
    inBackground: true
  });
};

exports.testCrossDomainXHR = function(assert, done) {
  let server = startServerAsync(serverPort);
  server.registerPathHandler("/xhr", function handle(request, response) {
    response.write("foo");
  });

  let pageMod = PageMod({
    include: TEST_TAB_URL,
    contentScript: "new " + function ContentScriptScope() {
      self.on("message", function (url) {
        let request = new XMLHttpRequest();
        request.overrideMimeType("text/plain");
        request.open("GET", url, true);
        request.onload = function () {
          self.postMessage(request.responseText);
        };
        request.send(null);
      });
    },
    onAttach: function(w) {
      w.on("message", function (body) {
        assert.equal(body, "foo", "received XHR content");
        pageMod.destroy();
        w.tab.close(function() {
          server.stop(done);
        });
      });

      w.postMessage("http://localhost:" + serverPort + "/xhr");
    }
  });

  tabs.open({
    url: TEST_TAB_URL,
    inBackground: true
  });
};

require("sdk/test/runner").runTestsFromModule(module);