deno.land / x / abc@v1.3.3 / app_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
207
208
209
210
211
212
213
214
215
216
217
218
import type { HandlerFunc } from "./types.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";import { Status } from "./vendor/https/deno.land/std/http/http_status.ts";import { createApplication } from "./test_util.ts";import { NotFoundHandler } from "./util.ts";import { NotFoundException } from "./http_exception.ts";import { HttpMethod } from "./constants.ts";const { readFile, test } = Deno;
const decoder = new TextDecoder();const addr = `http://localhost:8081`;
test("app static", async function (): Promise<void> { const app = createApplication(); app.static("/examples", "./examples/template");
let res = await fetch(`${addr}/examples/main.ts`); assertEquals(res.status, Status.OK); assertEquals( await res.text(), decoder.decode(await readFile("./examples/template/main.ts")), );
res = await fetch(`${addr}/examples/`); assertEquals(res.status, Status.NotFound); assertEquals( await res.text(), JSON.stringify(new NotFoundException().response), ); res = await fetch(`${addr}/examples/index.html`); assertEquals(res.status, Status.OK); assertEquals( await res.text(), decoder.decode(await readFile("./examples/template/index.html")), );
res = await fetch(`${addr}/examples/empty`); assertEquals(res.status, Status.NotFound); assertEquals( await res.text(), JSON.stringify(new NotFoundException().response), ); await app.close();});
test("app file", async function (): Promise<void> { const app = createApplication(); app.file("ci", "./.github/workflows/ci.yml"); app.file("fileempty", "./fileempty");
let res = await fetch(`${addr}/ci`); assertEquals(res.status, Status.OK); assertEquals( await res.text(), decoder.decode(await readFile("./.github/workflows/ci.yml")), );
res = await fetch(`${addr}/fileempty`); assertEquals(res.status, Status.NotFound); assertEquals( await res.text(), JSON.stringify(new NotFoundException().response), ); await app.close();});
test("app middleware", async function (): Promise<void> { const app = createApplication(); let str = ""; app .pre((next) => (c) => { str += "0"; return next(c); } ) .use( (next) => (c) => { str += "1"; return next(c); }, (next) => (c) => { str += "2"; return next(c); }, (next) => (c) => { str += "3"; return next(c); }, ) .get("/middleware", () => str);
const res = await fetch(`${addr}/middleware`); assertEquals(res.status, Status.OK); assertEquals(await res.text(), str); assertEquals(str, "0123"); await app.close();});
test("app middleware error", async function (): Promise<void> { const app = createApplication(); const errMsg = "err"; app.get("/middlewareerror", NotFoundHandler, function (): HandlerFunc { return function (): HandlerFunc { throw new NotFoundException(errMsg); }; });
const res = await fetch(`${addr}/middlewareerror`); assertEquals(res.status, Status.NotFound); assertEquals( await res.text(), JSON.stringify(new NotFoundException(errMsg).response), ); await app.close();});
test("app handler", async function (): Promise<void> { const app = createApplication(); app.get("/ok", (): string => "ok");
const res = await fetch(`${addr}/ok`); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "ok"); await app.close();});
test("app http methods", async function (): Promise<void> { const app = createApplication(); app .delete("/delete", (): string => "delete") .get("/get", (): string => "get") .post("/post", (): string => "post") .put("/put", (): string => "put") .any("/any", (): string => "any") .match(Object.values(HttpMethod), "/match", (): string => "match");
let res = await fetch(`${addr}/delete`, { method: HttpMethod.Delete }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "delete");
res = await fetch(`${addr}/get`, { method: HttpMethod.Get }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "get");
res = await fetch(`${addr}/post`, { method: HttpMethod.Post }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "post");
res = await fetch(`${addr}/put`, { method: HttpMethod.Put }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "put");
for (const i of ["GET", "PUT", "POST", "PATCH", "DELETE"]) { res = await fetch(`${addr}/any`, { method: i }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "any"); res = await fetch(`${addr}/match`, { method: i }); assertEquals(res.status, Status.OK); assertEquals(await res.text(), "match"); } await app.close();});
test("app not found", async function (): Promise<void> { const app = createApplication(); app.get("/not_found_handler", NotFoundHandler);
const res = await fetch(`${addr}/not_found_handler`); assertEquals(res.status, Status.NotFound); assertEquals( await res.text(), JSON.stringify(new NotFoundException().response), ); await app.close();});
test("app query string", async function (): Promise<void> { const app = createApplication(); app.get("/qs", (c) => c.queryParams); const res = await fetch(`${addr}/qs?foo=bar`); assertEquals(res.status, Status.OK); assertEquals(await res.json(), { foo: "bar" }); await app.close();});
test("app use after router", async function (): Promise<void> { const app = createApplication(); let preUname: string | undefined, useUname: string | undefined, handlerUname: string | undefined; app.get("/:uname", (c) => { handlerUname = c.params.uname; }); app.pre((next) => (c) => { preUname = c.params.uname; return next(c); } ); app.use((next) => (c) => { useUname = c.params.uname; return next(c); } );
await fetch(`${addr}/zhmushan`).then((resp) => resp.text()); assertEquals(preUname, undefined); assertEquals(useUname, "zhmushan"); assertEquals(handlerUname, "zhmushan"); await app.close();});
abc

Version Info

Tagged at
2 years ago