aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/node-http.test.ts
blob: 24b3f1c85018c5995383f18df02f9a628bccd2f4 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, expect, it } from "bun:test";
import { createServer } from "node:http";

describe("node:http", () => {
  describe("createServer", async () => {
    it("hello world", async () => {
      const server = createServer((req, res) => {
        res.writeHead(200, { "Content-Type": "text/plain" });
        res.end("Hello World");
      });
      server.listen(8123);

      const res = await fetch("http://localhost:8123");
      expect(await res.text()).toBe("Hello World");
      server.close();
    });

    it("request & response body streaming (large)", async () => {
      const bodyBlob = new Blob(["hello world", "hello world".repeat(9000)]);

      const input = await bodyBlob.text();

      const server = createServer((req, res) => {
        res.writeHead(200, { "Content-Type": "text/plain" });
        req.on("data", (chunk) => {
          res.write(chunk);
        });

        req.on("end", () => {
          res.end();
        });
      });
      server.listen(8124);

      const res = await fetch("http://localhost:8124", {
        method: "POST",
        body: bodyBlob,
      });

      const out = await res.text();
      expect(out).toBe(input);
      server.close();
    });

    it("request & response body streaming (small)", async () => {
      const bodyBlob = new Blob(["hello world", "hello world".repeat(4)]);

      const input = await bodyBlob.text();

      const server = createServer((req, res) => {
        res.writeHead(200, { "Content-Type": "text/plain" });
        req.on("data", (chunk) => {
          res.write(chunk);
        });

        req.on("end", () => {
          res.end();
        });
      });
      server.listen(8125);

      const res = await fetch("http://localhost:8125", {
        method: "POST",
        body: bodyBlob,
      });

      const out = await res.text();
      expect(out).toBe(input);
      server.close();
    });
  });
});