<!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>
