tor-browser

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

fetch-request-css-base-url.https.html (3205B)


      1 <!DOCTYPE html>
      2 <title>Service Worker: CSS's base URL must be the response URL</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="resources/test-helpers.sub.js?pipe=sub"></script>
      6 <script>
      7 const SCOPE = 'resources/fetch-request-css-base-url-iframe.html';
      8 const SCRIPT = 'resources/fetch-request-css-base-url-worker.js';
      9 let worker;
     10 
     11 var signalMessage;
     12 function getNextMessage() {
     13  return new Promise(resolve => { signalMessage = resolve; });
     14 }
     15 
     16 promise_test(async (t) => {
     17  const registration = await service_worker_unregister_and_register(
     18      t, SCRIPT, SCOPE);
     19  worker = registration.installing;
     20  await wait_for_state(t, worker, 'activated');
     21 }, 'global setup');
     22 
     23 // Creates a test concerning the base URL of a stylesheet. It loads a
     24 // stylesheet from a controlled page. The stylesheet makes a subresource
     25 // request for an image. The service worker messages back the details of the
     26 // image request in order to test the base URL.
     27 //
     28 // The request URL for the stylesheet is under "resources/request-url-path/".
     29 // The service worker may respond in a way such that the response URL is
     30 // different to the request URL.
     31 function base_url_test(params) {
     32  promise_test(async (t) => {
     33    let frame;
     34    t.add_cleanup(() => {
     35      if (frame)
     36        frame.remove();
     37    });
     38 
     39    // Ask the service worker to message this page once it gets the request
     40    // for the image.
     41    let channel = new MessageChannel();
     42    const sawPong = getNextMessage();
     43    channel.port1.onmessage = (event) => {
     44      signalMessage(event.data);
     45    };
     46    worker.postMessage({port:channel.port2},[channel.port2]);
     47 
     48    // It sends a pong back immediately. This ping/pong protocol helps deflake
     49    // the test for browsers where message/fetch ordering isn't guaranteed.
     50    assert_equals('pong', await sawPong);
     51 
     52    // Load the frame which will load the stylesheet that makes the image
     53    // request.
     54    const sawResult = getNextMessage();
     55    frame = await with_iframe(params.framePath);
     56    const result = await sawResult;
     57 
     58    // Test the image request.
     59    const base = new URL('.', document.location).href;
     60    assert_equals(result.url,
     61                  base + params.expectImageRequestPath,
     62                  'request');
     63    assert_equals(result.referrer,
     64                  base + params.expectImageRequestReferrer,
     65                  'referrer');
     66  }, params.description);
     67 }
     68 
     69 const cssFile = 'fetch-request-css-base-url-style.css';
     70 
     71 base_url_test({
     72  framePath: SCOPE + '?fetch',
     73  expectImageRequestPath: 'resources/sample.png',
     74  expectImageRequestReferrer: `resources/${cssFile}?fetch`,
     75  description: 'base URL when service worker does respondWith(fetch(responseUrl)).'});
     76 
     77 base_url_test({
     78  framePath: SCOPE + '?newResponse',
     79  expectImageRequestPath: 'resources/request-url-path/sample.png',
     80  expectImageRequestReferrer: `resources/request-url-path/${cssFile}?newResponse`,
     81  description: 'base URL when service worker does respondWith(new Response()).'});
     82 
     83 // Cleanup step: this must be the last promise_test.
     84 promise_test(async (t) => {
     85  return service_worker_unregister(t, SCOPE);
     86 }, 'cleanup global state');
     87 </script>