tor-browser

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

keepalive.any.js (2431B)


      1 // META: global=window
      2 // META: timeout=long
      3 // META: title=Fetch API: keepalive handling
      4 // META: script=/common/utils.js
      5 // META: script=/common/get-host-info.sub.js
      6 // META: script=../resources/keepalive-helper.js
      7 
      8 'use strict';
      9 
     10 const {
     11  HTTP_NOTSAMESITE_ORIGIN,
     12  HTTP_REMOTE_ORIGIN,
     13  HTTP_REMOTE_ORIGIN_WITH_DIFFERENT_PORT
     14 } = get_host_info();
     15 
     16 /**
     17 * In a different-site iframe, test to fetch a keepalive URL on the specified
     18 * document event.
     19 */
     20 function keepaliveSimpleRequestTest(method) {
     21  for (const evt of ['load', 'unload', 'pagehide']) {
     22    const desc =
     23        `[keepalive] simple ${method} request on '${evt}' [no payload]`;
     24    promise_test(async (test) => {
     25      const token1 = token();
     26      const iframe = document.createElement('iframe');
     27      iframe.src = getKeepAliveIframeUrl(token1, method, {sendOn: evt});
     28      document.body.appendChild(iframe);
     29      await iframeLoaded(iframe);
     30      if (evt != 'load') {
     31        iframe.remove();
     32      }
     33 
     34      assertStashedTokenAsync(desc, token1);
     35    }, `${desc}; setting up`);
     36  }
     37 }
     38 
     39 for (const method of ['GET', 'POST']) {
     40  keepaliveSimpleRequestTest(method);
     41 }
     42 
     43 // verifies fetch keepalive requests from a worker
     44 function keepaliveSimpleWorkerTest() {
     45    const desc =
     46        `simple keepalive test for web workers`;
     47    promise_test(async (test) => {
     48      const TOKEN = token();
     49      const FRAME_ORIGIN = new URL(location.href).origin;
     50      const TEST_URL = get_host_info().HTTP_ORIGIN + `/fetch/api/resources/stash-put.py?key=${TOKEN}&value=on`
     51    + `&frame_origin=${FRAME_ORIGIN}`;
     52      // start a worker which sends keepalive request and immediately terminates
     53      const worker = new Worker(`/fetch/api/resources/keepalive-worker.js?param=${TEST_URL}`);
     54 
     55      const keepAliveWorkerPromise = new Promise((resolve, reject) => {
     56        worker.onmessage = (event) => {
     57          if (event.data === 'started') {
     58            resolve();
     59          } else {
     60            reject(new Error("Unexpected message received from worker"));
     61          }
     62        };
     63        worker.onerror = (error) => {
     64          reject(error);
     65        };
     66      });
     67 
     68      // wait until the worker has been initialized (indicated by the "started" message)
     69      await keepAliveWorkerPromise;
     70      // verifies if the token sent in fetch request has been updated in the server
     71      assertStashedTokenAsync(desc, TOKEN);
     72 
     73    }, `${desc};`);
     74 
     75 }
     76 
     77 keepaliveSimpleWorkerTest();