element-link-prefetch.optional.sub.html (2204B)
1 <!DOCTYPE html> 2 <!-- 3 [%provenance%] 4 --> 5 <html lang="en"> 6 <meta charset="utf-8"> 7 {%- if subtests|length > 10 %} 8 <meta name="timeout" content="long"> 9 {%- endif %} 10 <title>HTTP headers on request for HTML "link" element with rel="prefetch"</title> 11 <script src="/resources/testharness.js"></script> 12 <script src="/resources/testharnessreport.js"></script> 13 <script src="/fetch/metadata/resources/helper.sub.js"></script> 14 <body> 15 <script> 16 'use strict'; 17 18 /** 19 * The `link` element supports a `load` event. That event would reliably 20 * indicate that the browser had received the request. Multiple major 21 * browsers do not implement the event, however, so in order to promote the 22 * visibility of this test, a less efficient polling-based detection 23 * mechanism is used. 24 * 25 * https://bugzilla.mozilla.org/show_bug.cgi?id=1638188 26 * https://bugs.chromium.org/p/chromium/issues/detail?id=1083034 27 */ 28 function induceRequest(t, url, attributes) { 29 const link = document.createElement('link'); 30 link.setAttribute('rel', 'prefetch'); 31 link.setAttribute('href', url); 32 33 for (const [ name, value ] of Object.entries(attributes)) { 34 link.setAttribute(name, value); 35 } 36 37 document.head.appendChild(link); 38 t.add_cleanup(() => link.remove()); 39 } 40 41 setup(() => { 42 assert_implements_optional(document.createElement('link').relList.supports('prefetch')); 43 }); 44 45 {%- for subtest in subtests %} 46 47 promise_test((t) => { 48 const key = '{{uuid()}}'; 49 50 induceRequest( 51 t, 52 makeRequestURL(key, [% subtest.origins %]), 53 [%subtest.elementAttrs | default({}) | tojson%] 54 ); 55 56 return retrieve(key, {poll:true}) 57 .then((headers) => { 58 {%- if subtest.expected == none %} 59 assert_not_own_property(headers, '[%subtest.headerName%]'); 60 {%- else %} 61 assert_own_property(headers, '[%subtest.headerName%]'); 62 assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']); 63 {%- endif %} 64 }); 65 }, '[%subtest.headerName%][%subtest.description | pad("start", " - ")%] [%subtest.elementAttrs | collection("attributes")%]'); 66 67 {%- endfor %} 68 </script> 69 </body> 70 </html>