diff options
author | 2023-08-07 20:15:53 -0700 | |
---|---|---|
committer | 2023-08-07 20:15:53 -0700 | |
commit | 2fe6a965af394db7228dce56ce29b629c650764c (patch) | |
tree | 9d0df164cd0ffcc2c1000a58aa498476abf0b3c6 /test/js/web/fetch/fetch.test.ts | |
parent | a32097aa9f53f7c8ea1331c61dc2658bc1b11208 (diff) | |
download | bun-2fe6a965af394db7228dce56ce29b629c650764c.tar.gz bun-2fe6a965af394db7228dce56ce29b629c650764c.tar.zst bun-2fe6a965af394db7228dce56ce29b629c650764c.zip |
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
Diffstat (limited to 'test/js/web/fetch/fetch.test.ts')
-rw-r--r-- | test/js/web/fetch/fetch.test.ts | 109 |
1 files changed, 109 insertions, 0 deletions
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({ |