headers.optional.html (1497B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <meta name=timeout content=long> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/utils.js"></script> 7 8 <iframe id="i" src="/common/blank.html"></iframe> 9 10 <!-- Test is optional because hyperlink auditing is optional. --> 11 12 <script> 13 promise_test(async t => { 14 await new Promise(resolve => window.onload = resolve); 15 16 const id = token(); 17 18 const el = document.createElement("a"); 19 el.ping = new URL(`resources/stash-headers.py?id=${id}`, location.href); // this will be a POST 20 el.href = "/common/blank.html?1"; 21 22 i.contentDocument.body.append(el); 23 el.click(); 24 25 let headers; 26 await pollForConditionFunc(t, async () => { 27 const res = await fetch(el.ping); // this will be a GET 28 const json = await res.json(); 29 30 if (json !== "no headers yet") { 31 headers = json; 32 return true; 33 } 34 return false; 35 }); 36 37 assert_equals(headers["content-type"], "text/ping", "content-type"); 38 assert_equals(headers["ping-from"], i.src, "ping-from"); 39 assert_equals(headers["ping-to"], el.href, "ping-to"); 40 }); 41 42 async function pollForConditionFunc(t, func, timeout = 3000, interval = 100) { 43 let remaining = Math.ceil(timeout / interval); 44 45 while (remaining > 0) { 46 --remaining; 47 await new Promise(resolve => t.step_timeout(resolve, interval)); 48 49 if (await func()) { 50 return; 51 } 52 } 53 54 assert_true(false, "Condition never became true"); 55 } 56 </script>