-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
application_test.ts
206 lines (183 loc) · 5.33 KB
/
application_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { assertEquals } from "./deps.ts";
import application, {
ConnInfo,
getParam,
getParams,
Next,
router,
} from "./mod.ts";
Deno.test("app", async (t) => {
const host = "http://localhost:8000";
const app = application();
// routing
app.get("/", () => new Response("GET"));
app.post("/", () => new Response("POST"));
app.put("/", () => new Response("PUT"));
app.delete("/", () => new Response("DELETE"));
app.patch("/", () => new Response("PATCH"));
app.options("/", () => new Response("OPTIONS"));
app.head("/", () => new Response("HEAD"));
app.get(/v/, () => new Response("regex"));
app.get("/user/:id", (req: Request) => {
const id = getParam("id", req);
return new Response(id);
});
app.get("/account/:name", (req: Request) => {
const params = getParams(req);
return new Response(JSON.stringify(params));
});
// app middleware
app.use("/m1", (_req: Request, _conn: ConnInfo, next: Next) => next());
app.get("/m1", () => new Response("m1"));
app.use("/m11", [(_req: Request, _conn: ConnInfo, next: Next) => next()]);
app.get("/m11", () => new Response("m11"));
// app middleware not working
app.use("/m2", (_req: Request, _conn: ConnInfo, _next: Next) => {});
// router middleware
const r = router();
r.get("/", () => new Response("r1"));
app.use("/r1", r);
app.use("/r2", [(_req: Request, _conn: ConnInfo, next: Next) => next()], r);
app.get(
"/level",
(_req: Request, _conn: ConnInfo, next: Next) => next(),
() => new Response("route level1"),
);
app.get(
"/level2",
[(_req: Request, _conn: ConnInfo, next: Next) => next()],
() => new Response("route level2"),
);
const promise = app.serve();
await t.step({
name: "GET",
fn: async () => {
const response = await fetch(host, { method: "GET" });
assertEquals(await response.text(), "GET");
},
sanitizeResources: false,
sanitizeOps: false,
});
await t.step({
name: "PUT",
fn: async () => {
const response = await fetch(host, { method: "PUT" });
assertEquals(await response.text(), "PUT");
},
});
await t.step({
name: "DELETE",
fn: async () => {
const response = await fetch(host, { method: "DELETE" });
assertEquals(await response.text(), "DELETE");
},
});
await t.step({
name: "PATCH",
fn: async () => {
const response = await fetch(host, { method: "PATCH" });
assertEquals(await response.text(), "PATCH");
},
});
await t.step({
name: "OPTIONS",
fn: async () => {
const response = await fetch(host, { method: "OPTIONS" });
assertEquals(await response.text(), "OPTIONS");
},
});
await t.step({
name: "POST",
fn: async () => {
const response = await fetch(host, { method: "POST" });
assertEquals(await response.text(), "POST");
},
});
await t.step({
name: "HEAD",
fn: async () => {
const response = await fetch(host, { method: "HEAD" });
// QUESTION: WHY EMPTY?
assertEquals(await response.text(), "");
},
});
await t.step({
name: "Route with regex path",
fn: async () => {
const response = await fetch(`${host}/v`, { method: "GET" });
assertEquals(await response.text(), "regex");
},
});
await t.step({
name: "app level middleware",
fn: async () => {
const response = await fetch(`${host}/m1`, { method: "GET" });
assertEquals(await response.text(), "m1");
},
});
await t.step({
name: "app level middleware with array",
fn: async () => {
const response = await fetch(`${host}/m11`, { method: "GET" });
assertEquals(await response.text(), "m11");
},
});
await t.step({
name: "app level middleware not finished",
fn: async () => {
const response = await fetch(`${host}/m2`, { method: "GET" });
assertEquals(await response.text(), "Internal Server Error");
},
});
await t.step({
name: "router middleware",
fn: async () => {
const response = await fetch(`${host}/r1`, { method: "GET" });
assertEquals(await response.text(), "r1");
},
});
await t.step({
name: "router middleware with array",
fn: async () => {
const response = await fetch(`${host}/r2`, { method: "GET" });
assertEquals(await response.text(), "r1");
},
});
await t.step({
name: "route level middleware",
fn: async () => {
const response = await fetch(`${host}/level`, { method: "GET" });
assertEquals(await response.text(), "route level1");
},
});
await t.step({
name: "route level middleware with array",
fn: async () => {
const response = await fetch(`${host}/level2`, { method: "GET" });
assertEquals(await response.text(), "route level2");
},
});
await t.step({
name: "Route get param",
fn: async () => {
const response = await fetch(`${host}/user/5`, { method: "GET" });
assertEquals(await response.text(), "5");
},
});
await t.step({
name: "Route get params",
fn: async () => {
const response = await fetch(`${host}/account/agus`, { method: "GET" });
assertEquals(await response.text(), JSON.stringify({ name: "agus" }));
},
});
await t.step({
name: "Server Close",
fn: async () => {
app.close();
await promise;
},
sanitizeResources: false,
sanitizeOps: false,
});
});