aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/response.file.test.js
blob: 442a527f24760b8bc456b8e24570e359e25e0e14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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));
});