tor-browser

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

preconnect-onerror-event.html (2310B)


      1 <!DOCTYPE html>
      2 <html>
      3 <title>Makes sure that preconnected resources do not trigger load or error events</title>
      4 <meta name="timeout" content="long">
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 <script src="/common/get-host-info.sub.js"></script>
      8 
      9 <body>
     10 <script>
     11    const {HTTP_REMOTE_ORIGIN} = get_host_info();
     12 
     13    /**
     14     * Test workflow:
     15     * - Create preconnect link
     16     * - Wait 200 ms
     17     * - Create preload link
     18     * Event listeners for 'load' and 'error' are attached to both links.
     19     * The first event listener that fires is tested to see which element fired it.
     20     * The event should always be fired by the preload element because per spec, the preconnect link should never fire these events:
     21     * https://html.spec.whatwg.org/#link-type-preconnect
     22     *
     23     * This test assumes that the preload link element fires 'load' and 'error' events correctly, otherwise the test will timeout.
     24     */
     25    function test_preconnect(origin, resource, desc) {
     26        promise_test(async t => {
     27            const result = await new Promise(async didLoad => {
     28                const href = `${origin}${resource}`;
     29                for (const rel of ['preconnect', 'preload']) {
     30                    const link = document.createElement('link');
     31                    link.href = href;
     32                    link.as = 'script';
     33                    link.rel = rel;
     34                    link.addEventListener('load', () => didLoad({rel, type: 'load'}));
     35                    link.addEventListener('error', () => didLoad({rel, type: 'error'}));
     36                    document.head.appendChild(link);
     37                    await new Promise(resolve => t.step_timeout(resolve, 200));
     38                }
     39            });
     40            assert_equals(result.rel, 'preload');
     41        }, desc);
     42    }
     43 
     44    test_preconnect(HTTP_REMOTE_ORIGIN, '/preload/resources/dummy.js', 'Preconnect should not fire load (or error) events');
     45    test_preconnect('http://NON-EXISTENT.origin', '/preload/resources/dummy.js', 'Preconnect should not fire error (or load) events for non-existent origins');
     46    test_preconnect('some-scheme://URL', '/preload/resources/dummy.js', 'Preconnect should not fire error (or load) events for non-http(s) scheme');
     47 </script>
     48 </body>
     49 </html>