summaryrefslogtreecommitdiffstats
path: root/dom/security/test/cors/browser_CORS-console-warnings.js
blob: aa4a211146232cfcf02c006650f152e73582d9d3 (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
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Description of the test:
 *   Ensure that CORS warnings are printed to the web console.
 *
 *   This test uses the same tests as the plain mochitest, but needs access to
 *   the console.
 */
"use strict";

function console_observer(subject, topic, data) {
  var message = subject.wrappedJSObject.arguments[0];
  ok(false, message);
}

var webconsole = null;
var messages_seen = 0;
var expected_messages = 50;

function on_new_message(msgObj) {
  let text = msgObj.message;

  if (text.match("Cross-Origin Request Blocked:")) {
    ok(true, "message is: " + text);
    messages_seen++;
  }
}

async function do_cleanup() {
  Services.console.unregisterListener(on_new_message);
  await unsetCookiePref();
}

/**
 * Set e10s related preferences in the test environment.
 * @return {Promise} promise that resolves when preferences are set.
 */
function setCookiePref() {
  return new Promise(resolve =>
    // accept all cookies so that the CORS requests will send the right cookies
    SpecialPowers.pushPrefEnv(
      {
        set: [["network.cookie.cookieBehavior", 0]],
      },
      resolve
    )
  );
}

/**
 * Unset e10s related preferences in the test environment.
 * @return {Promise} promise that resolves when preferences are unset.
 */
function unsetCookiePref() {
  return new Promise(resolve => {
    SpecialPowers.popPrefEnv(resolve);
  });
}

//jscs:disable
add_task(async function () {
  //jscs:enable
  // A longer timeout is necessary for this test than the plain mochitests
  // due to opening a new tab with the web console.
  requestLongerTimeout(4);
  registerCleanupFunction(do_cleanup);
  await setCookiePref();
  Services.console.registerListener(on_new_message);

  let test_uri =
    "http://mochi.test:8888/browser/dom/security/test/cors/file_cors_logging_test.html";

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank"
  );

  BrowserTestUtils.startLoadingURIString(gBrowser, test_uri);

  await BrowserTestUtils.waitForLocationChange(
    gBrowser,
    test_uri + "#finished"
  );

  // Different OS combinations
  Assert.greater(messages_seen, 0, "Saw " + messages_seen + " messages.");

  messages_seen = 0;
  let test_two_uri =
    "http://mochi.test:8888/browser/dom/security/test/cors/file_bug1456721.html";
  BrowserTestUtils.startLoadingURIString(gBrowser, test_two_uri);

  await BrowserTestUtils.waitForLocationChange(
    gBrowser,
    test_two_uri + "#finishedTestTwo"
  );
  await BrowserTestUtils.waitForCondition(() => messages_seen > 0);

  Assert.greater(messages_seen, 0, "Saw " + messages_seen + " messages.");

  BrowserTestUtils.removeTab(tab);
});