aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/streams/streams.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/js/web/streams/streams.test.js')
-rw-r--r--test/js/web/streams/streams.test.js162
1 files changed, 158 insertions, 4 deletions
diff --git a/test/js/web/streams/streams.test.js b/test/js/web/streams/streams.test.js
index 5fc25b37c..55843cb23 100644
--- a/test/js/web/streams/streams.test.js
+++ b/test/js/web/streams/streams.test.js
@@ -1,13 +1,167 @@
import { file, readableStreamToArrayBuffer, readableStreamToArray, readableStreamToText, ArrayBufferSink } from "bun";
-import { expect, it, beforeEach, afterEach, describe } from "bun:test";
+import { expect, it, beforeEach, afterEach, describe, test } from "bun:test";
import { mkfifo } from "mkfifo";
import { realpathSync, unlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "os";
-import { gc } from "harness";
-beforeEach(() => gc());
-afterEach(() => gc());
+describe("readableStreamToFormData", () => {
+ const fixtures = {
+ withTextFile: [
+ [
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n",
+ 'Content-Disposition: form-data; name="file"; filename="test.txt"\r\n',
+ "Content-Type: text/plain\r\n",
+ "\r\n",
+ "hello world",
+ "\r\n",
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n",
+ "\r\n",
+ ],
+ (() => {
+ const fd = new FormData();
+ fd.append("file", new Blob(["hello world"]), "test.txt");
+ return fd;
+ })(),
+ ],
+ withTextFileAndField: [
+ [
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n",
+ 'Content-Disposition: form-data; name="field"\r\n',
+ "\r\n",
+ "value",
+ "\r\n",
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n",
+ 'Content-Disposition: form-data; name="file"; filename="test.txt"\r\n',
+ "Content-Type: text/plain\r\n",
+ "\r\n",
+ "hello world",
+ "\r\n",
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n",
+ "\r\n",
+ ],
+ (() => {
+ const fd = new FormData();
+ fd.append("file", new Blob(["hello world"]), "test.txt");
+ fd.append("field", "value");
+ return fd;
+ })(),
+ ],
+
+ with1Field: [
+ [
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW\r\n",
+ 'Content-Disposition: form-data; name="field"\r\n',
+ "\r\n",
+ "value",
+ "\r\n",
+ "--WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n",
+ "\r\n",
+ ],
+ (() => {
+ const fd = new FormData();
+ fd.append("field", "value");
+ return fd;
+ })(),
+ ],
+
+ empty: [["--WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n", "\r\n"], new FormData()],
+ };
+ for (let name in fixtures) {
+ const [chunks, expected] = fixtures[name];
+ function responseWithStart(start) {
+ return new Response(
+ new ReadableStream({
+ start(controller) {
+ for (let chunk of chunks) {
+ controller.enqueue(chunk);
+ }
+ controller.close();
+ },
+ }),
+ {
+ headers: {
+ "content-type": "multipart/form-data; boundary=WebKitFormBoundary7MA4YWxkTrZu0gW",
+ },
+ },
+ );
+ }
+
+ function responseWithPull(start) {
+ return new Response(
+ new ReadableStream({
+ pull(controller) {
+ for (let chunk of chunks) {
+ controller.enqueue(chunk);
+ }
+ controller.close();
+ },
+ }),
+ {
+ headers: {
+ "content-type": "multipart/form-data; boundary=WebKitFormBoundary7MA4YWxkTrZu0gW",
+ },
+ },
+ );
+ }
+
+ function responseWithPullAsync(start) {
+ return new Response(
+ new ReadableStream({
+ async pull(controller) {
+ for (let chunk of chunks) {
+ await Bun.sleep(0);
+ controller.enqueue(chunk);
+ }
+ controller.close();
+ },
+ }),
+ {
+ headers: {
+ "content-type": "multipart/form-data; boundary=WebKitFormBoundary7MA4YWxkTrZu0gW",
+ },
+ },
+ );
+ }
+
+ test("response.formData()", async () => {
+ expect((await responseWithPull().formData()).toJSON()).toEqual(expected.toJSON());
+ expect((await responseWithStart().formData()).toJSON()).toEqual(expected.toJSON());
+ expect((await responseWithPullAsync().formData()).toJSON()).toEqual(expected.toJSON());
+ });
+
+ test("Bun.readableStreamToFormData", async () => {
+ expect(
+ (
+ await Bun.readableStreamToFormData(await responseWithPull().body, "WebKitFormBoundary7MA4YWxkTrZu0gW")
+ ).toJSON(),
+ ).toEqual(expected.toJSON());
+ });
+
+ test("FormData.from", async () => {
+ expect(FormData.from(await responseWithPull().text(), "WebKitFormBoundary7MA4YWxkTrZu0gW").toJSON()).toEqual(
+ expected.toJSON(),
+ );
+
+ expect(FormData.from(await responseWithPull().blob(), "WebKitFormBoundary7MA4YWxkTrZu0gW").toJSON()).toEqual(
+ expected.toJSON(),
+ );
+
+ expect(
+ FormData.from(
+ await (await responseWithPull().blob()).arrayBuffer(),
+ "WebKitFormBoundary7MA4YWxkTrZu0gW",
+ ).toJSON(),
+ ).toEqual(expected.toJSON());
+ });
+ }
+
+ test("URL-encoded example", async () => {
+ const stream = new Response("hello=123").body;
+ const formData = await Bun.readableStreamToFormData(stream);
+ expect(formData.get("hello")).toBe("123");
+ });
+});
describe("WritableStream", () => {
it("works", async () => {