forked from MissionSung/actionhero
/
websocket.js
468 lines (395 loc) · 14.7 KB
/
websocket.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
var should = require('should');
var request = require('request');
var EventEmitter = require('events').EventEmitter
var actionheroPrototype = require(__dirname + '/../../actionhero.js').actionheroPrototype;
var actionhero = new actionheroPrototype();
var api;
var clientA;
var clientB;
var clientC;
var url
var connectClients = function(callback){
// get actionheroClient in scope
// TODO: Perhaps we read this from disk after server boot.
eval( api.servers.servers.websocket.compileActionheroClientJS() );
var S = api.servers.servers.websocket.server.Socket;
var url = 'http://localhost:' + api.config.servers.web.port;
var clientAsocket = new S(url);
var clientBsocket = new S(url);
var clientCsocket = new S(url);
clientA = new ActionheroClient({}, clientAsocket);
clientB = new ActionheroClient({}, clientBsocket);
clientC = new ActionheroClient({}, clientCsocket);
setTimeout(function(){
callback();
}, 100);
}
describe('Server: Web Socket', function(){
before(function(done){
actionhero.start(function(err, a){
api = a;
url = 'http://localhost:' + api.config.servers.web.port;
api.config.servers.websocket.clientUrl = 'http://localhost:' + api.config.servers.web.port;
connectClients(function(){
done();
});
});
});
after(function(done){
actionhero.stop(function(){
done();
});
});
it('socket client connections should work: client 1', function(done){
clientA.connect(function(err, data){
data.context.should.equal('response');
data.data.totalActions.should.equal(0);
clientA.welcomeMessage.should.equal('Hello! Welcome to the actionhero api');
done();
});
});
it('socket client connections should work: client 2', function(done){
clientB.connect(function(err, data){
data.context.should.equal('response');
data.data.totalActions.should.equal(0);
clientA.welcomeMessage.should.equal('Hello! Welcome to the actionhero api');
done();
});
});
it('socket client connections should work: client 3', function(done){
clientC.connect(function(err, data){
data.context.should.equal('response');
data.data.totalActions.should.equal(0);
clientA.welcomeMessage.should.equal('Hello! Welcome to the actionhero api');
done();
});
});
it('I can get my connection details', function(done){
clientA.detailsView(function(response){
response.data.connectedAt.should.be.within(0, new Date().getTime())
response.data.remoteIP.should.equal('127.0.0.1');
done()
});
});
it('can run actions with errors', function(done){
clientA.action('cacheTest', function(response){
response.error.should.equal('key is a required parameter for this action');
done();
});
});
it('can run actions properly', function(done){
clientA.action('cacheTest', {key: 'test key', value: 'test value'}, function(response){
should.not.exist(response.error);
done();
});
});
it('has sticky params', function(done){
clientA.action('cacheTest', {key: 'test key', value: 'test value'}, function(response){
should.not.exist(response.error);
response.cacheTestResults.loadResp.key.should.equal('cacheTest_test key');
response.cacheTestResults.loadResp.value.should.equal('test value');
clientA.action('cacheTest', {key: 'test key', value: 'test value'}, function(response){
response.cacheTestResults.loadResp.key.should.equal('cacheTest_test key');
response.cacheTestResults.loadResp.value.should.equal('test value');
done();
});
});
});
it('will limit how many simultaneous connections I can have', function(done){
var responses = [];
clientA.action('sleepTest', {sleepDuration: 100}, function(response){ responses.push(response) })
clientA.action('sleepTest', {sleepDuration: 200}, function(response){ responses.push(response) })
clientA.action('sleepTest', {sleepDuration: 300}, function(response){ responses.push(response) })
clientA.action('sleepTest', {sleepDuration: 400}, function(response){ responses.push(response) })
clientA.action('sleepTest', {sleepDuration: 500}, function(response){ responses.push(response) })
clientA.action('sleepTest', {sleepDuration: 600}, function(response){ responses.push(response) })
setTimeout(function(){
responses.length.should.equal(6);
for(var i in responses){
var response = responses[i];
if(i === 0 || i === '0'){
response.error.should.eql('you have too many pending requests');
} else {
should.not.exist(response.error)
}
}
done();
}, 1000);
});
describe('files', function(){
it('can request file data', function(done){
clientA.file('simple.html', function(data){
should.not.exist(data.error);
data.content.should.equal('<h1>ActionHero</h1>\\nI am a flat file being served to you via the API from ./public/simple.html<br />');
data.mime.should.equal('text/html');
data.length.should.equal(101);
done();
});
});
it('missing files', function(done){
clientA.file('missing.html', function(data){
data.error.should.equal( api.config.errors.fileNotFound() );
data.mime.should.equal('text/html');
should.not.exist(data.content);
done();
});
});
});
describe('chat', function(){
before(function(done){
api.chatRoom.addMiddleware({
name: 'join chat middleware',
join: function(connection, room, callback){
api.chatRoom.broadcast({}, room, 'I have entered the room: ' + connection.id, function(e){
callback();
});
}
});
api.chatRoom.addMiddleware({
name: 'leave chat middleware',
leave: function(connection, room, callback){
api.chatRoom.broadcast({}, room, 'I have left the room: ' + connection.id, function(e){
callback();
});
}
});
done();
})
after(function(done){
api.chatRoom.middleware = {};
api.chatRoom.globalMiddleware = [];
done();
})
beforeEach(function(done){
clientA.roomAdd('defaultRoom',function(){
clientB.roomAdd('defaultRoom',function(){
clientC.roomAdd('defaultRoom',function(){
setTimeout(function(){ // timeout to skip welcome messages as clients join rooms
done();
}, 500);
});
});
});
});
afterEach(function(done){
clientA.roomLeave('defaultRoom',function(){
clientB.roomLeave('defaultRoom',function(){
clientC.roomLeave('defaultRoom',function(){
clientA.roomLeave('otherRoom',function(){
clientB.roomLeave('otherRoom',function(){
clientC.roomLeave('otherRoom',function(){
done();
}); }); }); }); }); });
});
it('can change rooms and get room details', function(done){
clientA.roomAdd('otherRoom', function(){
clientA.detailsView(function(response){
should.not.exist(response.error);
response.data.rooms[0].should.equal('defaultRoom');
response.data.rooms[1].should.equal('otherRoom');
clientA.roomView('otherRoom', function(response){
response.data.membersCount.should.equal(1);
done();
});
});
});
});
it('will update client room info when they change rooms', function(done){
clientA.rooms[0].should.equal('defaultRoom');
should.not.exist( clientA.rooms[1] );
clientA.roomAdd('otherRoom', function(response){
should.not.exist(response.error);
clientA.rooms[0].should.equal('defaultRoom');
clientA.rooms[1].should.equal('otherRoom');
clientA.roomLeave('defaultRoom', function(response){
should.not.exist(response.error);
clientA.rooms[0].should.equal('otherRoom');
should.not.exist( clientA.rooms[1] );
done();
});
});
})
it('Clients can talk to each other', function(done){
var listener = function(response){
clientA.removeListener('say', listener);
response.context.should.equal('user');
response.message.should.equal('hello from client 2');
done();
};
clientA.on('say', listener);
clientB.say('defaultRoom', 'hello from client 2');
});
it('The client say method does not rely on order', function(done){
var listener = function(response){
clientA.removeListener('say', listener);
response.context.should.equal('user');
response.message.should.equal('hello from client 2');
done();
};
clientB.say = function(room, message, callback){
this.send({message: message, room: room, event: 'say'}, callback);
}
clientA.on('say', listener);
clientB.say('defaultRoom', 'hello from client 2');
});
it('connections are notified when I join a room', function(done){
var listener = function(response){
clientA.removeListener('say', listener);
response.context.should.equal('user');
response.message.should.equal('I have entered the room: ' + clientB.id);
done();
};
clientA.roomAdd('otherRoom', function(){
clientA.on('say', listener);
clientB.roomAdd('otherRoom');
});
});
it('connections are notified when I leave a room', function(done){
var listener = function(response){
clientA.removeListener('say', listener);
response.context.should.equal('user');
response.message.should.equal('I have left the room: ' + clientB.id);
done();
}
clientA.on('say', listener);
clientB.roomLeave('defaultRoom');
});
it('will not get messages for rooms I am not in', function(done){
clientB.roomAdd('otherRoom', function(response){
should.not.exist(response.error);
clientB.rooms.length.should.equal(2);
var listener = function(response){
clientC.removeListener('say', listener);
should.not.exist(response);
};
clientC.rooms.length.should.equal(1);
clientC.on('say', listener);
setTimeout(function(){
clientC.removeListener('say', listener);
done();
}, 1000)
clientB.say('otherRoom', 'you should not hear this');
});
});
it('connections can see member counts changing within rooms as folks join and leave', function(done){
clientA.roomView('defaultRoom', function(response){
response.data.membersCount.should.equal(3);
clientB.roomLeave('defaultRoom', function(){
clientA.roomView('defaultRoom', function(response){
response.data.membersCount.should.equal(2);
done();
});
});
});
});
describe('custom room member data', function(){
var currentSanitize;
var currentGenerate;
before(function(done){
//Ensure that default behavior works
clientA.roomAdd('defaultRoom',function(){
clientA.roomView('defaultRoom', function(response){
response.data.room.should.equal('defaultRoom');
for( var key in response.data.members ){
( response.data.members[key].type === undefined ).should.eql(true);
}
//save off current functions
currentSanitize = api.chatRoom.sanitizeMemberDetails;
currentGenerate = api.chatRoom.generateMemberDetails;
//override functions
api.chatRoom.sanitizeMemberDetails = function(data){
return { id: data.id,
joinedAt: data.joinedAt,
type: data.type };
}
api.chatRoom.generateMemberDetails = function(connection){
return { id: connection.id,
joinedAt: new Date().getTime(),
type : connection.type };
}
clientA.roomLeave('defaultRoom', function(){
done();
});
});
});
});
after(function(done){
api.chatRoom.joinCallbacks = {};
api.chatRoom.leaveCallbacks = {};
api.chatRoom.sanitizeMemberDetails = currentSanitize;
api.chatRoom.generateMemberDetails = currentGenerate;
//Check that everything is back to normal
clientA.roomAdd('defaultRoom',function(){
clientA.roomView('defaultRoom', function(response){
response.data.room.should.equal('defaultRoom');
for( var key in response.data.members ){
( response.data.members[key].type === undefined ).should.eql(true);
}
setTimeout(function(){
clientA.roomLeave('defaultRoom', function(){
done();
});
}, 100);
});
});
});
it('should view non-default member data', function(done){
clientA.roomAdd('defaultRoom',function(){
clientA.roomView('defaultRoom', function(response){
response.data.room.should.equal('defaultRoom');
for( var key in response.data.members ){
response.data.members[key].type.should.eql('websocket');
}
clientA.roomLeave('defaultRoom');
done();
});
})
});
});
});
describe('fingerprint', function(){
// TODO: Cannot test socket within a browser context
// public/linkedSession.html has been provided as an example for now
it('will have the same fingerprint as the browser cookie which spawned the connection');
});
describe('disconnect', function(){
beforeEach(function(done){
try{
clientA.disconnect();
clientB.disconnect();
clientC.disconnect();
}catch(e){}
connectClients(function(){
clientA.connect();
clientB.connect();
clientC.connect();
setTimeout(done, 500);
});
});
it('client can disconnect', function(done){
api.servers.servers.websocket.connections().length.should.equal(3);
clientA.disconnect();
clientB.disconnect();
clientC.disconnect();
setTimeout(function(){
api.servers.servers.websocket.connections().length.should.equal(0);
done();
}, 500);
});
it('can be sent disconnect events from the server', function(done){
clientA.detailsView(function(response){
response.data.remoteIP.should.equal('127.0.0.1');
var count = 0
for(var id in api.connections.connections){
count++;
api.connections.connections[id].destroy();
}
count.should.equal(3);
clientA.detailsView(function(){
throw new Error("should not get responst")
});
setTimeout(done, 500)
});
});
});
});