summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/cross-origin-status-codes.html
blob: 197a7663396ef66c7764d6599b03854d138f2e60 (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
63
64
65
66
67
68
69
70
<!doctype html>
<html>
<head>
<title>Resource Timing: PerformanceResourceTiming attributes shouldn't change
  if the HTTP status code changes</title>
<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#sec-performanceresourcetiming"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/common/get-host-info.sub.js></script>
</head>
<body>
<img id="img_200">
<img id="img_307">
<img id="img_404">
<img id="img_502">
<script id="script_200"></script>
<script id="script_307"></script>
<script id="script_404"></script>
<script id="script_502"></script>
<script>

const listenForPerformanceEntries = num_expected => {
  return new Promise(resolve => {
    let results = [];
    new PerformanceObserver(entryList => {
      entryList.getEntries().forEach(entry => {
        if (!entry.name.includes("status-code"))
          return;

        results.push(entry);
        if (results.length == num_expected) {
          resolve(results);
        }
      });
    }).observe({entryTypes: ['resource']});
  });
}

promise_test(async t => {
  const destUrl = get_host_info().HTTP_REMOTE_ORIGIN + '/resource-timing/resources/';
  const statusCodes = ['200', '307', '404', '502'];

  let expected_entry_count = 0;
  statusCodes.forEach(status => {
    document.getElementById(`img_${status}`).src = `${destUrl}status-code.py?status=${status}`;
    document.getElementById(`script_${status}`).src = `${destUrl}status-code.py?status=${status}&script=1`;
    expected_entry_count += 2;
  });

  const entries = await listenForPerformanceEntries(expected_entry_count);

  // We will check that the non-timestamp values of the entry match for all
  // entries.
  const keys = [
    'entryType',
    'nextHopProtocol',
    'transferSize',
    'encodedBodySize',
    'decodedBodySize',
  ];

  const first = entries[0];
  entries.slice(1).forEach(entry => {
    keys.forEach(attribute => {
      assert_equals(entry[attribute], first[attribute],
        `There must be no discernible difference for the ${attribute} ` +
        `attribute but found a difference for the ${entry.name} resource.`);
  })});
}, "Make sure cross origin resource fetch failures with different status codes are indistinguishable");
</script>