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
|
import { test, expect } from "bun:test";
import express, { Application, Request, Response } from "express";
import { json } from "body-parser";
// Express uses iconv-lite
test("iconv works", () => {
var iconv = require("iconv-lite");
// Convert from an encoded buffer to a js string.
var str = iconv.decode(
Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]),
"win1251",
);
// Convert from a js string to an encoded buffer.
var buf = iconv.encode("Sample input string", "win1251");
expect(str).toBe("hello");
expect(iconv.decode(buf, "win1251")).toBe("Sample input string");
// Check if encoding is supported
expect(iconv.encodingExists("us-ascii")).toBe(true);
});
// https://github.com/oven-sh/bun/issues/1913
test("httpServer", async (done) => {
// Constants
const PORT = 8412;
// App handlers
const app: Application = express();
const httpServer = require("http").createServer(app);
app.on("error", (err) => {
done(err);
});
app.use(json());
var reached = false;
// This throws a TypeError since it uses body-parser.json
app.post("/ping", (request: Request, response: Response) => {
expect(request.body).toEqual({ hello: "world" });
reached = true;
response.status(200).send("POST - pong");
httpServer.close();
done();
});
httpServer.listen(PORT);
const resp = await fetch(`http://localhost:${PORT}/ping`, {
method: "POST",
body: JSON.stringify({ hello: "world" }),
headers: {
"Content-Type": "application/json",
},
});
expect(resp.status).toBe(200);
expect(await resp.text()).toBe("POST - pong");
expect(reached).toBe(true);
done();
});
|