summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/content-type/response.window.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /testing/web-platform/tests/fetch/content-type/response.window.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/fetch/content-type/response.window.js')
-rw-r--r--testing/web-platform/tests/fetch/content-type/response.window.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/testing/web-platform/tests/fetch/content-type/response.window.js b/testing/web-platform/tests/fetch/content-type/response.window.js
new file mode 100644
index 0000000000..746f51c9bb
--- /dev/null
+++ b/testing/web-platform/tests/fetch/content-type/response.window.js
@@ -0,0 +1,72 @@
+promise_test(() => {
+ return fetch("resources/content-types.json").then(res => res.json()).then(runTests);
+}, "Loading JSONā€¦");
+
+function runTests(tests) {
+ tests.forEach(testUnit => {
+ runFrameTest(testUnit, false);
+ runFrameTest(testUnit, true);
+ runFetchTest(testUnit, false);
+ runFetchTest(testUnit, true);
+ runRequestResponseTest(testUnit, "Request");
+ runRequestResponseTest(testUnit, "Response");
+ });
+}
+
+function runFrameTest(testUnit, singleHeader) {
+ // Note: window.js is always UTF-8
+ const encoding = testUnit.encoding !== null ? testUnit.encoding : "UTF-8";
+ async_test(t => {
+ const frame = document.body.appendChild(document.createElement("iframe"));
+ t.add_cleanup(() => frame.remove());
+ frame.src = getURL(testUnit.contentType, singleHeader);
+ frame.onload = t.step_func_done(() => {
+ // Edge requires toUpperCase()
+ const doc = frame.contentDocument;
+ assert_equals(doc.characterSet.toUpperCase(), encoding.toUpperCase());
+ if (testUnit.documentContentType === "text/plain") {
+ assert_equals(doc.body.textContent, "<b>hi</b>\n");
+ } else if (testUnit.documentContentType === "text/html") {
+ assert_equals(doc.body.firstChild.localName, "b");
+ assert_equals(doc.body.firstChild.textContent, "hi");
+ }
+ assert_equals(doc.contentType, testUnit.documentContentType);
+ });
+ }, getDesc("<iframe>", testUnit.contentType, singleHeader));
+}
+
+function getDesc(type, input, singleHeader) {
+ return type + ": " + (singleHeader ? "combined" : "separate") + " response Content-Type: " + input.join(" ");
+}
+
+function getURL(input, singleHeader) {
+ // Edge does not support URLSearchParams
+ let url = "resources/content-type.py?"
+ if (singleHeader) {
+ url += "single_header&"
+ }
+ input.forEach(val => {
+ url += "value=" + encodeURIComponent(val) + "&";
+ });
+ return url;
+}
+
+function runFetchTest(testUnit, singleHeader) {
+ promise_test(async t => {
+ const blob = await (await fetch(getURL(testUnit.contentType, singleHeader))).blob();
+ assert_equals(blob.type, testUnit.mimeType);
+ }, getDesc("fetch()", testUnit.contentType, singleHeader));
+}
+
+function runRequestResponseTest(testUnit, stringConstructor) {
+ promise_test(async t => {
+ // Cannot give Response a body as that will set Content-Type, but Request needs a URL
+ const constructorArgument = stringConstructor === "Request" ? "about:blank" : undefined;
+ const r = new self[stringConstructor](constructorArgument);
+ testUnit.contentType.forEach(val => {
+ r.headers.append("Content-Type", val);
+ });
+ const blob = await r.blob();
+ assert_equals(blob.type, testUnit.mimeType);
+ }, getDesc(stringConstructor, testUnit.contentType, true));
+}