-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
app_test.ts
71 lines (70 loc) · 1.9 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
65
66
67
68
69
70
71
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import { App, createApp } 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";
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",
sanitizeResources: false,
}, ({ 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({
name: "should accept ws",
fn: async () => {
const sock = new WebSocket("ws://127.0.0.1:8890/ws");
const p1 = new Promise((resolve) => {
sock.onmessage = (msg) => {
resolve(msg.data);
};
});
const p2 = new Promise<void>((resolve) => {
sock.onclose = () => {
resolve();
};
});
const [msg] = await Promise.all([p1, p2]);
assertEquals(msg, "Hello");
sock.close();
},
});
});