-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
app_test.ts
64 lines (63 loc) 路 1.92 KB
/
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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { createApp, App } from "./app.ts";
import {
assertEquals,
assertMatch,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { group, makeGet } from "./test_util.ts";
import { Loglevel, setLevel } from "./logger.ts";
import { connectWebSocket } from "./vendor/https/deno.land/std/ws/mod.ts";
setLevel(Loglevel.NONE);
group({
name: "app",
}, ({ setupAll, test }) => {
const app = createApp();
app.handle("/no-response", () => {});
app.handle("/throw", () => {
throw new Error("throw");
});
const get = makeGet(app);
setupAll(() => {
const l = app.listen({ port: 8899 });
return () => l.close();
});
test("should respond if req.respond wasn't called", async () => {
const res = await get("/no-response");
assertEquals(res.status, 404);
});
test("should respond for unknown path", async () => {
const res = await get("/not-found");
assertEquals(res.status, 404);
});
test("should handle global error", async () => {
const res = await get("/throw");
const text = await res.text();
assertEquals(res.status, 500);
assertMatch(text, /Error: throw/);
});
});
group({
name: "app/ws",
}, ({ test, setupAll }) => {
const app = createApp();
app.ws("/ws", async (sock) => {
await sock.send("Hello");
await sock.close(1000);
});
setupAll(() => {
const l = app.listen({ port: 8890 });
return () => l.close();
});
test("should accept ws", async () => {
const sock = await connectWebSocket("ws://127.0.0.1:8890/ws");
const it = sock[Symbol.asyncIterator]();
const { value: msg1 } = await it.next();
assertEquals(msg1, "Hello");
const { value: msg2 } = await it.next();
assertEquals(msg2, { code: 1000, reason: "" });
const { done } = await it.next();
assertEquals(done, true);
assertEquals(sock.isClosed, true);
sock.closeForce();
});
});