-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.spec.ts
More file actions
73 lines (64 loc) · 1.85 KB
/
server.spec.ts
File metadata and controls
73 lines (64 loc) · 1.85 KB
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
73
import { beforeAll, expect, test } from "bun:test";
import { GET, PUT } from "./fetcher";
const MOCK_TOKEN = "I_AM_abc101";
beforeAll(() => {
const DATABASE_URL = process.env.DATABASE_URL;
if (DATABASE_URL !== "postgres://user:password@localhost:5432/database") {
console.log(
"SAFETY GUARD: test must be run via `make test`, otherwise it might break dev/prod database",
);
throw new Error(`got: \`${DATABASE_URL}\``);
}
});
test("server up", async () => {
const res = await GET("/");
const text = await res.text();
expect(text).toBe("Hello from Hono 🔥");
});
test("/users/exists", async () => {
let res = await GET("/users/exists/abc101");
expect(res.status).toBe(200);
res = await GET("/users/exists/not-there");
expect(res.status).toBe(404);
});
test("basic auth", async () => {
let res = await GET("/users/me");
expect(res.status).toBe(401);
res = await GET("/users/me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(200);
const json = await res.json();
expect(json.name).toBe("田中太郎");
});
test("send request", async () => {
// should error in auth
let res = await GET("/users/pending/from-me");
expect(res.status).toBe(401);
res = await PUT("/requests/send/102");
expect(res.status).toBe(401);
res = await GET("/users/pending/from-me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(200);
expect(await res.json()).toSatisfy((s) => s.length === 0);
// starting actual request
res = await PUT("/requests/send/102", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(201);
res = await GET("/users/pending/from-me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(await res.json()).toSatisfy(
(s) => s.length === 1 && s[0].name === "山田花子",
);
});