aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/fetch.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/js/web/fetch/fetch.test.ts')
-rw-r--r--test/js/web/fetch/fetch.test.ts109
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({