forked from zhmushan/abc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.ts
63 lines (58 loc) · 1.6 KB
/
context_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
import { assertEquals, runIfMain, createMockRequest } from "./dev_deps.ts";
import Context from "./context.ts";
const { test } = Deno;
const options = { app: undefined!, r: createMockRequest() };
test(function StringResponse(): void {
const results = [
`{foo: "bar"}`,
`<h1>Title</h1>`,
`foo`,
`foo=bar`,
`undefined`,
`null`,
`0`,
`true`,
``
];
const c = new Context(options);
for (const r of results) {
c.string(r);
assertEquals(c.response.status, 200);
assertEquals(c.response.body, new TextEncoder().encode(r));
assertEquals(c.response.headers!.get("Content-Type"), "text/plain");
}
});
test(function JSONResponse(): void {
const results = [{ foo: "bar" }, `{foo: "bar"}`, [1, 2], {}, [], `[]`];
const c = new Context(options);
for (const r of results) {
c.json(r);
assertEquals(c.response.status, 200);
assertEquals(
c.response.body,
new TextEncoder().encode(typeof r === "object" ? JSON.stringify(r) : r)
);
assertEquals(c.response.headers!.get("Content-Type"), "application/json");
}
});
test(function HTMLResponse(): void {
const results = [
`{foo: "bar"}`,
`<h1>Title</h1>`,
`foo`,
`foo=bar`,
`undefined`,
`null`,
`0`,
`true`,
``
];
const c = new Context(options);
for (const r of results) {
c.html(r);
assertEquals(c.response.status, 200);
assertEquals(c.response.body, new TextEncoder().encode(r));
assertEquals(c.response.headers!.get("Content-Type"), "text/html");
}
});
runIfMain(import.meta);