summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/preload/link-header-preload-non-html.html
blob: c990e610d9c1f7305b85111e59d36b96352a8a03 (plain)
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
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<meta charset=utf-8>
<title>Makes sure that Link headers preload resources in non-HTML documents</title>
<meta name="timeout" content="long">
<script src="resources/dummy.js?link-header-preload2"></script>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/preload/resources/preload_helper.js"></script>
<body>
<script>

    function test_document_type(options, desc) {
        promise_test(async t => {
            const id = token();
            const preloadLink = `/html/semantics/document-metadata/the-link-element/stylesheet.py?id=${id}`;
            const params = new URLSearchParams();
            for (const opt in options)
                params.set(opt, options[opt]);
            params.set('link', `<${preloadLink}>;rel=preload;as=style`);

            const docURL = getAbsoluteURL(`./resources/echo-preload-header.py?${params.toString()}`);
            const iframe = document.createElement('iframe');
            t.add_cleanup(() => iframe.remove());
            iframe.src = docURL;
            document.body.appendChild(iframe);
            await new Promise(resolve => iframe.addEventListener('load', resolve));
            const timeout = 5000;
            const interval = 25;
            let count = 0;
            const before = performance.now();

            while (performance.now() < before + timeout) {
                // count=true returns the number of times the resource was accessed
                const res = await fetch(preloadLink + '&count=true');

                // If count is positive, the resource was accessed.
                count = Number(await res.text());
                if (count > 0)
                    break;

                await new Promise(resolve => t.step_timeout(resolve, interval));
            }

            assert_equals(count, 1, "verify that request was issued exactly once");
        }, `${desc} documents should respect preload Link headers`);
    }

    test_document_type({
        type: 'application/xml',
        content: `<?xml version="1.0" encoding="utf-8"?>
        <html xmlns="http://www.w3.org/1999/xhtml">
        </html>`}, "XHTML");
    test_document_type({content: 'Hello', type: 'text/plain'}, 'plain text');
    test_document_type({file: 'square.png', type: 'image/png'}, 'image');
    test_document_type({file: 'white.mp4', type: 'video/mp4'}, 'media');
    test_document_type({content: 'dummy', type: 'image/png'}, 'invalid image');
</script>
</body>