tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_aboutNetError_blank_page.js (4183B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const BLANK_PAGE =
      7  "https://example.com/browser/browser/base/content/test/about/blank_page.sjs";
      8 
      9 function getConnectionState() {
     10  // Prevents items that are being lazy loaded causing issues
     11  document.getElementById("identity-icon-box").click();
     12  gIdentityHandler.refreshIdentityPopup();
     13  return document.getElementById("identity-popup").getAttribute("connection");
     14 }
     15 
     16 async function test_blankPage(
     17  page,
     18  expectedL10nID,
     19  responseStatus,
     20  responseStatusText,
     21  header = "show" // show (zero content-length), hide (no content-length), or lie (non-empty content-length)
     22 ) {
     23  await SpecialPowers.pushPrefEnv({
     24    set: [
     25      ["browser.http.blank_page_with_error_response.enabled", false],
     26      ["browser.urlbar.trustPanel.featureGate", false],
     27    ],
     28  });
     29 
     30  let browser;
     31  let pageLoaded;
     32  const uri = `${page}?status=${encodeURIComponent(
     33    responseStatus
     34  )}&message=${encodeURIComponent(responseStatusText)}&header=${encodeURIComponent(header)}`;
     35 
     36  // Simulating loading the page
     37  await BrowserTestUtils.openNewForegroundTab(
     38    gBrowser,
     39    () => {
     40      gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, uri);
     41      browser = gBrowser.selectedBrowser;
     42      pageLoaded = BrowserTestUtils.waitForErrorPage(browser);
     43    },
     44    false
     45  );
     46 
     47  info("Loading and waiting for the net error");
     48  await pageLoaded;
     49 
     50  is(
     51    getConnectionState(),
     52    "secure",
     53    "httpErrorPage/serverError should be a secure neterror"
     54  );
     55 
     56  await SpecialPowers.spawn(
     57    browser,
     58    [expectedL10nID, responseStatus, responseStatusText],
     59    function (l10nID, expectedStatus, expectedText) {
     60      const doc = content.document;
     61      ok(
     62        doc.documentURI.startsWith("about:neterror"),
     63        "Should be showing error page"
     64      );
     65 
     66      const titleEl = doc.querySelector(".title-text");
     67      const actualDataL10nID = titleEl.getAttribute("data-l10n-id");
     68      is(actualDataL10nID, l10nID, "Correct error page title is set");
     69 
     70      const expectedLabel =
     71        "Error code: " + expectedStatus.toString() + " " + expectedText;
     72      const actualLabel = doc.getElementById(
     73        "response-status-label"
     74      ).textContent;
     75      is(actualLabel, expectedLabel, "Correct response status message is set");
     76    }
     77  );
     78 
     79  BrowserTestUtils.removeTab(gBrowser.selectedTab);
     80  await SpecialPowers.popPrefEnv();
     81 }
     82 
     83 add_task(async function test_blankPage_4xx() {
     84  await test_blankPage(BLANK_PAGE, "httpErrorPage-title", 400, "Bad Request");
     85 });
     86 
     87 add_task(async function test_blankPage_5xx() {
     88  await test_blankPage(
     89    BLANK_PAGE,
     90    "serverError-title",
     91    503,
     92    "Service Unavailable"
     93  );
     94 });
     95 
     96 add_task(async function test_blankPage_withoutHeader_4xx() {
     97  await test_blankPage(
     98    BLANK_PAGE,
     99    "httpErrorPage-title",
    100    400,
    101    "Bad Request",
    102    "hide"
    103  );
    104 });
    105 
    106 add_task(async function test_blankPage_withoutHeader_5xx() {
    107  await test_blankPage(
    108    BLANK_PAGE,
    109    "serverError-title",
    110    503,
    111    "Service Unavailable",
    112    "hide"
    113  );
    114 });
    115 
    116 add_task(async function test_blankPage_lyingHeader_4xx() {
    117  await test_blankPage(
    118    BLANK_PAGE,
    119    "httpErrorPage-title",
    120    400,
    121    "Bad Request",
    122    "lie"
    123  );
    124 });
    125 
    126 add_task(async function test_blankPage_lyingHeader_5xx() {
    127  await test_blankPage(
    128    BLANK_PAGE,
    129    "serverError-title",
    130    503,
    131    "Service Unavailable",
    132    "lie"
    133  );
    134 });
    135 
    136 add_task(async function test_emptyPage_viewSource() {
    137  await SpecialPowers.pushPrefEnv({
    138    set: [["browser.http.blank_page_with_error_response.enabled", false]],
    139  });
    140 
    141  let tab = await BrowserTestUtils.openNewForegroundTab(
    142    gBrowser,
    143    `view-source:${BLANK_PAGE}?status=503&message=Service%20Unavailable&header=show`,
    144    true // wait for the load to complete
    145  );
    146  let browser = tab.linkedBrowser;
    147 
    148  await SpecialPowers.spawn(browser, [], () => {
    149    const doc = content.document;
    150    ok(
    151      !doc.documentURI.startsWith("about:neterror"),
    152      "Should not be showing error page since the scheme is view-source"
    153    );
    154  });
    155 
    156  BrowserTestUtils.removeTab(tab);
    157  await SpecialPowers.popPrefEnv();
    158 });