File: preconnect-onerror-event.html

package info (click to toggle)
firefox 144.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,637,504 kB
  • sloc: cpp: 7,576,692; javascript: 6,430,831; ansic: 3,748,119; python: 1,398,978; xml: 628,810; asm: 438,679; java: 186,194; sh: 63,212; makefile: 19,159; objc: 13,086; perl: 12,986; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (49 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html>
<title>Makes sure that preconnected resources do not trigger load or error events</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>

<body>
<script>
    const {HTTP_REMOTE_ORIGIN} = get_host_info();

    /**
     * Test workflow:
     * - Create preconnect link
     * - Wait 200 ms
     * - Create preload link
     * Event listeners for 'load' and 'error' are attached to both links.
     * The first event listener that fires is tested to see which element fired it.
     * The event should always be fired by the preload element because per spec, the preconnect link should never fire these events:
     * https://html.spec.whatwg.org/#link-type-preconnect
     *
     * This test assumes that the preload link element fires 'load' and 'error' events correctly, otherwise the test will timeout.
     */
    function test_preconnect(origin, resource, desc) {
        promise_test(async t => {
            const result = await new Promise(async didLoad => {
                const href = `${origin}${resource}`;
                for (const rel of ['preconnect', 'preload']) {
                    const link = document.createElement('link');
                    link.href = href;
                    link.as = 'script';
                    link.rel = rel;
                    link.addEventListener('load', () => didLoad({rel, type: 'load'}));
                    link.addEventListener('error', () => didLoad({rel, type: 'error'}));
                    document.head.appendChild(link);
                    await new Promise(resolve => t.step_timeout(resolve, 200));
                }
            });
            assert_equals(result.rel, 'preload');
        }, desc);
    }

    test_preconnect(HTTP_REMOTE_ORIGIN, '/preload/resources/dummy.js', 'Preconnect should not fire load (or error) events');
    test_preconnect('http://NON-EXISTENT.origin', '/preload/resources/dummy.js', 'Preconnect should not fire error (or load) events for non-existent origins');
    test_preconnect('some-scheme://URL', '/preload/resources/dummy.js', 'Preconnect should not fire error (or load) events for non-http(s) scheme');
</script>
</body>
</html>