Skip to content

Truly independent web browser

Tests: Import fetch body `textStream()` WPT tests

Callum Lawf556fdc701c1
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index a95baa18ae1..33a76ead7cb 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -155,6 +155,9 @@ Text/input/wpt-import/xhr/xmlhttprequest-timeout-overridesexpires.html
Text/input/wpt-import/xhr/xmlhttprequest-timeout-worker-overrides.html
Text/input/wpt-import/xhr/xmlhttprequest-timeout-worker-overridesexpires.html
+; The dedicated worker script must be fetched over HTTP.
+Text/input/wpt-import/fetch/api/body/textstream.any.worker.html
+
; fetching a file only works from an HTTP server.
Text/input/wpt-import/fetch/api/basic/text-utf8.any.html
Text/input/wpt-import/fetch/api/abort/general.any.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.txt
new file mode 100644
index 00000000000..d054d79dfe0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.txt
@@ -0,0 +1,20 @@
+Harness status: OK
+
+Found 14 tests
+
+4 Pass
+10 Fail
+Fail textStream method existence
+Fail Response.textStream() basic functionality
+Fail Request.textStream() basic functionality
+Fail textStream() handles chunked byte stream input
+Pass Response.textStream() on consumed body throws TypeError
+Pass Request.textStream() on consumed body throws TypeError
+Pass Response.textStream() on locked body throws TypeError
+Pass Request.textStream() on locked body throws TypeError
+Fail Response.textStream() with null body
+Fail Request.textStream() with null body
+Fail Response.textStream() with empty body
+Fail Response.textStream() ignores Content-Type charset (UTF-16LE)
+Fail Request.textStream() ignores Content-Type charset (UTF-16LE)
+Fail Response.textStream() ignores invalid Content-Type charset (invalid-charset)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.worker.txt
new file mode 100644
index 00000000000..d054d79dfe0
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/body/textstream.any.worker.txt
@@ -0,0 +1,20 @@
+Harness status: OK
+
+Found 14 tests
+
+4 Pass
+10 Fail
+Fail textStream method existence
+Fail Response.textStream() basic functionality
+Fail Request.textStream() basic functionality
+Fail textStream() handles chunked byte stream input
+Pass Response.textStream() on consumed body throws TypeError
+Pass Request.textStream() on consumed body throws TypeError
+Pass Response.textStream() on locked body throws TypeError
+Pass Request.textStream() on locked body throws TypeError
+Fail Response.textStream() with null body
+Fail Request.textStream() with null body
+Fail Response.textStream() with empty body
+Fail Response.textStream() ignores Content-Type charset (UTF-16LE)
+Fail Request.textStream() ignores Content-Type charset (UTF-16LE)
+Fail Response.textStream() ignores invalid Content-Type charset (invalid-charset)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.html
new file mode 100644
index 00000000000..6f0a8955dbf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../../fetch/api/body/textstream.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.js
new file mode 100644
index 00000000000..a908bc232a9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.js
@@ -0,0 +1,177 @@
+// META: global=window,worker
+
+async function readAllChunks(stream) {
+ const reader = stream.getReader();
+ const chunks = [];
+ while (true) {
+ const {done, value} = await reader.read();
+ if (done) {
+ break;
+ }
+ chunks.push(value);
+ }
+ return chunks;
+}
+
+test(() => {
+ assert_true('textStream' in Response.prototype, "textStream exists on Response.prototype");
+ assert_true('textStream' in Request.prototype, "textStream exists on Request.prototype");
+ assert_equals(typeof Response.prototype.textStream, "function", "Response.prototype.textStream is a function");
+ assert_equals(typeof Request.prototype.textStream, "function", "Request.prototype.textStream is a function");
+}, "textStream method existence");
+
+promise_test(async () => {
+ const response = new Response("hello world");
+ assert_false(response.bodyUsed, "bodyUsed is false initially");
+ const stream = response.textStream();
+ assert_true(stream instanceof ReadableStream, "textStream() returns a ReadableStream");
+ assert_true(response.bodyUsed, "bodyUsed becomes true immediately after calling textStream()");
+
+ const chunks = await readAllChunks(stream);
+ assert_greater_than(chunks.length, 0);
+ for (const chunk of chunks) {
+ assert_equals(typeof chunk, "string", "each chunk should be a string");
+ }
+ assert_equals(chunks.join(""), "hello world", "concatenated chunks match the body content");
+}, "Response.textStream() basic functionality");
+
+promise_test(async () => {
+ const request = new Request("https://example.com", {
+ method: "POST",
+ body: "hello world"
+ });
+ assert_false(request.bodyUsed, "bodyUsed is false initially");
+ const stream = request.textStream();
+ assert_true(stream instanceof ReadableStream, "textStream() returns a ReadableStream");
+ assert_true(request.bodyUsed, "bodyUsed becomes true immediately after calling textStream()");
+
+ const chunks = await readAllChunks(stream);
+ assert_greater_than(chunks.length, 0);
+ for (const chunk of chunks) {
+ assert_equals(typeof chunk, "string", "each chunk should be a string");
+ }
+ assert_equals(chunks.join(""), "hello world", "concatenated chunks match the body content");
+}, "Request.textStream() basic functionality");
+
+promise_test(async () => {
+ const stream = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode("hello "));
+ controller.enqueue(new TextEncoder().encode("world"));
+ controller.close();
+ }
+ });
+ const response = new Response(stream);
+ const textStream = response.textStream();
+ const chunks = await readAllChunks(textStream);
+ assert_greater_than(chunks.length, 0);
+ for (const chunk of chunks) {
+ assert_equals(typeof chunk, "string");
+ }
+ assert_equals(chunks.join(""), "hello world");
+}, "textStream() handles chunked byte stream input");
+
+test(() => {
+ const response = new Response("hello");
+ response.text(); // consumes body
+ assert_throws_js(TypeError, () => response.textStream());
+}, "Response.textStream() on consumed body throws TypeError");
+
+test(() => {
+ const request = new Request("https://example.com", {
+ method: "POST",
+ body: "hello"
+ });
+ request.text(); // consumes body
+ assert_throws_js(TypeError, () => request.textStream());
+}, "Request.textStream() on consumed body throws TypeError");
+
+test(() => {
+ const response = new Response("hello");
+ const reader = response.body.getReader(); // locks body
+ assert_throws_js(TypeError, () => response.textStream());
+}, "Response.textStream() on locked body throws TypeError");
+
+test(() => {
+ const request = new Request("https://example.com", {
+ method: "POST",
+ body: "hello"
+ });
+ const reader = request.body.getReader(); // locks body
+ assert_throws_js(TypeError, () => request.textStream());
+}, "Request.textStream() on locked body throws TypeError");
+
+promise_test(async () => {
+ const response = new Response();
+ assert_equals(response.body, null);
+ assert_false(response.bodyUsed, "bodyUsed is false initially");
+ const stream1 = response.textStream();
+ assert_true(stream1 instanceof ReadableStream);
+ assert_false(response.bodyUsed, "bodyUsed remains false after first textStream()");
+ const stream2 = response.textStream();
+ assert_true(stream2 instanceof ReadableStream);
+ assert_false(response.bodyUsed, "bodyUsed remains false after second textStream()");
+ assert_not_equals(stream1, stream2, "multiple calls must return different stream objects");
+ const chunks1 = await readAllChunks(stream1);
+ assert_equals(chunks1.length, 0, "no chunks should be read from first null body textStream");
+ const chunks2 = await readAllChunks(stream2);
+ assert_equals(chunks2.length, 0, "no chunks should be read from second null body textStream");
+}, "Response.textStream() with null body");
+
+promise_test(async () => {
+ const request = new Request("https://example.com");
+ assert_equals(request.body, null);
+ assert_false(request.bodyUsed, "bodyUsed is false initially");
+ const stream1 = request.textStream();
+ assert_true(stream1 instanceof ReadableStream);
+ assert_false(request.bodyUsed, "bodyUsed remains false after first textStream()");
+ const stream2 = request.textStream();
+ assert_true(stream2 instanceof ReadableStream);
+ assert_false(request.bodyUsed, "bodyUsed remains false after second textStream()");
+ assert_not_equals(stream1, stream2, "multiple calls must return different stream objects");
+ const chunks1 = await readAllChunks(stream1);
+ assert_equals(chunks1.length, 0, "no chunks should be read from first null body textStream");
+ const chunks2 = await readAllChunks(stream2);
+ assert_equals(chunks2.length, 0, "no chunks should be read from second null body textStream");
+}, "Request.textStream() with null body");
+
+promise_test(async () => {
+ const response = new Response("");
+ assert_false(response.bodyUsed);
+ const stream = response.textStream();
+ assert_true(stream instanceof ReadableStream);
+ assert_true(response.bodyUsed);
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.length, 0, "no chunks should be read from empty stream");
+}, "Response.textStream() with empty body");
+
+promise_test(async () => {
+ const buffer = new Uint8Array([0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00]); // "hello" in UTF-16LE
+ const response = new Response(buffer, {
+ headers: { "Content-Type": "text/plain; charset=utf-16le" }
+ });
+ const stream = response.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "h\0e\0l\0l\0o\0", "ignores charset=utf-16le and decodes as UTF-8");
+}, "Response.textStream() ignores Content-Type charset (UTF-16LE)");
+
+promise_test(async () => {
+ const buffer = new Uint8Array([0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00]); // "hello" in UTF-16LE
+ const request = new Request("https://example.com", {
+ method: "POST",
+ body: buffer,
+ headers: { "Content-Type": "text/plain; charset=utf-16le" }
+ });
+ const stream = request.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "h\0e\0l\0l\0o\0", "ignores charset=utf-16le and decodes as UTF-8");
+}, "Request.textStream() ignores Content-Type charset (UTF-16LE)");
+
+promise_test(async () => {
+ const response = new Response(new TextEncoder().encode("hello"), {
+ headers: { "Content-Type": "text/plain; charset=invalid-charset" }
+ });
+ const stream = response.textStream();
+ const chunks = await readAllChunks(stream);
+ assert_equals(chunks.join(""), "hello", "ignores invalid-charset and decodes as UTF-8");
+}, "Response.textStream() ignores invalid Content-Type charset (invalid-charset)");
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.html b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.html
new file mode 100644
index 00000000000..b88e52290d3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+fetch_tests_from_worker(new Worker("../../../fetch/api/body/textstream.any.worker.js"));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.js b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.js
new file mode 100644
index 00000000000..1b05a1c6fc7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/body/textstream.any.worker.js
@@ -0,0 +1,10 @@
+
+self.GLOBAL = {
+ isWindow: function() { return false; },
+ isWorker: function() { return true; },
+ isShadowRealm: function() { return false; },
+};
+importScripts("../../../resources/testharness.js");
+
+importScripts("../../../fetch/api/body/textstream.any.js");
+done();