diff options
Diffstat (limited to 'test/js/web/fetch')
-rw-r--r-- | test/js/web/fetch/blob.test.ts | 17 | ||||
-rw-r--r-- | test/js/web/fetch/body.test.ts | 23 | ||||
-rw-r--r-- | test/js/web/fetch/fetch.test.ts | 187 | ||||
-rw-r--r-- | test/js/web/fetch/fixtures/file.txt | 1 |
4 files changed, 189 insertions, 39 deletions
diff --git a/test/js/web/fetch/blob.test.ts b/test/js/web/fetch/blob.test.ts index 51b9c0ea8..ba44f8c1b 100644 --- a/test/js/web/fetch/blob.test.ts +++ b/test/js/web/fetch/blob.test.ts @@ -28,23 +28,6 @@ test("Blob.slice", () => { expect(blob.slice("text/plain;charset=utf-8").type).toBe("text/plain;charset=utf-8"); }); -test("Blob.prototype.type setter", () => { - var blob = new Blob(["Bun", "Foo"], { type: "text/foo" }); - expect(blob.type).toBe("text/foo"); - blob.type = "text/bar"; - expect(blob.type).toBe("text/bar"); - blob.type = "text/baz"; - expect(blob.type).toBe("text/baz"); - blob.type = "text/baz; charset=utf-8"; - expect(blob.type).toBe("text/baz; charset=utf-8"); - // @ts-expect-error - blob.type = NaN; - expect(blob.type).toBe(""); - // @ts-expect-error - blob.type = Symbol(); - expect(blob.type).toBe(""); -}); - test("new Blob", () => { var blob = new Blob(["Bun", "Foo"], { type: "text/foo" }); expect(blob.size).toBe(6); diff --git a/test/js/web/fetch/body.test.ts b/test/js/web/fetch/body.test.ts index 76125ad00..40a70c5b3 100644 --- a/test/js/web/fetch/body.test.ts +++ b/test/js/web/fetch/body.test.ts @@ -207,6 +207,7 @@ for (const { body, fn } of bodyTypes) { return readable; }, content: /Example Domain/, + skip: true, // fails, text is empty }, { label: "Bun.spawn() stream", @@ -242,16 +243,16 @@ for (const { body, fn } of bodyTypes) { }); } }); - for (const { body, fn: fn0 } of bodyTypes) { - describe(body.name, () => { - for (const { string, buffer } of utf8) { - // @ts-expect-error - expect(() => fn(fn0(buffer))).not.toThrow(); - // @ts-expect-error - expect(async () => await fn(fn0(buffer)).text()).toBe(string); - } - }); - } + test(body.name, async () => { + for (const { string, buffer } of utf8) { + // @ts-expect-error + expect(() => { + fn(buffer); + }).not.toThrow(); + // @ts-expect-error + expect(await fn(buffer).text()).toBe(string); + } + }); }); for (const { string, buffer } of utf8) { describe("arrayBuffer()", () => { @@ -574,7 +575,7 @@ for (const { body, fn } of bodyTypes) { ], "blob": [ { - body: null, + body: undefined, bodyUsed: false, }, { diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts index 1185dbd55..065506fad 100644 --- a/test/js/web/fetch/fetch.test.ts +++ b/test/js/web/fetch/fetch.test.ts @@ -512,14 +512,7 @@ function testBlobInterface(blobbyConstructor, hasBlobFn?) { if (withGC) gc(); expect(blobed.size).toBe(size); if (withGC) gc(); - blobed.type = ""; - if (withGC) gc(); - expect(blobed.type).toBe(""); - if (withGC) gc(); - blobed.type = "application/json"; - if (withGC) gc(); - expect(blobed.type).toBe("application/json"); - if (withGC) gc(); + expect(blobed.type).toBe("text/plain;charset=utf-8"); const out = await blobed.text(); expect(out).toBe(text); if (withGC) gc(); @@ -599,6 +592,36 @@ describe("Bun.file", () => { describe("Blob", () => { testBlobInterface(data => new Blob([data])); + it("should have expected content type", async () => { + var response = new Response("<div>hello</div>", { + headers: { + "content-type": "multipart/form-data;boundary=boundary", + }, + }); + expect((await response.blob()).type).toBe("multipart/form-data;boundary=boundary"); + + response = new Response("<div>hello</div>", { + headers: { + "content-type": "text/html; charset=utf-8", + }, + }); + expect((await response.blob()).type).toBe("text/html;charset=utf-8"); + + response = new Response("<div>hello</div>", { + headers: { + "content-type": "octet/stream", + }, + }); + expect((await response.blob()).type).toBe("octet/stream"); + + response = new Response("<div>hello</div>", { + headers: { + "content-type": "text/plain;charset=utf-8", + }, + }); + expect((await response.blob()).type).toBe("text/plain;charset=utf-8"); + }); + var blobConstructorValues = [ ["123", "456"], ["123", 456], @@ -726,13 +749,13 @@ describe("Response", () => { }); it("sets the content-type header", () => { let response = Response.json("hello"); - expect(response.type).toBe("basic"); + expect(response.type).toBe("default"); expect(response.headers.get("content-type")).toBe("application/json;charset=utf-8"); expect(response.status).toBe(200); }); it("supports number status code", () => { let response = Response.json("hello", 407); - expect(response.type).toBe("basic"); + expect(response.type).toBe("default"); expect(response.headers.get("content-type")).toBe("application/json;charset=utf-8"); expect(response.status).toBe(407); }); @@ -777,7 +800,7 @@ describe("Response", () => { expect(response.headers.get("x-hello")).toBe("world"); expect(response.headers.get("Location")).toBe("https://example.com"); expect(response.status).toBe(302); - expect(response.type).toBe("basic"); + expect(response.type).toBe("default"); expect(response.ok).toBe(false); }); }); @@ -821,7 +844,149 @@ describe("Response", () => { expect(exception instanceof SyntaxError).toBe(true); } }); + describe("should consume body correctly", async () => { + it("with text first", async () => { + var response = new Response("<div>hello</div>"); + expect(await response.text()).toBe("<div>hello</div>"); + expect(async () => { + await response.text(); + }).toThrow("Body already used"); + expect(async () => { + await response.json(); + }).toThrow("Body already used"); + expect(async () => { + await response.formData(); + }).toThrow("Body already used"); + expect(async () => { + await response.blob(); + }).toThrow("Body already used"); + expect(async () => { + await response.arrayBuffer(); + }).toThrow("Body already used"); + }); + it("with json first", async () => { + var response = new Response('{ "hello": "world" }'); + expect(await response.json()).toEqual({ "hello": "world" }); + expect(async () => { + await response.json(); + }).toThrow("Body already used"); + expect(async () => { + await response.text(); + }).toThrow("Body already used"); + expect(async () => { + await response.formData(); + }).toThrow("Body already used"); + expect(async () => { + await response.blob(); + }).toThrow("Body already used"); + expect(async () => { + await response.arrayBuffer(); + }).toThrow("Body already used"); + }); + it("with formData first", async () => { + var response = new Response("--boundary--", { + headers: { + "content-type": "multipart/form-data;boundary=boundary", + }, + }); + expect(await response.formData()).toBeInstanceOf(FormData); + expect(async () => { + await response.formData(); + }).toThrow("Body already used"); + expect(async () => { + await response.text(); + }).toThrow("Body already used"); + expect(async () => { + await response.json(); + }).toThrow("Body already used"); + expect(async () => { + await response.blob(); + }).toThrow("Body already used"); + expect(async () => { + await response.arrayBuffer(); + }).toThrow("Body already used"); + }); + it("with blob first", async () => { + var response = new Response("<div>hello</div>"); + expect(response.body instanceof ReadableStream).toBe(true); + expect(response.headers instanceof Headers).toBe(true); + expect(response.type).toBe("default"); + var blob = await response.blob(); + expect(blob).toBeInstanceOf(Blob); + expect(blob.stream()).toBeInstanceOf(ReadableStream); + expect(async () => { + await response.blob(); + }).toThrow("Body already used"); + expect(async () => { + await response.text(); + }).toThrow("Body already used"); + expect(async () => { + await response.json(); + }).toThrow("Body already used"); + expect(async () => { + await response.formData(); + }).toThrow("Body already used"); + expect(async () => { + await response.arrayBuffer(); + }).toThrow("Body already used"); + }); + it("with arrayBuffer first", async () => { + var response = new Response("<div>hello</div>"); + expect(await response.arrayBuffer()).toBeInstanceOf(ArrayBuffer); + expect(async () => { + await response.arrayBuffer(); + }).toThrow("Body already used"); + expect(async () => { + await response.text(); + }).toThrow("Body already used"); + expect(async () => { + await response.json(); + }).toThrow("Body already used"); + expect(async () => { + await response.formData(); + }).toThrow("Body already used"); + expect(async () => { + await response.blob(); + }).toThrow("Body already used"); + }); + it("with Bun.file() streams", async () => { + var stream = Bun.file(import.meta.dir + "/fixtures/file.txt").stream(); + expect(stream instanceof ReadableStream).toBe(true); + var input = new Response((await new Response(stream).blob()).stream()).arrayBuffer(); + var output = Bun.file(import.meta.dir + "/fixtures/file.txt").arrayBuffer(); + expect(await input).toEqual(await output); + }); + it("with Bun.file() with request/response", async () => { + startServer({ + async fetch(request: Request) { + var text = await request.text(); + expect(async () => { + await request.arrayBuffer(); + }).toThrow(); + return (response = new Response((await new Response(text).blob()).stream())); + }, + }); + + var response = await fetch(`http://127.0.0.1:${server.port}`, { + method: "POST", + body: await Bun.file(import.meta.dir + "/fixtures/file.txt").arrayBuffer(), + }); + var input = await response.arrayBuffer(); + var output = await Bun.file(import.meta.dir + "/fixtures/file.txt").stream(); + expect(input).toEqual((await output.getReader().read()).value?.buffer); + }); + }); + it("should work with bigint", () => { + var r = new Response("hello status", { status: 200n }); + expect(r.status).toBe(200); + r = new Response("hello status", { status: 599n }); + expect(r.status).toBe(599); + r = new Response("hello status", { status: BigInt(200) }); + expect(r.status).toBe(200); + r = new Response("hello status", { status: BigInt(599) }); + expect(r.status).toBe(599); + }); testBlobInterface(data => new Response(data), true); }); diff --git a/test/js/web/fetch/fixtures/file.txt b/test/js/web/fetch/fixtures/file.txt new file mode 100644 index 000000000..21e64d50b --- /dev/null +++ b/test/js/web/fetch/fixtures/file.txt @@ -0,0 +1 @@ +this is a file
\ No newline at end of file |