From 2fe6a965af394db7228dce56ce29b629c650764c Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Mon, 7 Aug 2023 20:15:53 -0700 Subject: implement fetching data urls (#4000) * fetch data urls * `byteSlice` * deinit slice * allocate `mime_type` string if needed * `content_type_allocated` and uncomment tests * `str_` * createAtom and slice decode result --- test/js/web/fetch/fetch.test.ts | 109 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'test/js/web/fetch/fetch.test.ts') diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts index a1585ba14..a381cb320 100644 --- a/test/js/web/fetch/fetch.test.ts +++ b/test/js/web/fetch/fetch.test.ts @@ -38,6 +38,115 @@ it("new Request(invalid url) throws", () => { expect(() => new Request("!")).toThrow(); }); +describe("fetch data urls", () => { + it("basic", async () => { + var url = + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; + + var res = await fetch(url); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + var blob = await res.blob(); + expect(blob.size).toBe(85); + expect(blob.type).toBe("image/png"); + }); + it("percent encoded", async () => { + var url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D"; + var res = await fetch(url); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + var blob = await res.blob(); + expect(blob.size).toBe(13); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("Hello, World!"); + }); + it("percent encoded (invalid)", async () => { + var url = "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3"; + expect(async () => { + await fetch(url); + }).toThrow("failed to fetch the data URL"); + }); + it("plain text", async () => { + var url = "data:,Hello%2C%20World!"; + var res = await fetch(url); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + var blob = await res.blob(); + expect(blob.size).toBe(13); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("Hello, World!"); + + url = "data:,helloworld!"; + res = await fetch(url); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + blob = await res.blob(); + expect(blob.size).toBe(11); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("helloworld!"); + }); + it("data url (invalid)", async () => { + var url = "data:Hello%2C%20World!"; + expect(async () => { + await fetch(url); + }).toThrow("failed to fetch the data URL"); + }); + it("emoji", async () => { + var url = "data:,😀"; + + var res = await fetch(url); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + var blob = await res.blob(); + expect(blob.size).toBe(4); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("😀"); + }); + it("should work with Request", async () => { + var req = new Request("data:,Hello%2C%20World!"); + var res = await fetch(req); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + var blob = await res.blob(); + expect(blob.size).toBe(13); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("Hello, World!"); + + req = new Request("data:,😀"); + res = await fetch(req); + expect(res.status).toBe(200); + expect(res.statusText).toBe("OK"); + expect(res.ok).toBe(true); + + blob = await res.blob(); + expect(blob.size).toBe(4); + expect(blob.type).toBe("text/plain;charset=utf-8"); + expect(blob.text()).resolves.toBe("😀"); + }); + it("should work with Request (invalid)", async () => { + var req = new Request("data:Hello%2C%20World!"); + expect(async () => { + await fetch(req); + }).toThrow("failed to fetch the data URL"); + req = new Request("data:Hello%345632"); + expect(async () => { + await fetch(req); + }).toThrow("failed to fetch the data URL"); + }); +}); + describe("AbortSignal", () => { beforeEach(() => { startServer({ -- cgit v1.2.3