summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/content-type-parsing.html
blob: c0081eb4137d9f38366484bbcdf7aa48e9ca25ae (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
71
72
73
74
75
76
<!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>