forked from ryo-ma/deno-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
77 lines (65 loc) · 1.94 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
assertEquals,
assertThrows,
assertNotEquals,
assertThrowsAsync,
} from "https://deno.land/std/testing/asserts.ts";
import { WebSocket, WebSocketServer, WebSocketError } from "./mod.ts";
import { on } from "https://deno.land/std/node/events.ts";
const endpoint = "ws://127.0.0.1:8080";
Deno.test(
{
name: "Connect to the server",
async fn(): Promise<void> {
const wss = new WebSocketServer(8080);
const connection = on(wss, "connection");
const ws = new WebSocket(endpoint);
const open = on(ws, "open");
const event = await connection.next();
assertNotEquals(event, undefined);
await open.next();
await ws.close();
assertEquals(ws.isClosed, true);
await wss.close();
},
},
);
Deno.test(
{
name: "Connect to the server from the two clients",
async fn(): Promise<void> {
const wss = new WebSocketServer(8080);
const connection = on(wss, "connection");
const ws1 = new WebSocket(endpoint);
const ws2 = new WebSocket(endpoint);
const open1 = on(ws1, "open");
const open2 = on(ws2, "open");
let event = await connection.next();
assertNotEquals(event, undefined);
event = await connection.next();
assertNotEquals(event, undefined);
await open1.next();
await ws1.close();
assertEquals(ws1.isClosed, true);
await open2.next();
await ws2.close();
assertEquals(ws2.isClosed, true);
await wss.close();
},
},
);
Deno.test(
{
name: "Fails connection to the server",
async fn(): Promise<void> {
const wss = new WebSocketServer(8080);
const ws1 = new WebSocket(endpoint);
const connection = on(wss, "connection");
await assertThrowsAsync(async (): Promise<void> => {
await ws1.send("message");
}, WebSocketError, "WebSocket is not open: state 0 (CONNECTING)");
await connection.next();
await wss.close();
},
},
);