forked from EranStockdale/BetterWebsockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
82 lines (70 loc) · 2.17 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
78
79
80
81
82
import {
assertEquals,
assertNotEquals,
assertThrowsAsync,
} from "./deps.ts";
import { StandardWebSocketClient, WebSocketServer, WebSocketError } from "./mod.ts";
import { on } from "./deps.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 StandardWebSocketClient(endpoint);
assertEquals(ws.webSocket?.readyState, 0)
const open = on(ws, "open");
const event = await connection.next();
assertNotEquals(event, undefined);
await open.next();
assertEquals(ws.webSocket?.readyState, 1)
await ws.close();
assertEquals(ws.webSocket?.readyState, 2)
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 StandardWebSocketClient(endpoint);
const ws2 = new StandardWebSocketClient(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 ws = new StandardWebSocketClient(endpoint);
const connection = on(wss, "connection");
const open = on(ws, "open");
await assertThrowsAsync(async (): Promise<void> => {
await ws.send("test", { hello: 'world' });
}, WebSocketError, "WebSocket is not open: state 0 (CONNECTING)");
await open.next();
await connection.next();
await ws.close();
await wss.close();
},
},
);