Bug 1802814 [wpt PR 37175] - WPT for testing content-type parsing, a=testonly

Automatic update from web-platform-tests
WPT for testing content-type parsing

Browsers may not parse out Content-type headers and mime-type strings correctly. This WPT makes use of the field contentType in PerformanceResourceTiming to validate the parsing that was done.

Bug: 1366706
Change-Id: I87a443f6f8890d5f43147ec96d8c3c70c0185de3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4058549
Commit-Queue: Abin Paul <abin.paul1@gmail.com>
Reviewed-by: Yoav Weiss <yoavweiss@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1081390}

--

wpt-commits: 77eadc1dc505a2d27794c06aa7fdb2d8fefb4df9
wpt-pr: 37175
This commit is contained in:
Abin K Paul 2022-12-11 21:39:40 +00:00 committed by moz-wptsync-bot
parent 554ff0982f
commit c8eafd7f6e

View File

@ -0,0 +1,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>