|
1
|
|
|
var should = require('should'); |
|
2
|
|
|
var io = require('socket.io-client'), |
|
3
|
|
|
server = require('../index'); |
|
4
|
|
|
|
|
5
|
|
|
var socketURL = 'http://localhost:5000'; |
|
6
|
|
|
|
|
7
|
|
|
var options ={ |
|
8
|
|
|
transports: ['websocket'], |
|
9
|
|
|
'force new connection': true |
|
10
|
|
|
}; |
|
11
|
|
|
|
|
12
|
|
|
var chatUser1 = {'name':'Tom'}; |
|
13
|
|
|
var chatUser2 = {'name':'Sally'}; |
|
14
|
|
|
var chatUser3 = {'name':'Dana'}; |
|
15
|
|
|
|
|
16
|
|
|
let request = require('supertest'); |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
describe("Chat Server",function(){ |
|
19
|
|
|
let server; |
|
20
|
|
|
beforeEach(function () { |
|
21
|
|
|
delete require.cache[require.resolve('../index')]; |
|
22
|
|
|
server = require('../index'); |
|
23
|
|
|
}); |
|
24
|
|
|
afterEach(function (done) { |
|
25
|
|
|
server.close(done); |
|
26
|
|
|
}); |
|
27
|
|
|
/* Test 1 - A Single User */ |
|
28
|
|
|
it('Should broadcast new user once they connect',function(done){ |
|
29
|
|
|
var client = io.connect(socketURL, options); |
|
30
|
|
|
|
|
31
|
|
|
client.on('connect',function(data){ |
|
|
|
|
|
|
32
|
|
|
client.emit('connection name',chatUser1); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
client.on('new user',function(usersName){ |
|
36
|
|
|
usersName.should.be.type('string'); |
|
37
|
|
|
usersName.should.equal(chatUser1.name + " has joined."); |
|
38
|
|
|
/* If this client doesn't disconnect it will interfere |
|
39
|
|
|
with the next test */ |
|
40
|
|
|
client.disconnect(); |
|
41
|
|
|
done(); |
|
42
|
|
|
}); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
/* Test 2 - Two Users */ |
|
46
|
|
|
it('Should broadcast new user to all users', function(done){ |
|
47
|
|
|
var client1 = io.connect(socketURL, options); |
|
48
|
|
|
|
|
49
|
|
|
client1.on('connect', function(data){ |
|
|
|
|
|
|
50
|
|
|
client1.emit('connection name', chatUser1); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/* Since first client is connected, we connect |
|
54
|
|
|
the second client. */ |
|
55
|
|
|
var client2 = io.connect(socketURL, options); |
|
56
|
|
|
|
|
57
|
|
|
client2.on('connect', function(data){ |
|
|
|
|
|
|
58
|
|
|
client2.emit('connection name', chatUser2); |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
client2.on('new user', function(usersName){ |
|
62
|
|
|
usersName.should.equal(chatUser2.name + " has joined."); |
|
63
|
|
|
client2.disconnect(); |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
var numUsers = 0; |
|
69
|
|
|
client1.on('new user', function(usersName){ |
|
70
|
|
|
numUsers += 1; |
|
71
|
|
|
|
|
72
|
|
|
if(numUsers === 2){ |
|
73
|
|
|
usersName.should.equal(chatUser2.name + " has joined."); |
|
74
|
|
|
client1.disconnect(); |
|
75
|
|
|
done(); |
|
76
|
|
|
} |
|
77
|
|
|
}); |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
/* Test 3 - User sends a message to chat room. */ |
|
81
|
|
|
it('Should be able to broadcast messages', function(done){ |
|
82
|
|
|
var client1, client2, client3; |
|
83
|
|
|
var message = 'Hello World'; |
|
84
|
|
|
var messages = 0; |
|
85
|
|
|
|
|
86
|
|
|
var checkMessage = function(client){ |
|
87
|
|
|
client.on('message', function(msg){ |
|
88
|
|
|
message.should.equal(msg); |
|
89
|
|
|
client.disconnect(); |
|
90
|
|
|
messages++; |
|
91
|
|
|
if(messages === 3){ |
|
92
|
|
|
done(); |
|
93
|
|
|
}; |
|
94
|
|
|
}); |
|
95
|
|
|
}; |
|
96
|
|
|
|
|
97
|
|
|
client1 = io.connect(socketURL, options); |
|
98
|
|
|
checkMessage(client1); |
|
99
|
|
|
|
|
100
|
|
|
client1.on('connect', function(data){ |
|
|
|
|
|
|
101
|
|
|
client2 = io.connect(socketURL, options); |
|
102
|
|
|
checkMessage(client2); |
|
103
|
|
|
|
|
104
|
|
|
client2.on('connect', function(data){ |
|
|
|
|
|
|
105
|
|
|
client3 = io.connect(socketURL, options); |
|
106
|
|
|
checkMessage(client3); |
|
107
|
|
|
|
|
108
|
|
|
client3.on('connect', function(data){ |
|
|
|
|
|
|
109
|
|
|
client2.send(message); |
|
110
|
|
|
}); |
|
111
|
|
|
}); |
|
112
|
|
|
}); |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
/* Test 4 - User sends a private message to another user. */ |
|
116
|
|
|
it('Should be able to send private messages', function(done){ |
|
117
|
|
|
var client1, client2, client3; |
|
118
|
|
|
var message = {to: chatUser1.name, txt:'Private Hello World'}; |
|
119
|
|
|
var messages = 0; |
|
120
|
|
|
|
|
121
|
|
|
var completeTest = function(){ |
|
122
|
|
|
messages.should.equal(1); |
|
123
|
|
|
client1.disconnect(); |
|
124
|
|
|
client2.disconnect(); |
|
125
|
|
|
client3.disconnect(); |
|
126
|
|
|
done(); |
|
127
|
|
|
}; |
|
128
|
|
|
|
|
129
|
|
|
var checkPrivateMessage = function(client){ |
|
130
|
|
|
client.on('private message', function(msg){ |
|
131
|
|
|
message.txt.should.equal(msg.txt); |
|
132
|
|
|
msg.from.should.equal(chatUser3.name); |
|
133
|
|
|
messages++; |
|
134
|
|
|
if(client === client1){ |
|
135
|
|
|
/* The first client has received the message |
|
136
|
|
|
we give some time to ensure that the others |
|
137
|
|
|
will not receive the same message. */ |
|
138
|
|
|
setTimeout(completeTest, 40); |
|
139
|
|
|
}; |
|
140
|
|
|
}); |
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
client1 = io.connect(socketURL, options); |
|
144
|
|
|
checkPrivateMessage(client1); |
|
145
|
|
|
|
|
146
|
|
|
client1.on('connect', function(data){ |
|
|
|
|
|
|
147
|
|
|
client1.emit('connection name', chatUser1); |
|
148
|
|
|
client2 = io.connect(socketURL, options); |
|
149
|
|
|
checkPrivateMessage(client2); |
|
150
|
|
|
|
|
151
|
|
|
client2.on('connect', function(data){ |
|
|
|
|
|
|
152
|
|
|
client2.emit('connection name', chatUser2); |
|
153
|
|
|
client3 = io.connect(socketURL, options); |
|
154
|
|
|
checkPrivateMessage(client3); |
|
155
|
|
|
|
|
156
|
|
|
client3.on('connect', function(data){ |
|
|
|
|
|
|
157
|
|
|
client3.emit('connection name', chatUser3); |
|
158
|
|
|
client3.emit('private message', message) |
|
159
|
|
|
}); |
|
160
|
|
|
}); |
|
161
|
|
|
}); |
|
162
|
|
|
}); |
|
163
|
|
|
}); |
|
164
|
|
|
|