-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·236 lines (175 loc) · 6.76 KB
/
test.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
"use strict";
// assert
const assert = require("assert");
// setup
const ws = require("stt-websocket");
function createServer(self) {
self.server = ws.createServer(self, undefined);
}
function listen(self, port, cb) {
self.server.listen(port, cb);
}
function connect(self, port, cb) {
ws.connect("ws://127.0.0.1:"+port+(self.path||"")+(self.search||""),
self, (cl, err)=>{self.client=cl; self.error=err; cb();});
}
function clientSetup(self, port, cb) {
createServer(self);
listen(self, port, ()=>connect(self, port, ()=>cb(self.client)));
}
function serverSetup(self, port, cb) {
createServer(self);
listen(self, port, ()=>connect(self, port, ()=>cb(self.serverClient)));
self.server.on("connect", cl=>{self.serverClient=cl;});
}
function cleanup(self, cb) {
self.server.close();
self.server.disconnect();
cb();
}
var ports = 2300;
// tests
const tests = {
"server basic setup": function (cb) {
const self = {}; createServer(self); const port = ++ports;
assert(self.server.constructor === ws.Server, "server is not a WebSocketServer");
assert(self.server.http instanceof require("http").Server, "httpServer is not a HttpServer");
cleanup(self, cb);
},
"server listening in time": function (cb) {
const self = {}; createServer(self); const port = ++ports;
var gotback = false;
function finish() {if (!gotback) {gotback = true; cleanup(self, cb);}}
listen(self, port, finish);
assert(self.server.http.listening, "server is not listening");
var tm = setTimeout(function () {
assert(gotback, "listening callback takes too long");
finish();
}, 100);
},
"client connects to server": function (cb) {
const self = {}; createServer(self); const port = ++ports;
listen(self, port, function () {
connect(self, port, ()=>{});
setTimeout(function () {
assert(!self.error, "connect error occured, or timeout");
assert(self.client instanceof ws.Socket, "client is not a WebSocket, or timeout");
cleanup(self, cb);
}, 100);
});
},
"server receives connection": function (cb) {
const self = {}; const port = ++ports;
createServer(self);
self.server.on("connect", cl=>{self.serverClient=cl;});
listen(self, port, function () {
connect({}, port, ()=>{});
setTimeout(function () {
assert(self.serverClient instanceof ws.Socket, "serverClient is not a WebSocket, or timeout");
cleanup(self, cb);
}, 100);
});
},
"server accept, path, protocol, client headers, auth": function (cb) {
const port = ++ports;
var auth;
const self = {
headers: {"User": "testUser"},
auth: "testUser:testPassword",
path: "/testting",
search: "?reallyTesting",
protocol: ["proto1", "proto2", "proto3"],
accept: (request, protocol)=>(
protocol === "proto1" && request.url.search === self.search &&
request.headers.user === "testUser" &&
(auth = request.headers.authorization) &&
auth.slice(6) === Buffer.from("testUser:testPassword").toString("base64")
)
};
serverSetup(self, port, function (serverClient) {
assert(serverClient instanceof ws.Socket, "server didn't accept");
assert(serverClient.protocol === "proto1", "server didn't choose proto1");
// console.log(serverClient.request.headers);
cleanup(self, cb);
});
},
"client ping -> pong": function (cb) {
const self = {}; const port = ++ports;
clientSetup(self, port, function (client) {
var ping = false;
client.on("pong", ()=>{ping=true});
client.ping("");
setTimeout(function () {
assert(ping, "no ping");
cleanup(self, cb);
}, 1000);
});
},
"server ping -> pong": function (cb) {
const self = {}; const port = ++ports;
serverSetup(self, port, function (serverClient) {
var ping = false;
serverClient.on("pong", ()=>{ping=true});
serverClient.ping("");
setTimeout(function () {
assert(ping, "no ping");
cleanup(self, cb);
}, 1000);
});
},
"client echo text and binary": function (cb) {
const self = {}; const port = ++ports;
clientSetup(self, port, function (client) {
self.server.on("message", (msg,cl)=>cl.send(msg));
var msgText = "";
var msgBinary = "";
client.on("message", (msg)=>{
if (typeof msg === "string") msgText = msg;
else msgBinary = msg.toString();
});
client.send("hallo");
client.send(Buffer.from("halloBuffer"));
setTimeout(function () {
assert(msgText === "hallo", "not echoing text");
assert(msgBinary === "halloBuffer", "not echoing binary");
cleanup(self, cb);
}, 1000);
});
},
"server echo text and binary": function (cb) {
const self = {}; const port = ++ports;
serverSetup(self, port, function (serverClient) {
self.client.on("message", (msg)=>self.client.send(msg));
var msgText = "";
var msgBinary = "";
serverClient.on("message", (msg)=>{
if (typeof msg === "string") msgText = msg;
else msgBinary = msg.toString();
});
serverClient.send("hallo");
serverClient.send(Buffer.from("halloBuffer"));
setTimeout(function () {
assert(msgText === "hallo", "not echoing text");
assert(msgBinary === "halloBuffer", "not echoing binary");
cleanup(self, cb);
}, 1000);
});
},
"server sent to big a message": function (cb) {
const self = {}; const port = ++ports;
serverSetup(self, port, function (serverClient) {
serverClient.on("close", function (code, msg) {
self.close = {code, msg};
});
serverClient.send(Buffer.allocUnsafe(129*1024*1024));
setTimeout(function () {
assert(self.close.code === 1009, "a message too large got through or didn't arrive yet");
cleanup(self, cb);
}, 1000);
});
}
}
Object.entries(tests).forEach(function ([name, test], index) {
test(()=>console.log("done", (index+1)+":", name));
});