aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-21 02:21:51 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-21 02:21:51 -0700
commitfa343fa8adb25a7e307e91a3cd3c2c3f24e0152b (patch)
tree63a7ed4a7d787dd2b61330f5c1e69b2af0737e39 /integration/bunjs-only-snippets
parent1f93de264f55e7a392bd34dbb9bda0b2365d7c88 (diff)
downloadbun-fa343fa8adb25a7e307e91a3cd3c2c3f24e0152b.tar.gz
bun-fa343fa8adb25a7e307e91a3cd3c2c3f24e0152b.tar.zst
bun-fa343fa8adb25a7e307e91a3cd3c2c3f24e0152b.zip
[bun.js] 1/? Implement `Response.file`
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r--integration/bunjs-only-snippets/response.file.test.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js
new file mode 100644
index 000000000..442a527f2
--- /dev/null
+++ b/integration/bunjs-only-snippets/response.file.test.js
@@ -0,0 +1,49 @@
+import fs from "fs";
+import { it, expect } from "bun:test";
+import path from "path";
+it("Response.file", async () => {
+ const file = path.join(import.meta.dir, "fetch.js.txt");
+ expect(await Response.file(file).text()).toBe(fs.readFileSync(file, "utf8"));
+});
+
+it("Response.file as a blob", async () => {
+ const file = path.join(import.meta.url, "../fetch.js.txt");
+ var response = Response.file(file);
+ var blob = await response.blob();
+ expect(blob.size).toBe(0);
+ expect(await blob.text()).toBe(fs.readFileSync(file, "utf8"));
+ expect(blob.size).toBe(1256);
+ expect(await blob.text()).toBe(fs.readFileSync(file, "utf8"));
+
+ const array = new Uint8Array(await blob.arrayBuffer());
+ const text = fs.readFileSync(file, "utf8");
+ for (let i = 0; i < text.length; i++) {
+ expect(array[i]).toBe(text.charCodeAt(i));
+ }
+ expect(blob.size).toBe(1256);
+ blob = null;
+ response = null;
+ Bun.gc(true);
+ await new Promise((resolve) => setTimeout(resolve, 1));
+});
+
+it("Response.file as a blob", async () => {
+ const file = path.join(import.meta.url, "../fetch.js.txt");
+ var response = Response.file(file);
+ var blob = await response.blob();
+
+ expect(blob.size).toBe(0);
+ expect(await blob.text()).toBe(fs.readFileSync(file, "utf8"));
+ expect(blob.size).toBe(1256);
+ expect(await blob.text()).toBe(fs.readFileSync(file, "utf8"));
+ const array = new Uint8Array(await blob.arrayBuffer());
+ const text = fs.readFileSync(file, "utf8");
+ for (let i = 0; i < text.length; i++) {
+ expect(array[i]).toBe(text.charCodeAt(i));
+ }
+ expect(blob.size).toBe(1256);
+ blob = null;
+ response = null;
+ Bun.gc(true);
+ await new Promise((resolve) => setTimeout(resolve, 1));
+});