diff options
author | 2022-11-16 04:42:33 -0800 | |
---|---|---|
committer | 2022-11-16 04:42:33 -0800 | |
commit | bf6b1742330343c6004789f02761426aeafdb47b (patch) | |
tree | 2c0c05672c7ee1d1cd3406d21e4e54aac370ed99 /test/bun.js/node-http.test.ts | |
parent | 5de98f23bb4e69e54ea173039ce9ba5a0839e968 (diff) | |
download | bun-bf6b1742330343c6004789f02761426aeafdb47b.tar.gz bun-bf6b1742330343c6004789f02761426aeafdb47b.tar.zst bun-bf6b1742330343c6004789f02761426aeafdb47b.zip |
Make `node:http`.createServer work better
Diffstat (limited to 'test/bun.js/node-http.test.ts')
-rw-r--r-- | test/bun.js/node-http.test.ts | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/test/bun.js/node-http.test.ts b/test/bun.js/node-http.test.ts new file mode 100644 index 000000000..24b3f1c85 --- /dev/null +++ b/test/bun.js/node-http.test.ts @@ -0,0 +1,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(); + }); + }); +}); |