<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>This test validates the parsing of content-type of resources.</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>
</head>
<body>
<script>

// Utility function picked from https://github.com/web-platform-tests/wpt/blob/master/mimesniff/mime-types/charset-parameter.window.js
function isByteCompatible(str) {
  // see https://fetch.spec.whatwg.org/#concept-header-value-normalize
  if(/^[\u0009\u0020\u000A\u000D]+|[\u0009\u0020\u000A\u000D]+$/.test(str)) {
    return "header-value-incompatible";
  }

  for(let i = 0; i < str.length; i++) {
    const charCode = str.charCodeAt(i);
    // See https://fetch.spec.whatwg.org/#concept-header-value
    if(charCode > 0xFF) {
      return "incompatible";
    } else if(charCode === 0x00 || charCode === 0x0A || charCode === 0x0D) {
      return "header-value-incompatible";
    }
  }
  return "compatible";
}

// Test for content-type parsing.
const run_content_type_parsing_tests = (json_entries) => {
  json_entries.forEach( (json_entry, i) => {
    promise_test(async t => {
        let url = "/fetch/content-type/resources/content-type.py?single_header&";
        json_entry.contentType.forEach(val => {
          url += "value=" + encodeURIComponent(val) + "&";
        });
        fetch(url);
        const entry = await new Promise(resolve => new PerformanceObserver((entryList, observer) => {
            observer.disconnect();
            resolve(entryList.getEntries()[0]);
        }).observe({entryTypes: ['resource']}));
        assert_equals(entry.contentType, json_entry["mimeType"]);
    }, "content-type " + i + " : " + json_entry.contentType);
  });
}

// Test for mime-type parsing.
const run_mime_type_parsing_tests = (json_entries) => {
  json_entries.forEach( (val, i) => {
    if(typeof val === "string" || val.navigable === undefined || isByteCompatible(val.input) !== "compatible") {
      return;
    }
    const output = val.output === null ? "" : val.output
    promise_test(async t => {
        let url = `/fetch/content-type/resources/content-type.py?single_header&value=${val.input}`;
        fetch(url);
        const entry = await new Promise(resolve => new PerformanceObserver((entryList, observer) => {
            observer.disconnect();
            resolve(entryList.getEntries()[0]);
        }).observe({entryTypes: ['resource']}));
        assert_equals(entry.contentType, output);
    }, "mime-type " + i + " : " + val.input);
  });
}

Promise.all([
    fetch("/fetch/content-type/resources/content-types.json"),
    fetch("/mimesniff/mime-types/resources/mime-types.json")
  ]).then(([res, res2]) => res.json().then(run_content_type_parsing_tests)
    .then(() => res2.json().then(run_mime_type_parsing_tests)));

</script>
</body>
</html>