-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
router_test.ts
106 lines (105 loc) 路 3.54 KB
/
router_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
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { createRouter } from "./router.ts";
import { runIfMain } from "./vendor/https/deno.land/std/testing/mod.ts";
import {
assertEquals,
assertMatch
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { it } from "./test_util.ts";
import { Loglevel, setLevel } from "./logger.ts";
setLevel(Loglevel.NONE);
it("router", t => {
let errorHandled = false;
t.beforeAfterAll(() => {
const router = createRouter();
router.handle("/index", async req => {
await req.respond({
status: 200,
body: "ok"
});
});
router.handle("/Index", async req => {
await req.respond({ status: 200, body: "ok" });
});
router.handle(new RegExp("^/Var"), async req => {
await req.respond({ status: 200, body: req.url });
});
router.handle(new RegExp("/foo/(?<id>.+)"), async req => {
const { id } = req.match.groups;
await req.respond({
status: 200,
headers: new Headers({
"content-type": "application/json"
}),
body: JSON.stringify({ id })
});
});
router.handle("/no-response", async req => {});
router.handle("/throw", async req => {
throw new Error("throw");
});
router.handle("/redirect", req => req.redirect("/index"));
router.handleError((e, req) => {
errorHandled = true;
});
const listener = router.listen({
hostname: "127.0.0.1",
port: 8898
});
return () => listener.close();
});
t.run("should respond string path", async () => {
{
const res1 = await fetch("http://127.0.0.1:8898/index");
const text = await res1.body.text();
assertEquals(res1.status, 200);
assertEquals(text, "ok");
}
});
t.run("should respond with capitalized path", async () => {
const res1 = await fetch("http://127.0.0.1:8898/Index");
const text = await res1.body.text();
assertEquals(res1.status, 200);
assertEquals(text, "ok");
});
t.run("should respond with capitalized path in regex", async () => {
for (const p of ["var", "Var"]) {
const res1 = await fetch("http://127.0.0.1:8898/" + p);
const text = await res1.body.text();
assertEquals(res1.status, 200);
assertEquals(text, text);
}
});
t.run("should respond regexp path", async () => {
const res2 = await fetch("http://127.0.0.1:8898/foo/123");
const json = await res2.body.json();
assertEquals(res2.status, 200);
assertEquals(res2.headers.get("content-type"), "application/json");
assertEquals(json["id"], "123");
});
t.run("should respond even if req.respond wasn't called", async () => {
const res = await fetch("http://127.0.0.1:8898/no-response");
// assertEquals(res.status, 404);
const text = await res.body.text();
assertEquals(text, "Not Found");
});
t.run("should respond for unknown path", async () => {
const res = await fetch("http://127.0.0.1:8898/not-found");
const text = await res.body.text();
assertEquals(res.status, 404);
assertEquals(text, "Not Found");
});
t.run("should handle global error", async () => {
const res = await fetch("http://127.0.0.1:8898/throw");
const text = await res.body.text();
assertEquals(res.status, 500);
assertMatch(text, /Error: throw/);
assertEquals(errorHandled, true);
});
t.run("should redirect", async () => {
const res = await fetch("http://127.0.0.1:8898/redirect");
assertEquals(res.status, 200);
assertEquals(await res.body.text(), "ok");
});
});
runIfMain(import.meta);