link-header-preload-non-html.html (2565B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Makes sure that Link headers preload resources in non-HTML documents</title> 4 <meta name="timeout" content="long"> 5 <script src="resources/dummy.js?link-header-preload2"></script> 6 <script src="/common/utils.js"></script> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/preload/resources/preload_helper.js"></script> 10 <body> 11 <script> 12 13 function test_document_type(options, desc) { 14 promise_test(async t => { 15 const id = token(); 16 const preloadLink = `/html/semantics/document-metadata/the-link-element/stylesheet.py?id=${id}`; 17 const params = new URLSearchParams(); 18 for (const opt in options) 19 params.set(opt, options[opt]); 20 params.set('link', `<${preloadLink}>;rel=preload;as=style`); 21 22 const docURL = getAbsoluteURL(`./resources/echo-preload-header.py?${params.toString()}`); 23 const iframe = document.createElement('iframe'); 24 t.add_cleanup(() => iframe.remove()); 25 iframe.src = docURL; 26 document.body.appendChild(iframe); 27 await new Promise(resolve => iframe.addEventListener('load', resolve)); 28 const timeout = 5000; 29 const interval = 25; 30 let count = 0; 31 const before = performance.now(); 32 33 while (performance.now() < before + timeout) { 34 // count=true returns the number of times the resource was accessed 35 const res = await fetch(preloadLink + '&count=true'); 36 37 // If count is positive, the resource was accessed. 38 count = Number(await res.text()); 39 if (count > 0) 40 break; 41 42 await new Promise(resolve => t.step_timeout(resolve, interval)); 43 } 44 45 assert_equals(count, 1, "verify that request was issued exactly once"); 46 }, `${desc} documents should respect preload Link headers`); 47 } 48 49 test_document_type({ 50 type: 'application/xml', 51 content: `<?xml version="1.0" encoding="utf-8"?> 52 <html xmlns="http://www.w3.org/1999/xhtml"> 53 </html>`}, "XHTML"); 54 test_document_type({content: 'Hello', type: 'text/plain'}, 'plain text'); 55 test_document_type({file: 'square.png', type: 'image/png'}, 'image'); 56 test_document_type({file: 'white.mp4', type: 'video/mp4'}, 'media'); 57 test_document_type({content: 'dummy', type: 'image/png'}, 'invalid image'); 58 </script> 59 </body>