-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_test.ts
80 lines (65 loc) · 2.16 KB
/
mod_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
import { assertEquals } from "./deps.ts";
import { ByondClient } from "./mod.ts";
const TEST_HOSTNAME = "localhost";
const TEST_PORT = 5613;
let listener: Deno.Listener;
let connection: Deno.Conn;
async function mockByondServer(
expectedPayload: Uint8Array,
response: Uint8Array
) {
listener = Deno.listen({
hostname: TEST_HOSTNAME,
port: TEST_PORT,
});
connection = await listener.accept();
const payload = new Uint8Array(expectedPayload.length);
await connection.read(payload);
assertEquals(payload, expectedPayload);
await connection.write(response);
connection.close();
listener.close();
}
Deno.test("?ping", async () => {
mockByondServer(
// ?ping
Uint8Array.from([0, 131, 0, 11, 0, 0, 0, 0, 0, 63, 112, 105, 110, 103, 0]),
Uint8Array.from([0, 131, 0, 5, 42, 0, 0, 60, 66])
);
const connector = new ByondClient();
await connector.connect({ hostname: TEST_HOSTNAME, port: TEST_PORT });
const response = await connector.send("?ping");
connector.close();
assertEquals(47, response);
});
Deno.test("ping", async () => {
// ?ping
mockByondServer(
Uint8Array.from([0, 131, 0, 11, 0, 0, 0, 0, 0, 63, 112, 105, 110, 103, 0]),
Uint8Array.from([0, 131, 0, 5, 42, 0, 0, 60, 66])
);
const connector = new ByondClient();
await connector.connect({ hostname: TEST_HOSTNAME, port: TEST_PORT });
const response = await connector.send("ping");
connector.close();
assertEquals(47, response);
});
Deno.test("?status&comms_key=SuperSecretPassword", async () => {
mockByondServer(
// ?ping&comms_key=Super%26Secret%3FPassword
Uint8Array.from([
0, 131, 0, 47, 0, 0, 0, 0, 0, 63, 112, 105, 110, 103, 38, 99, 111, 109,
109, 115, 95, 107, 101, 121, 61, 83, 117, 112, 101, 114, 37, 50, 54, 83,
101, 99, 114, 101, 116, 37, 51, 70, 80, 97, 115, 115, 119, 111, 114, 100,
0,
]),
Uint8Array.from([0, 131, 0, 5, 42, 0, 0, 60, 66])
);
const connector = new ByondClient();
await connector.connect({ hostname: TEST_HOSTNAME, port: TEST_PORT });
const response = await connector.send("?ping", {
comms_key: "Super&Secret?Password",
});
connector.close();
assertEquals(47, response);
});