inflight-fetch-helper.js (1753B)
1 // Delay after fetch start: 2 // - 0.0 seconds: before BFCache 3 // - 2.0 seconds: when in BFCache 4 // - 3.5 seconds: after restored from BFCache 5 function runTest(urlToFetch, hasCSP, shouldSucceed, description) { 6 runBfcacheTest({ 7 funcBeforeNavigation: async (urlToFetch, hasCSP) => { 8 if (hasCSP) { 9 // Set CSP. 10 const meta = document.createElement('meta'); 11 meta.setAttribute('http-equiv', 'Content-Security-Policy'); 12 meta.setAttribute('content', "connect-src 'self'"); 13 document.head.appendChild(meta); 14 } 15 16 // Initiate a `fetch()`. 17 window.fetchPromise = fetch(urlToFetch); 18 19 // Wait for 0.5 seconds to receive response headers for the fetch() 20 // before BFCache, if any. 21 await new Promise(resolve => setTimeout(resolve, 500)); 22 }, 23 argsBeforeNavigation: [urlToFetch, hasCSP], 24 funcBeforeBackNavigation: () => { 25 // Wait for 2 seconds before back navigating to pageA. 26 return new Promise(resolve => setTimeout(resolve, 2000)); 27 }, 28 funcAfterAssertion: async (pageA, pageB, t) => { 29 // Wait for fetch() completion and check the result. 30 const result = pageA.execute_script( 31 () => window.fetchPromise.then(r => r.text())); 32 if (shouldSucceed) { 33 assert_equals( 34 await result, 35 'Body', 36 'Fetch should complete successfully after restored from BFCache'); 37 } else { 38 await promise_rejects_js(t, TypeError, result, 39 'Fetch should fail after restored from BFCache'); 40 } 41 } 42 }, 'Eligibility (in-flight fetch): ' + description); 43 } 44 45 const url = new URL('../resources/slow.py', location); 46 const sameOriginUrl = url.href; 47 const crossSiteUrl = originCrossSite + url.pathname;