-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.test.ts
50 lines (43 loc) · 1.2 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
import { createRouter } from "./mod.ts";
import * as t from "https://deno.land/[email protected]/testing/asserts.ts";
function echoParamsJSON(
_req: Request,
params?: Partial<Record<string, string>>,
) {
return Promise.resolve(
new Response(JSON.stringify(params), {
headers: { "Content-Type": "application/json" },
}),
);
}
const baseUrl = "http://localhost";
Deno.test("Matches routes", async () => {
const router = createRouter({
"/user/:id": {
"GET": echoParamsJSON,
"POST": echoParamsJSON,
},
"/ping": () => new Response("Pong"),
});
const user = await router(
new Request(baseUrl + "/user/123", { "method": "GET" }),
);
t.assertEquals(user.status, 200);
t.assertEquals((await user.json()).id, "123");
t.assertEquals(
(await router(new Request(baseUrl + "/user/123", { method: "POST" })))
.status,
200,
);
t.assertEquals((await router(new Request(baseUrl + "/ping"))).status, 200);
});
Deno.test("Return 404 on unknown route", async () => {
const router = createRouter({
"/user/:id": echoParamsJSON,
});
t.assertEquals(
(await router(new Request(baseUrl + "/unknown", { method: "POST" })))
.status,
404,
);
});