summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-only/browser_httpsonly_speculative_connect.js
blob: 664d624ab816b2aa81cdd209f0c298b4cdcdd75b (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
"use strict";

const TEST_PATH_HTTP = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.org"
);

let console_messages = [
  {
    description: "Speculative Connection should get logged",
    expectLogLevel: Ci.nsIConsoleMessage.warn,
    expectIncludes: [
      "Upgrading insecure speculative TCP connection",
      "to use",
      "example.org",
      "file_httpsonly_speculative_connect.html",
    ],
  },
  {
    description: "Upgrade should get logged",
    expectLogLevel: Ci.nsIConsoleMessage.warn,
    expectIncludes: [
      "Upgrading insecure request",
      "to use",
      "example.org",
      "file_httpsonly_speculative_connect.html",
    ],
  },
];

function on_new_console_messages(msgObj) {
  const message = msgObj.message;
  const logLevel = msgObj.logLevel;

  if (message.includes("HTTPS-Only Mode:")) {
    for (let i = 0; i < console_messages.length; i++) {
      const testCase = console_messages[i];
      // Check if log-level matches
      if (logLevel !== testCase.expectLogLevel) {
        continue;
      }
      // Check if all substrings are included
      if (testCase.expectIncludes.some(str => !message.includes(str))) {
        continue;
      }
      ok(true, testCase.description);
      console_messages.splice(i, 1);
      break;
    }
  }
}

add_task(async function () {
  requestLongerTimeout(4);

  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_only_mode", true]],
  });
  Services.console.registerListener(on_new_console_messages);

  await BrowserTestUtils.loadURIString(
    gBrowser.selectedBrowser,
    `${TEST_PATH_HTTP}file_httpsonly_speculative_connect.html`
  );

  await BrowserTestUtils.waitForCondition(() => console_messages.length === 0);

  Services.console.unregisterListener(on_new_console_messages);
});