tor-browser

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

prefetch-single-with-hint.https.html (5683B)


      1 <!DOCTYPE html>
      2 <title>Use for navigation the requested prefetched response annotated with No-Vary-Search hint, if
      3 No-Vary-Search headers also match during navigation</title>
      4 <meta charset="utf-8">
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 <script src="/common/dispatcher/dispatcher.js"></script>
      8 <script src="/common/utils.js"></script>
      9 <script src="../../resources/utils.js"></script>
     10 <script src="../resources/utils.sub.js"></script>
     11 <script src="/common/subset-tests.js"></script>
     12 <script src="hint-test-inputs.js"></script>
     13 
     14 <meta name="variant" content="?1-1">
     15 <meta name="variant" content="?2-2">
     16 <meta name="variant" content="?3-3">
     17 <meta name="variant" content="?4-4">
     18 <meta name="variant" content="?5-5">
     19 <meta name="variant" content="?6-6">
     20 <meta name="variant" content="?7-7">
     21 <meta name="variant" content="?8-8">
     22 <meta name="variant" content="?9-9">
     23 <meta name="variant" content="?10-10">
     24 <meta name="variant" content="?11-11">
     25 <meta name="variant" content="?12-12">
     26 <meta name="variant" content="?13-13">
     27 <meta name="variant" content="?14-14">
     28 <meta name="variant" content="?15-15">
     29 <meta name="variant" content="?16-16">
     30 <meta name="variant" content="?17-17">
     31 <meta name="variant" content="?18-18">
     32 <meta name="variant" content="?19-19">
     33 <meta name="variant" content="?20-20">
     34 <meta name="variant" content="?21-21">
     35 <meta name="variant" content="?22-22">
     36 <meta name="variant" content="?23-23">
     37 <meta name="variant" content="?24-24">
     38 <meta name="variant" content="?25-25">
     39 <meta name="variant" content="?26-26">
     40 <meta name="variant" content="?27-27">
     41 <meta name="variant" content="?28-last">
     42 
     43 <script>
     44  setup(() => assertSpeculationRulesIsSupported());
     45 
     46  /*
     47    remoteAgent: the RemoteContext instance used to communicate between the
     48      test and the window where prefetch/navigation is happening
     49    noVarySearchHeaderValue: the value of No-Vary-Search header to be populated
     50      for the prefetched response
     51    noVarySearchHintValue: the value of No-Vary-Search hint passed in
     52      as expects_no_vary_search hint in prefetch speculation rules.
     53    prefetchQuery: query params to be added to prefetchExecutor url and prefetched
     54    navigateQuery: query params to be added to prefetchExecutor url and navigated to
     55  */
     56  async function prefetchAndNavigate(remoteAgent, noVarySearchHeaderValue, noVarySearchHintValue, prefetchQuery, navigateQuery){
     57    /*
     58    Flow:
     59      * prefetch prefetch_nvs_hint.py?uuid=...&nvs_header=...&otherqueryparams
     60      * the prefetch request above includes no_vary_search_hint in the speculation
     61        rules
     62      * the server blocks progress on this prefetch request on the server side so
     63        from the browser perspective the server is "thinking"
     64      * the test starts navigation to
     65        prefetch_nvs_hint.py?uuid=...&nvs_header=...&otherdifferentqueryparams.
     66        This navigation matches by No-Vary-Search hint the above in
     67        progress prefetch.
     68      * the test fetches prefetch_nvs_hint.py?uuid=...&unblock="unblock"
     69        which unblocks the in progress prefetch so that the in-progress
     70        navigation can continue
     71    */
     72    const prefetch_nvs_hint_server_page = "prefetch_nvs_hint.py";
     73    const prefetchUrl = remoteAgent.getExecutorURL({executor:prefetch_nvs_hint_server_page});
     74    const navigateToUrl = new URL(prefetchUrl);
     75    // Add query params to the url to be prefetched.
     76    const additionalPrefetchedUrlSearchParams = new URLSearchParams(prefetchQuery);
     77    addNoVarySearchHeaderUsingQueryParam(prefetchUrl, noVarySearchHeaderValue);
     78    additionalPrefetchedUrlSearchParams.forEach((value, key) => {
     79      prefetchUrl.searchParams.append(key, value);
     80    });
     81 
     82    await remoteAgent.forceSinglePrefetch(prefetchUrl,
     83        {expects_no_vary_search:noVarySearchHintValue});
     84 
     85    // Add new query params to navigateToUrl to match No-Vary-Search test case.
     86    const additionalNavigateToUrlSearchParams = new URLSearchParams(navigateQuery);
     87    addNoVarySearchHeaderUsingQueryParam(navigateToUrl, noVarySearchHeaderValue);
     88    additionalNavigateToUrlSearchParams.forEach((value, key) => {
     89      navigateToUrl.searchParams.append(key, value);
     90    });
     91    // Url used by fetch in order to unblock the prefetched url
     92    const nvshint_unblock_url = remoteAgent.getExecutorURL(
     93      {executor:prefetch_nvs_hint_server_page, unblock:"unblock"});
     94    await remoteAgent.execute_script((unblock_url) => {
     95      onbeforeunload = (event) => {
     96          fetch(unblock_url);
     97      };
     98    }, [nvshint_unblock_url]);
     99 
    100    // Try navigating to a non-exact prefetched URL that matches by
    101    // No-Vary-Search hint
    102    // Wait for the navigation to finish
    103    await remoteAgent.navigate(navigateToUrl);
    104  }
    105 
    106  function prefetch_no_vary_search_test(description, noVarySearch, noVarySearchHint, prefetchQuery, navigateQuery, shouldUse){
    107    promise_test(async t => {
    108      const agent = await spawnWindow(t, {});
    109      await prefetchAndNavigate(agent,
    110        noVarySearch,
    111        noVarySearchHint,
    112        prefetchQuery,
    113        navigateQuery);
    114 
    115      if (shouldUse) {
    116        assert_prefetched(await agent.getRequestHeaders(),
    117          "Navigation didn't use the prefetched response!");
    118      }
    119      else{
    120        assert_not_prefetched(await agent.getRequestHeaders(),
    121          "Navigation used the prefetched response!");
    122      }
    123     }, description);
    124  }
    125 
    126  hint_test_inputs.forEach(({description, noVarySearch, noVarySearchHint, prefetchQuery, navigateQuery, shouldUse}) => {
    127    subsetTest(prefetch_no_vary_search_test,
    128      description, noVarySearch, noVarySearchHint, prefetchQuery, navigateQuery,
    129      shouldUse);
    130  });
    131 
    132 </script>