summaryrefslogtreecommitdiffstats
path: root/toolkit/components/httpsonlyerror/tests/browser/browser_errorpage_www_suggestion.js
blob: 7e3eac98173b49402b694771749ea1b535eb8913 (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
/* 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";
requestLongerTimeout(2);

const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  ""
);
const HTML_PATH = "/file_errorpage_www_suggestion.html";
const KICK_OF_REQUEST_WITH_SUGGESTION =
  "http://suggestion-example.com" + TEST_PATH + HTML_PATH;

add_task(async function () {
  info("Check that the www button shows up and leads to a secure www page");

  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.security.https_only_mode", true],
      ["dom.security.https_only_mode_send_http_background_request", false],
      ["dom.security.https_only_mode_error_page_user_suggestions", true],
    ],
  });

  let browser = gBrowser.selectedBrowser;
  let errorPageLoaded = BrowserTestUtils.waitForErrorPage(browser);
  BrowserTestUtils.startLoadingURIString(
    browser,
    KICK_OF_REQUEST_WITH_SUGGESTION
  );
  await errorPageLoaded;

  let pageShownPromise = BrowserTestUtils.waitForContentEvent(
    browser,
    "pageshow",
    true
  );

  // There's an arbitrary interval of 2 seconds in which the background
  // request for the www page is made. we wait this out to ensure the
  // www button has shown up.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(c => setTimeout(c, 2000));

  await SpecialPowers.spawn(browser, [], async function () {
    let doc = content.document;
    let innerHTML = doc.body.innerHTML;
    let errorPageL10nId = "about-httpsonly-title-alert";
    let suggestionBoxL10nId = "about-httpsonly-suggestion-box-www-text";

    ok(innerHTML.includes(errorPageL10nId), "the error page should show up");
    ok(doc.documentURI.startsWith("about:httpsonlyerror"));
    ok(
      innerHTML.includes(suggestionBoxL10nId),
      "the suggestion box should show up"
    );

    // click on www button
    let wwwButton = content.document.getElementById("openWWW");
    Assert.notStrictEqual(wwwButton, null, "The www Button should be shown");

    if (!wwwButton) {
      ok(false, "We should not be here");
    } else {
      wwwButton.click();
    }
  });
  await pageShownPromise;
  await SpecialPowers.spawn(browser, [], async function () {
    let doc = content.document;
    let innerHTML = doc.body.innerHTML;
    ok(
      innerHTML.includes("You are now on the secure www. page"),
      "The secure page should be reached after clicking the button"
    );
    ok(doc.documentURI.startsWith("https://www."), "Page should be secure www");
  });
});