tor-browser

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

browser_103_preload_2.js (5100B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 Services.prefs.setBoolPref("network.early-hints.enabled", true);
      8 
      9 const {
     10  test_hint_preload,
     11  test_hint_preload_internal,
     12  request_count_checking,
     13 } = ChromeUtils.importESModule(
     14  "resource://testing-common/early_hint_preload_test_helper.sys.mjs"
     15 );
     16 
     17 // two early hint responses
     18 add_task(async function test_103_two_preload_responses() {
     19  await test_hint_preload_internal(
     20    "103_two_preload_responses",
     21    "https://example.com",
     22    [
     23      [
     24        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     25        Services.uuid.generateUUID().toString(),
     26      ],
     27      ["", "new_response"], // indicate new early hint response
     28      [
     29        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     30        Services.uuid.generateUUID().toString(),
     31      ],
     32    ],
     33    { hinted: 1, normal: 1 }
     34  );
     35 });
     36 
     37 // two link header in one early hint response
     38 add_task(async function test_103_two_link_header() {
     39  await test_hint_preload_internal(
     40    "103_two_link_header",
     41    "https://example.com",
     42    [
     43      [
     44        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     45        Services.uuid.generateUUID().toString(),
     46      ],
     47      ["", ""], // indicate new link header in same reponse
     48      [
     49        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     50        Services.uuid.generateUUID().toString(),
     51      ],
     52    ],
     53    { hinted: 2, normal: 0 }
     54  );
     55 });
     56 
     57 // two links in one early hint link header
     58 add_task(async function test_103_two_links() {
     59  await test_hint_preload_internal(
     60    "103_two_links",
     61    "https://example.com",
     62    [
     63      [
     64        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     65        Services.uuid.generateUUID().toString(),
     66      ],
     67      [
     68        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     69        Services.uuid.generateUUID().toString(),
     70      ],
     71    ],
     72    { hinted: 2, normal: 0 }
     73  );
     74 });
     75 
     76 // two early hint responses, only second one has a link header
     77 add_task(async function test_103_two_links() {
     78  await test_hint_preload_internal(
     79    "103_two_links",
     80    "https://example.com",
     81    [
     82      ["", "non_link_header"], // indicate non-link related header
     83      ["", "new_response"], // indicate new early hint response
     84      [
     85        "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
     86        Services.uuid.generateUUID().toString(),
     87      ],
     88    ],
     89    { hinted: 1, normal: 0 }
     90  );
     91 });
     92 
     93 // Preload twice same origin in secure context
     94 add_task(async function test_103_preload_twice() {
     95  // pass two times the same uuid so that on the second request, the response is
     96  // already in the cache
     97  let uuid = Services.uuid.generateUUID();
     98  await test_hint_preload(
     99    "test_103_preload_twice_1",
    100    "https://example.com",
    101    "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
    102    { hinted: 1, normal: 0 },
    103    uuid
    104  );
    105  await test_hint_preload(
    106    "test_103_preload_twice_2",
    107    "https://example.com",
    108    "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
    109    { hinted: 0, normal: 0 },
    110    uuid
    111  );
    112 });
    113 
    114 // Test that preloads in iframes don't get triggered
    115 add_task(async function test_103_iframe() {
    116  // reset the count
    117  let headers = new Headers();
    118  headers.append("X-Early-Hint-Count-Start", "");
    119  await fetch(
    120    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
    121    { headers }
    122  );
    123 
    124  let iframeUri =
    125    "https://example.com/browser/netwerk/test/browser/103_preload_iframe.html";
    126 
    127  await BrowserTestUtils.withNewTab(
    128    {
    129      gBrowser,
    130      url: iframeUri,
    131      waitForLoad: true,
    132    },
    133    async function () {}
    134  );
    135 
    136  let gotRequestCount = await fetch(
    137    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
    138  ).then(response => response.json());
    139  let expectedRequestCount = { hinted: 0, normal: 1 };
    140 
    141  await request_count_checking(
    142    "test_103_iframe",
    143    gotRequestCount,
    144    expectedRequestCount
    145  );
    146 
    147  Services.cache2.clear();
    148 });
    149 
    150 // Test that anchors are parsed
    151 add_task(async function test_103_anchor() {
    152  // reset the count
    153  let headers = new Headers();
    154  headers.append("X-Early-Hint-Count-Start", "");
    155  await fetch(
    156    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
    157    { headers }
    158  );
    159 
    160  let anchorUri =
    161    "https://example.com/browser/netwerk/test/browser/103_preload_anchor.html";
    162 
    163  await BrowserTestUtils.withNewTab(
    164    {
    165      gBrowser,
    166      url: anchorUri,
    167      waitForLoad: true,
    168    },
    169    async function () {}
    170  );
    171 
    172  let gotRequestCount = await fetch(
    173    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
    174  ).then(response => response.json());
    175 
    176  await request_count_checking("test_103_anchor", gotRequestCount, {
    177    hinted: 0,
    178    normal: 1,
    179  });
    180 });