-
Notifications
You must be signed in to change notification settings - Fork 17
/
serve_test.ts
144 lines (135 loc) · 3.82 KB
/
serve_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
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { App, get, post } from "./mod.ts";
import { HandlerConfig, Method } from "./handler.ts";
interface RequestInit {
body?: string | FormData;
headers?: Record<string, string>;
method?: string;
}
const testPort = 8376;
const host = `http://localhost:${testPort}`;
interface testCase {
name: string;
registered: HandlerConfig;
path: string;
method: Method;
params?: string | FormData;
headers?: Record<string, string>;
expected: string;
}
const testCases: Array<testCase> = [
{
name: "valid basic handler",
registered: get("/basic", () => "basic"),
path: "basic",
method: Method.GET,
expected: "basic",
},
{
name: "valid basic handler with path parameter 1",
registered: get("/parameters/:param", ({ params }) => params.param),
path: "parameters/p",
method: Method.GET,
expected: "p",
},
{
name: "valid basic handler with path parameter 2",
registered: get("/parameters/:param", ({ params }) => params.param),
path: "parameters/pa",
method: Method.GET,
expected: "pa",
},
{
name: "valid basic handler with path parameter 3",
registered: get("/parameters/:param", ({ params }) => params.param),
path: "parameters/pat",
method: Method.GET,
expected: "pat",
},
{
name: "valid basic handler with path parameters",
registered: get(
"/parameters/:param1/:param2",
({ params }) => `${params.param1} ${params.param2}`,
),
path: "parameters/p1/p2",
method: Method.GET,
expected: "p1 p2",
},
{
name: "valid basic handler with path parameter at start",
registered: get("/:param", ({ params }) => params.param),
path: "basic",
method: Method.GET,
expected: "basic",
},
{
name: "valid async handler",
registered: get("/async", async () => "async"),
path: "async",
method: Method.GET,
expected: "async",
},
{
name: "valid get params",
registered: get("/params", ({ params }) => params.name),
path: "params?name=john",
method: Method.GET,
expected: "john",
},
{
name: "valid post",
registered: post("/params", ({ params }) => [200, params.name]),
path: "params",
params: JSON.stringify({ name: "ben" }),
method: Method.POST,
expected: "ben",
},
{
name: "valid post with detailed content-type",
registered: post("/params", ({ params }) => [200, params!.name]),
path: "params",
params: JSON.stringify({ name: "tom" }),
headers: { "content-type": "application/json; charset=utf-8" },
method: Method.POST,
expected: "tom",
},
// this test doesn't pass because deno's fetch is broken.
// {
// name: 'valid post formdata',
// registered: post('/params', ({ params }) => params.name),
// path: 'params',
// params: (() => {
// const fd = new FormData();
// fd.append('name', 'tom');
// return fd;
// })(),
// method: Method.POST,
// expected: 'tom',
// },
];
for (const tc of testCases) {
Deno.test({
name: tc.name,
async fn() {
const app = new App(testPort);
app.serve();
app.register(tc.registered);
const reqInit: RequestInit = { method: tc.method };
if (typeof tc.params === "string") {
reqInit.body = tc.params;
reqInit.headers = tc.headers || { "content-type": "application/json" };
} else {
reqInit.body = tc.params;
}
const res = await fetch(`${host}/${tc.path}`, reqInit);
const actual = await res.text();
const contentLength = res.headers.get("content-length");
assertEquals(actual, tc.expected);
assertEquals(contentLength, new Blob([tc.expected]).size.toString());
const { path, method } = tc.registered;
app.unregister(path, method);
app.close();
},
});
}