summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-only/test_user_suggestion_box.html
blob: 1feabcd003532f0abadb2208ef1ae34c8f72995e (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
<!DOCTYPE html>
<html>
<head>
    <title>Bug 1665057 - Add www button on https-only error page</title>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
"use strict";

/*
 * Description of the test:
 * We send a top-level request to a http-page in https-only mode
 * The page has a bad certificate and can't be updated so the error page appears
 * If there is a secure connection possible to www the suggestion-box on the error page
 * should appear and have a link to that secure www-page
 * if the original-pagerequest already has a www or there is no secure www connection
 * the suggestion box should not appear
 */

SimpleTest.requestFlakyTimeout("We need to wait for the HTTPS-Only error page to appear and for the additional 'www' request to provide a suggestion.");
SimpleTest.requestLongerTimeout(10);

// urls of server locations with bad cert -> https-only should display error page
const KICK_OFF_REQUEST_WITH_SUGGESTION = "http://suggestion-example.com";
const KICK_OFF_REQUEST_WITHOUT_SUGGESTION = "http://no-suggestion-example.com";

// l10n ids to compare html to
const ERROR_PAGE_L10N_ID = "about-httpsonly-title-alert";
const SUGGESTION_BOX_L10N_ID = "about-httpsonly-suggestion-box-www-text";

// the error page needs to be build and a background https://www request needs to happen
// we use 4 seconds to make sure these requests did happen.
function resolveAfter4Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve();
    }, 4000);
  });
}

async function runTests(aMessage) {
  let errorPageL10nId = "about-httpsonly-title-alert";
  let suggestionBoxL10nId = "about-httpsonly-suggestion-box-www-text";

  let innerHTML = content.document.body.innerHTML;

  if (aMessage === "with_suggestion") {
    // test if the page with suggestion shows the error page and the suggestion box
    ok(innerHTML.includes(errorPageL10nId), "the error page should be shown.");
    ok(innerHTML.includes(suggestionBoxL10nId), "the suggestion box should be shown.");
  } else if (aMessage === "without_suggestion") {
    // test if the page without suggestion shows the error page but not the suggestion box
    ok(innerHTML.includes(errorPageL10nId), "the error page should be shown.");
    ok(!innerHTML.includes(suggestionBoxL10nId), "the suggestion box should not be shown.");
  } else {
    ok(false, "we should never get here");
  }
}

add_task(async function() {
  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 testWinSuggestion = window.open(KICK_OFF_REQUEST_WITH_SUGGESTION, "_blank");
  let testWinWithoutSuggestion = window.open(KICK_OFF_REQUEST_WITHOUT_SUGGESTION, "_blank");

  await resolveAfter4Seconds();

  await SpecialPowers.spawn(testWinSuggestion, ["with_suggestion"], runTests);
  await SpecialPowers.spawn(testWinWithoutSuggestion, ["without_suggestion"], runTests);

  testWinSuggestion.close();
  testWinWithoutSuggestion.close();
});
</script>
</body>
</html>