-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
409 lines (347 loc) · 10.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
'use strict';
require('should');
var assert = require('chai').assert;
var Promise = require('bluebird');
Promise.longStackTraces();
var WebSocket = require('./ews');
let RequestHandler = require('./ews').RequestHandler;
let EventEmitter = require('events').EventEmitter;
describe('ews module', function() {
var ws, wss, wsServer, cancelRequestCheck;
beforeEach(function(cb) {
ws = new WebSocket('ws://localhost:23200', {
protocolVersion: 8,
origin: 'http://localhost'
});
wss = new WebSocket.Server({ port: 23200 });
wss.on('connection', function(ws) {
wsServer = ws;
});
ws.on('open', cb);
cancelRequestCheck = false;
});
afterEach(function() {
if(wss) {
wss.close();
}
if(!cancelRequestCheck) {
assert.deepEqual(ws.requestMap, {});
assert.deepEqual(wsServer.requestMap, {});
}
});
it('should work with casual web socket usage', endTest => {
ws.send('test');
ws.on('message', function(msg) {
msg.should.equal('test-reply');
endTest();
});
wsServer.on('message', function(msg) {
wsServer.send('test-reply');
});
});
it('should send JSON requests through basic API', endTest => {
ws.sendRequest('test1', {code: 42}, function(err, res) {
res.should.equal('foo');
ws.sendRequest('test2', {code: 43}).then(function(res) {
res.should.equal('foo2');
endTest();
});
});
wsServer.on('request:test1', function(data, responseCb) {
data.code.should.equal(42);
responseCb(null, 'foo');
});
wsServer.on('request:test2', function(data, responseCb) {
data.code.should.equal(43);
responseCb(null, 'foo2');
});
});
it('should send JSON requests through promise request handlers',
endTest => {
ws.sendRequest('test1', {code: 42}, function(err, res) {
res.should.equal('foo');
ws.sendRequest('test2', {code: 43}).then(function(res) {
res.should.equal('foo2');
endTest();
});
});
wsServer.onRequest('test1', function(data) {
return 'foo';
});
wsServer.onRequest('test2', function(data) {
return (new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo2');
}, 10);
}));
});
});
it('should send JSON requests through promise request handlers with nested promises',
endTest => {
ws.sendRequest('test1', {code: Promise.resolve(42)}, function(err, res) {
assert.deepEqual(res, {
foo: ['bar', 'foo']
})
endTest();
});
wsServer.onRequest('test1', function(data) {
assert.deepEqual(data, {code: 42});
return Promise.resolve({
foo: [Promise.resolve('bar'), 'foo']
});
});
});
it('should send an error if request is not handled', endTest => {
ws.sendRequest('test1', {code: 42}).catch(err => {
if(err.message.match(/No handler for request: test1/)) {
endTest();
} else {
throw err;
}
});
});
it('should send exceptions through promise handlers', endTest => {
ws.sendRequest('test', {code: 42}, function(err, res) {
err.should.equal("foo");
endTest();
});
wsServer.onRequest('test', function(data) {
throw "foo";
});
});
it('should send real exceptions through promise handlers', endTest => {
ws.sendRequest('test', {code: 42}, function(err, res) {
err.name.should.equal('Remote::Error');
err.message.should.equal("foo");
endTest();
});
wsServer.onRequest('test', function(data) {
throw new Error('foo');
});
});
it('should send real exceptions through promise handlers with custom exception hook', endTest => {
wsServer.setRemoteErrorHook(err => {
err.name = 'FooBar';
return Promise.resolve(err).delay(1);
});
wsServer.sendRequest('test', {code: 42}, function(err, res) {
err.name.should.equal('FooBar');
err.should.be.instanceof(WebSocket.RemoteError);
err.message.should.equal("foo");
endTest();
});
ws.onRequest('test', function(data) {
throw new Error('foo');
});
});
it('should not send Timeout after disconnected from server', endTest => {
ws.setResponseTimeout(20);
let timeout = setTimeout(() => {
endTest();
}, 30);
ws.sendRequest('test', {code: 42}, function(err, res) {
clearTimeout(timeout);
if(err) endTest(err);
});
setTimeout(() => {
wsServer.close();
}, 10);
wsServer.onRequest('test', function(data) {
return new Promise((resolve, reject) => {});
});
});
it('should not send Timeout after disconnected from client', endTest => {
ws.setResponseTimeout(10);
let timeout = setTimeout(() => {
endTest();
}, 15);
ws.sendRequest('test', {code: 42}, function(err, res) {
clearTimeout(timeout);
if(err) endTest(err);
});
setTimeout(() => {
ws.close();
}, 5);
wsServer.onRequest('test', function(data) {
return new Promise((resolve, reject) => {});
});
});
it('should send Error exceptions through promise handlers', endTest => {
ws.sendRequest('test', {code: 42}).then(function(res) {
//endTest();
}).catch(WebSocket.RemoteError, function(err) {
err.should.be.an.instanceOf(Error);
err.message.should.equal('error with stacktrace');
return Promise.resolve().then(function() {
throw err;
});
}).catch(WebSocket.RemoteError, function(err) {
err.stack.should.match(/remoteFunc/);
endTest();
});
wsServer.onRequest('test', function(data) {
function remoteFunc() {
throw new Error('error with stacktrace');
}
return Promise.resolve().then(remoteFunc);
});
});
it('should handle timeout errors', endTest => {
var gotTimeout = false;
ws.sendRequest('test1', {code: 42}, {responseTimeout: 10})
.then(function(res) {
res.should.equal('foo');
}).catch(Promise.TimeoutError, function(err) {
gotTimeout = true;
err.message.should.equal('operation timed out');
});
wsServer.onRequest('test1', function(data) {
return Promise.delay(20).then(function() {
if(gotTimeout)
endTest();
return 'foo';
});
});
});
it('should send events', endTest => {
ws.sendEvent('test', {
code: 'foo'
});
wsServer.onEvent('test', function(data) {
data.code.should.equal('foo');
endTest();
});
});
it('should not send events when removed', endTest => {
ws.sendEvent('test', {
code: 'foo'
});
wsServer.onEvent('test', function(data) {
assert(false, 'Should not happen');
});
wsServer.offEvent('test');
setTimeout(function() {
endTest();
}, 10);
});
it('should handle errors inside message handler', endTest => {
ws.sendEvent('test');
var tmp = console.error;
console.error = function(msg) {
};
wsServer.on('message', function(msg) {
throw new Error('test error');
});
wsServer.onEvent('test', function() {
console.error = tmp;
endTest();
});
});
it('should not break if remote party suddenly dies', endTest => {
ws.sendRequest('test1', {code: 42}).then(function(res) {
res.should.equal('foo');
});
wsServer.onRequest('test1', function(data) {
return Promise.delay(20).then(function() {
cancelRequestCheck = true;
ws.close();
}).delay(20).then(endTest);
});
});
describe('WebSocketServer', function() {
it('should emit close events', endTest => {
wss.close();
wsServer.on('close', function() {
endTest();
});
});
});
describe('WebSocket', function() {
it('should emit close events from client', endTest => {
ws.close(1000);
wsServer.on('close', function() {
endTest();
});
});
it('should emit close events from client with terminate', endTest => {
ws.terminate();
wsServer.on('close', function() {
endTest();
});
});
it('should emit close events from server', endTest => {
wsServer.close(1000);
ws.on('close', function() {
endTest();
});
});
it('should emit close events from server with terminate', endTest => {
wsServer.terminate();
ws.on('close', function() {
endTest();
});
});
it('should emit an error if closing with bad error number', endTest => {
ws.once('error', function(err) {
err.message.should.match(/first argument must be a valid error code number/);
endTest();
});
ws.close(0);
});
it('should emit an error if closing from server with bad error number', endTest => {
wsServer.once('error', function(err) {
err.message.should.match(/first argument must be a valid error code number/);
endTest();
});
wsServer.close(0);
});
});
describe('RequestHandler', function() {
let emitter, requestHandler;
before(() => {
emitter = new EventEmitter();
emitter.send = function(obj, cb) {
emitter.emit('message', obj);
if(cb) cb();
};
requestHandler = new RequestHandler(emitter);
})
function emitMessage(type, data) {
}
it('should emit event', endTest => {
requestHandler.onEvent('test', () => {
endTest();
})
requestHandler.sendEvent('test', 'foo');
});
it('should emit scoped event', endTest => {
let scoped = requestHandler.scope('foobar');
scoped.onEvent('test', () => {
endTest();
})
requestHandler.sendEvent('foobar:test', 'foo');
});
it('should not emit scoped event', endTest => {
let scoped = requestHandler.scope('foobar');
scoped.onEvent('test', () => {
endTest();
})
scoped.removeAllListeners();
scoped.onEvent('test2', data => {
data.should.equal('foo');
endTest();
})
scoped.sendEvent('test2', 'foo');
});
it('should handle requests', endTest => {
let scoped = requestHandler.scope('foobar');
scoped.onRequest('test', () => {
return 'bar';
});
scoped.sendRequest('test').then(data => {
data.should.equal('bar');
endTest();
})
});
});
});