summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/sizes-redirect.any.js
blob: e483a4d409c6c2deee73fa213d698b95b865d0c8 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// META: global=window,worker
// META: script=/common/get-host-info.sub.js
// META: script=/resource-timing/resources/sizes-helper.js

const baseUrl =
  new URL('/resource-timing/resources/TAOResponse.py?tao=wildcard', location.href).href;
const expectedSize = 4;

const hostInfo = get_host_info();
performance.clearResourceTimings();

const accumulateEntry = () => {
  return new Promise(resolve => {
    const po = new PerformanceObserver(list => {
      resolve(list);
    });
    po.observe({type: "resource", buffered: true});
  });
};

const checkResourceSizes = () => {
  const entries = performance.getEntriesByType('resource');
  for (let entry of entries) {
    checkSizeFields(entry, expectedSize, expectedSize + headerSize);
  }
}

const redirectUrl = (redirectSourceOrigin, allowOrigin, targetUrl) => {
  return redirectSourceOrigin +
    '/resource-timing/resources/redirect-cors.py?allow_origin=' +
    encodeURIComponent(allowOrigin) +
    '&timing_allow_origin=*' +
    '&location=' + encodeURIComponent(targetUrl);
}

promise_test(() => {
  // Use a different URL every time so that the cache behaviour does not
  // depend on execution order.
  const directUrl = cacheBustUrl(baseUrl);
  const sameOriginRedirect = redirectUrl(hostInfo.ORIGIN, '*', directUrl);
  const crossOriginRedirect = redirectUrl(hostInfo.REMOTE_ORIGIN,
    hostInfo.ORIGIN, directUrl);
  const mixedRedirect = redirectUrl(hostInfo.REMOTE_ORIGIN,
    hostInfo.ORIGIN, sameOriginRedirect);
  const complexRedirect = redirectUrl(hostInfo.ORIGIN,
    hostInfo.REMOTE_ORIGIN, mixedRedirect);
  let eatBody = response => response.arrayBuffer();
  return fetch(directUrl)
    .then(eatBody)
    .then(() => fetch(sameOriginRedirect))
    .then(eatBody)
    .then(() => fetch(crossOriginRedirect))
    .then(eatBody)
    .then(() => fetch(mixedRedirect))
    .then(eatBody)
    .then(() => fetch(complexRedirect))
    .then(eatBody)
    .then(accumulateEntry)
    .then(checkResourceSizes);
}, 'PerformanceResourceTiming sizes Fetch with redirect test');

done();