-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.ts
145 lines (121 loc) · 3.96 KB
/
test.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import fast, { Context } from "./mod.ts";
import { Router } from "./router.ts";
function makeRequest(path: string, init: RequestInit = {}) {
path = "https://example.com" + path;
return new Request(path, { ...init });
}
const app = fast();
const router = new Router();
Deno.test("404", async () => {
const req = makeRequest("/404");
const res = await app.handle(req);
assertEquals(res.status, 404);
});
// deno-fmt-ignore-line
const methods = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'] as const;
for (const method of methods) {
const id = crypto.randomUUID();
app[method]("/", () => id);
Deno.test(`app.${method}`, async () => {
const req = makeRequest("/", { method });
const res = await app.handle(req);
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, id);
});
}
const ping = crypto.randomUUID();
app.get("/ping", () => ping);
Deno.test("app.serve", async () => {
const ac = new AbortController();
app.serve({ signal: ac.signal, onListen: () => {} });
const res = await fetch("http://localhost:8000/ping");
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, ping);
ac.abort();
});
const err = {
status: 400,
code: "",
message: "",
};
app.get("/assert", (ctx: Context) => ctx.assert(false, err));
app.get("/assert2", (ctx: Context) => ctx.assert(true, err));
app.get("/throw", (ctx: Context) => ctx.throw(err));
// deno-fmt-ignore
app.get("/throw2", () => { throw new Error() });
Deno.test("ctx.assert", async () => {
const req = makeRequest("/assert");
const res = await app.handle(req);
assertEquals(res.status, 400);
const req2 = makeRequest("/assert2");
const res2 = await app.handle(req2);
assertEquals(res2.status, 204);
});
Deno.test("ctx.throw", async () => {
const req = makeRequest("/throw");
const res = await app.handle(req);
assertEquals(res.status, 400);
const req2 = makeRequest("/throw2");
const res2 = await app.handle(req2);
const data = await res2.json();
assertEquals(res2.status, 500);
assertEquals(data.error.message, "An unknown error occurred.");
});
app.get("/pages/:id", (ctx) => ctx.params.id);
Deno.test("ctx.params", async () => {
const req = makeRequest("/pages/abc");
const res = await app.handle(req);
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, "abc");
});
app.post("/body", async (ctx) => {
const body = await ctx.body;
return body;
});
Deno.test("ctx.body", async () => {
const body = "abc";
const req = makeRequest("/body", { body, method: "POST" });
const res = await app.handle(req);
assertEquals(res.status, 400);
});
app.get("/string", () => "abc");
app.get("/array", () => [1, 2, 3]);
app.get("/json", () => ({ abc: 123 }));
app.get("/void", () => {});
app.get("/res", () => new Response("Hello", { status: 201 }));
Deno.test("decode", async () => {
const req = makeRequest("/string");
const res = await app.handle(req);
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, "abc");
const req2 = makeRequest("/array");
const res2 = await app.handle(req2);
const data2 = await res2.json();
assertEquals(res2.status, 200);
assertEquals(data2, [1, 2, 3]);
const req3 = makeRequest("/json");
const res3 = await app.handle(req3);
const data3 = await res3.json();
assertEquals(res3.status, 200);
assertEquals(data3, { abc: 123 });
const req4 = makeRequest("/void");
const res4 = await app.handle(req4);
assertEquals(res4.status, 204);
const req5 = makeRequest("/res");
const res5 = await app.handle(req5);
assertEquals(res5.status, 201);
});
router.get("/", () => "router home");
app.use("/api", router);
Deno.test("router", async () => {
const req = makeRequest("/api/");
const res = await app.handle(req);
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, "router home");
});