speculative-parsing-util.js (2356B)
1 function expect_fetched_onload(uuid, expectation) { 2 return new Promise(resolve => { 3 addEventListener('load', resolve); 4 }).then(async () => { 5 const result = await get_result(uuid); 6 if (expectation) { 7 assert_not_equals(result, '', 'speculative case did not fetch'); 8 } else { 9 assert_equals(result, '', 'speculative case incorrectly fetched'); 10 } 11 return result; 12 }); 13 } 14 15 function compare_with_nonspeculative(uuid, title, test_nonspeculative) { 16 return function(speculative_result) { 17 if (!test_nonspeculative) { 18 return Promise.resolve(); 19 } 20 return new Promise(resolve => { 21 const iframe = document.createElement('iframe'); 22 iframe.onload = resolve; 23 iframe.src = `../resources/${title}-nonspeculative.sub.html?uuid=${uuid}`; 24 document.body.appendChild(iframe); 25 }).then(async () => { 26 const result = await get_result(uuid); 27 if (speculative_result === '') { 28 assert_equals(result, '', 'non-speculative case incorrectly fetched') 29 } else { 30 assert_not_equals(result, '', 'non-speculative case did not fetch'); 31 const speculative_headers = speculative_result.trim().split("\n"); 32 const nonspeculative_headers = result.trim().split("\n"); 33 assert_equals(speculative_headers.length, nonspeculative_headers.length, 'expected the same number of headers between speculative and non-speculative') 34 for (let i = 0; i < speculative_headers.length; ++i) { 35 let [s_header, s_value] = split_header(speculative_headers[i]); 36 let [ns_header, ns_value] = split_header(nonspeculative_headers[i]); 37 assert_equals(s_header, ns_header, 'expected the order of headers to match between speculative and non-speculative'); 38 assert_equals(s_value, ns_value, `expected \`${s_header}\` values to match between speculative and non-speculative`); 39 } 40 } 41 }); 42 } 43 } 44 45 function split_header(line) { 46 let [header, value] = line.split(': '); 47 header = header.toLowerCase(); 48 value = value.trim(); 49 if (header === 'referer') { 50 value = value.replace(/\/generated\/.+$/, '/generated/...'); 51 } 52 return [header, value]; 53 } 54 55 async function get_result(uuid) { 56 const response = await fetch(`/html/syntax/speculative-parsing/resources/stash.py?action=take&uuid=${uuid}`); 57 return await response.text(); 58 }