summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-first/browser_httpsfirst_speculative_connect.js
blob: f06ba0a944d6ec98be058041c5b058f7c38b85f4 (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
"use strict";

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

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

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

  if (message.includes("HTTPS-First 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_first", true]],
  });
  Services.console.registerListener(on_new_console_messages);

  let promiseLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  BrowserTestUtils.startLoadingURIString(
    gBrowser.selectedBrowser,
    `${TEST_PATH_HTTP}file_httpsfirst_speculative_connect.html`
  );
  await promiseLoaded;

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

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