aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/fetch.test.js
blob: 76e09bcf82d6de6637d8b5f074b73a31caa67a7c (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
import { it, describe, expect } from "bun:test";
import fs from "fs";

describe("fetch", () => {
  const urls = ["https://example.com", "http://example.com"];
  for (let url of urls) {
    it(url, async () => {
      const response = await fetch(url);
      const text = await response.text();

      if (
        fs.readFileSync(
          import.meta.path.substring(0, import.meta.path.lastIndexOf("/")) +
            "/fetch.js.txt",
          "utf8"
        ) !== text
      ) {
        throw new Error("Expected fetch.js.txt to match snapshot");
      }
    });
  }
});

describe("Response", () => {
  it("clone", async () => {
    var body = new Response("<div>hello</div>", {
      headers: {
        "content-type": "text/html; charset=utf-8",
      },
    });
    var clone = body.clone();
    body.headers.set("content-type", "text/plain");
    expect(clone.headers.get("content-type")).toBe("text/html; charset=utf-8");
    expect(body.headers.get("content-type")).toBe("text/plain");
    expect(await clone.text()).toBe("<div>hello</div>");
  });
});