summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/body
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/api/body
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/api/body')
-rw-r--r--testing/web-platform/tests/fetch/api/body/cloned-any.js50
-rw-r--r--testing/web-platform/tests/fetch/api/body/formdata.any.js14
-rw-r--r--testing/web-platform/tests/fetch/api/body/mime-type.any.js127
3 files changed, 191 insertions, 0 deletions
diff --git a/testing/web-platform/tests/fetch/api/body/cloned-any.js b/testing/web-platform/tests/fetch/api/body/cloned-any.js
new file mode 100644
index 0000000000..2bca96c704
--- /dev/null
+++ b/testing/web-platform/tests/fetch/api/body/cloned-any.js
@@ -0,0 +1,50 @@
+// Changing the body after it have been passed to Response/Request
+// should not change the outcome of the consumed body
+
+const url = 'http://a';
+const method = 'post';
+
+promise_test(async t => {
+ const body = new FormData();
+ body.set('a', '1');
+ const res = new Response(body);
+ const req = new Request(url, { method, body });
+ body.set('a', '2');
+ assert_true((await res.formData()).get('a') === '1');
+ assert_true((await req.formData()).get('a') === '1');
+}, 'FormData is cloned');
+
+promise_test(async t => {
+ const body = new URLSearchParams({a: '1'});
+ const res = new Response(body);
+ const req = new Request(url, { method, body });
+ body.set('a', '2');
+ assert_true((await res.formData()).get('a') === '1');
+ assert_true((await req.formData()).get('a') === '1');
+}, 'URLSearchParams is cloned');
+
+promise_test(async t => {
+ const body = new Uint8Array([97]); // a
+ const res = new Response(body);
+ const req = new Request(url, { method, body });
+ body[0] = 98; // b
+ assert_true(await res.text() === 'a');
+ assert_true(await req.text() === 'a');
+}, 'TypedArray is cloned');
+
+promise_test(async t => {
+ const body = new Uint8Array([97]); // a
+ const res = new Response(body.buffer);
+ const req = new Request(url, { method, body: body.buffer });
+ body[0] = 98; // b
+ assert_true(await res.text() === 'a');
+ assert_true(await req.text() === 'a');
+}, 'ArrayBuffer is cloned');
+
+promise_test(async t => {
+ const body = new Blob(['a']);
+ const res = new Response(body);
+ const req = new Request(url, { method, body });
+ assert_true(await res.blob() !== body);
+ assert_true(await req.blob() !== body);
+}, 'Blob is cloned');
diff --git a/testing/web-platform/tests/fetch/api/body/formdata.any.js b/testing/web-platform/tests/fetch/api/body/formdata.any.js
new file mode 100644
index 0000000000..e25035923c
--- /dev/null
+++ b/testing/web-platform/tests/fetch/api/body/formdata.any.js
@@ -0,0 +1,14 @@
+promise_test(async t => {
+ const res = new Response(new FormData());
+ const fd = await res.formData();
+ assert_true(fd instanceof FormData);
+}, 'Consume empty response.formData() as FormData');
+
+promise_test(async t => {
+ const req = new Request('about:blank', {
+ method: 'POST',
+ body: new FormData()
+ });
+ const fd = await req.formData();
+ assert_true(fd instanceof FormData);
+}, 'Consume empty request.formData() as FormData');
diff --git a/testing/web-platform/tests/fetch/api/body/mime-type.any.js b/testing/web-platform/tests/fetch/api/body/mime-type.any.js
new file mode 100644
index 0000000000..67c9af7da2
--- /dev/null
+++ b/testing/web-platform/tests/fetch/api/body/mime-type.any.js
@@ -0,0 +1,127 @@
+[
+ () => new Request("about:blank", { headers: { "Content-Type": "text/plain" } }),
+ () => new Response("", { headers: { "Content-Type": "text/plain" } })
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
+ const newMIMEType = "test/test";
+ bodyContainer.headers.set("Content-Type", newMIMEType);
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, newMIMEType);
+ }, `${bodyContainer.constructor.name}: overriding explicit Content-Type`);
+});
+
+[
+ () => new Request("about:blank", { body: new URLSearchParams(), method: "POST" }),
+ () => new Response(new URLSearchParams()),
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ assert_equals(bodyContainer.headers.get("Content-Type"), "application/x-www-form-urlencoded;charset=UTF-8");
+ bodyContainer.headers.delete("Content-Type");
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, "");
+ }, `${bodyContainer.constructor.name}: removing implicit Content-Type`);
+});
+
+[
+ () => new Request("about:blank", { body: new ArrayBuffer(), method: "POST" }),
+ () => new Response(new ArrayBuffer()),
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ assert_equals(bodyContainer.headers.get("Content-Type"), null);
+ const newMIMEType = "test/test";
+ bodyContainer.headers.set("Content-Type", newMIMEType);
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, newMIMEType);
+ }, `${bodyContainer.constructor.name}: setting missing Content-Type`);
+});
+
+[
+ () => new Request("about:blank", { method: "POST" }),
+ () => new Response(),
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, "");
+ }, `${bodyContainer.constructor.name}: MIME type for Blob from empty body`);
+});
+
+[
+ () => new Request("about:blank", { method: "POST", headers: [["Content-Type", "Mytext/Plain"]] }),
+ () => new Response("", { headers: [["Content-Type", "Mytext/Plain"]] })
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, 'mytext/plain');
+ }, `${bodyContainer.constructor.name}: MIME type for Blob from empty body with Content-Type`);
+});
+
+[
+ () => new Request("about:blank", { body: new Blob([""]), method: "POST" }),
+ () => new Response(new Blob([""]))
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, "");
+ assert_equals(bodyContainer.headers.get("Content-Type"), null);
+ }, `${bodyContainer.constructor.name}: MIME type for Blob`);
+});
+
+[
+ () => new Request("about:blank", { body: new Blob([""], { type: "Text/Plain" }), method: "POST" }),
+ () => new Response(new Blob([""], { type: "Text/Plain" }))
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, "text/plain");
+ assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
+ }, `${bodyContainer.constructor.name}: MIME type for Blob with non-empty type`);
+});
+
+[
+ () => new Request("about:blank", { method: "POST", body: new Blob([""], { type: "Text/Plain" }), headers: [["Content-Type", "Text/Html"]] }),
+ () => new Response(new Blob([""], { type: "Text/Plain" }, { headers: [["Content-Type", "Text/Html"]] }))
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ const cloned = bodyContainer.clone();
+ promise_test(async t => {
+ const blobs = [await bodyContainer.blob(), await cloned.blob()];
+ assert_equals(blobs[0].type, "text/html");
+ assert_equals(blobs[1].type, "text/html");
+ assert_equals(bodyContainer.headers.get("Content-Type"), "Text/Html");
+ assert_equals(cloned.headers.get("Content-Type"), "Text/Html");
+ }, `${bodyContainer.constructor.name}: Extract a MIME type with clone`);
+});
+
+[
+ () => new Request("about:blank", { body: new Blob([], { type: "text/plain" }), method: "POST", headers: [["Content-Type", "text/html"]] }),
+ () => new Response(new Blob([], { type: "text/plain" }), { headers: [["Content-Type", "text/html"]] }),
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ assert_equals(bodyContainer.headers.get("Content-Type"), "text/html");
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, "text/html");
+ }, `${bodyContainer.constructor.name}: Content-Type in headers wins Blob"s type`);
+});
+
+[
+ () => new Request("about:blank", { body: new Blob([], { type: "text/plain" }), method: "POST" }),
+ () => new Response(new Blob([], { type: "text/plain" })),
+].forEach(bodyContainerCreator => {
+ const bodyContainer = bodyContainerCreator();
+ promise_test(async t => {
+ assert_equals(bodyContainer.headers.get("Content-Type"), "text/plain");
+ const newMIMEType = "text/html";
+ bodyContainer.headers.set("Content-Type", newMIMEType);
+ const blob = await bodyContainer.blob();
+ assert_equals(blob.type, newMIMEType);
+ }, `${bodyContainer.constructor.name}: setting missing Content-Type in headers and it wins Blob"s type`);
+});