-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
59 lines (55 loc) · 1.7 KB
/
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
import {
assert,
assertEquals,
} from "https://deno.land/std@0.178.0/testing/asserts.ts";
import { handlers } from "./example.ts";
import { serve_http } from "./mod.ts";
Deno.test("example-request", async () => {
const ac = new AbortController();
const { signal } = ac;
await new Promise<void>((res) => {
serve_http(handlers, {
port: 18080,
signal,
onListen({ hostname, port }) {
console.log("listening", { hostname, port });
res();
},
});
});
const res = await fetch("http://localhost:18080/15487354", {});
assert(res.ok);
const json = await res.json();
assert(json?.url === "http://localhost:18080/15487354");
assert(json?.method === "GET");
ac.abort();
});
Deno.test("example-upgrade", async () => {
const ac = new AbortController();
const { signal } = ac;
await new Promise<void>((res) => {
serve_http(handlers, {
port: 18081,
signal,
onListen({ hostname, port }) {
console.log("listening", { hostname, port });
res();
},
});
});
const wss = new WebSocketStream("ws://localhost:18081/15487354");
const conn = await wss.connection;
const { readable } = conn;
const reader = readable.getReader();
const data: Array<string> = [];
while (true) {
const result = await reader.read();
if (result.done) break;
data.push(result.value as string);
}
const json = JSON.parse(data.join(""));
assertEquals(json?.url, "http://localhost:18081/15487354");
assert(json?.method === "GET");
wss.close();
ac.abort();
});