summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/back-forward-cache/resources/inflight-fetch-helper.js
blob: 7832003b76b9518d5f3588bb5e9f6e0dbd8cea07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Delay after fetch start:
// - 0.0 seconds: before BFCache
// - 2.0 seconds: when in BFCache
// - 3.5 seconds: after restored from BFCache
function runTest(urlToFetch, hasCSP, shouldSucceed, description) {
  runBfcacheTest({
    funcBeforeNavigation: async (urlToFetch, hasCSP) => {
      if (hasCSP) {
        // Set CSP.
        const meta = document.createElement('meta');
        meta.setAttribute('http-equiv', 'Content-Security-Policy');
        meta.setAttribute('content', "connect-src 'self'");
        document.head.appendChild(meta);
      }

      // Initiate a `fetch()`.
      window.fetchPromise = fetch(urlToFetch);

      // Wait for 0.5 seconds to receive response headers for the fetch()
      // before BFCache, if any.
      await new Promise(resolve => setTimeout(resolve, 500));
    },
    argsBeforeNavigation: [urlToFetch, hasCSP],
    funcBeforeBackNavigation: () => {
      // Wait for 2 seconds before back navigating to pageA.
      return new Promise(resolve => setTimeout(resolve, 2000));
    },
    funcAfterAssertion: async (pageA, pageB, t) => {
      // Wait for fetch() completion and check the result.
      const result = pageA.execute_script(
          () => window.fetchPromise.then(r => r.text()));
      if (shouldSucceed) {
        assert_equals(
          await result,
          'Body',
          'Fetch should complete successfully after restored from BFCache');
      } else {
        await promise_rejects_js(t, TypeError, result,
          'Fetch should fail after restored from BFCache');
      }
    }
  }, 'Eligibility (in-flight fetch): ' + description);
}

const url = new URL('../resources/slow.py', location);
const sameOriginUrl = url.href;
const crossSiteUrl = originCrossSite + url.pathname;