1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (module, io, should) {
|
9 |
|
10 | if ('object' == typeof global) {
|
11 | return module.exports = { '': function () {} };
|
12 | }
|
13 |
|
14 | module.exports = {
|
15 |
|
16 | 'test connecting the socket and disconnecting': function (next) {
|
17 | var socket = create();
|
18 |
|
19 | socket.on('error', function (msg) {
|
20 | throw new Error(msg || 'Received an error');
|
21 | });
|
22 |
|
23 | socket.on('connect', function () {
|
24 | socket.disconnect();
|
25 | next();
|
26 | });
|
27 | },
|
28 |
|
29 | 'test receiving messages': function (next) {
|
30 | var socket = create()
|
31 | , connected = false
|
32 | , messages = 0;
|
33 |
|
34 | socket.on('error', function (msg) {
|
35 | throw new Error(msg || 'Received an error');
|
36 | });
|
37 |
|
38 | socket.on('connect', function () {
|
39 | connected = true;
|
40 | });
|
41 |
|
42 | socket.on('message', function (i) {
|
43 | String(++messages).should().equal(i);
|
44 | });
|
45 |
|
46 | socket.on('disconnect', function (reason) {
|
47 | connected.should().be_true;
|
48 | messages.should().equal(3);
|
49 | reason.should().eql('booted');
|
50 | next();
|
51 | });
|
52 | },
|
53 |
|
54 | 'test sending messages': function (next) {
|
55 | var socket = create();
|
56 |
|
57 | socket.on('error', function (msg) {
|
58 | throw new Error(msg || 'Received an error');
|
59 | });
|
60 |
|
61 | socket.on('connect', function () {
|
62 | socket.send('echo');
|
63 |
|
64 | socket.on('message', function (msg) {
|
65 | msg.should().equal('echo');
|
66 | socket.disconnect();
|
67 | next();
|
68 | });
|
69 | });
|
70 | },
|
71 |
|
72 | 'test acks sent from client': function (next) {
|
73 | var socket = create();
|
74 |
|
75 | socket.on('error', function (msg) {
|
76 | throw new Error(msg || 'Received an error');
|
77 | });
|
78 |
|
79 | socket.on('connect', function () {
|
80 | socket.on('message', function (msg) {
|
81 | if ('tobi 2' == msg) {
|
82 | socket.disconnect();
|
83 | next();
|
84 | }
|
85 | });
|
86 | });
|
87 | },
|
88 |
|
89 | 'test acks sent from server': function (next) {
|
90 | var socket = create();
|
91 |
|
92 | socket.on('error', function (msg) {
|
93 | throw new Error(msg || 'Received an error');
|
94 | });
|
95 |
|
96 | socket.on('connect', function () {
|
97 | socket.send('ooo', function () {
|
98 | socket.disconnect();
|
99 | next();
|
100 | });
|
101 | });
|
102 | },
|
103 |
|
104 | 'test connecting to namespaces': function (next) {
|
105 | var io = create()
|
106 | , socket = io.socket
|
107 | , namespaces = 2
|
108 | , connect = 0;
|
109 |
|
110 | function finish () {
|
111 | socket.of('').disconnect();
|
112 | connect.should().equal(3);
|
113 | next();
|
114 | }
|
115 |
|
116 | io.on('connect', function(){
|
117 | connect++;
|
118 | });
|
119 |
|
120 | socket.of('/woot').on('connect', function () {
|
121 | connect++;
|
122 | }).on('message', function (msg) {
|
123 | msg.should().equal('connected to woot');
|
124 | --namespaces || finish();
|
125 | }).on('error', function (msg) {
|
126 | throw new Error(msg || 'Received an error');
|
127 | });
|
128 |
|
129 | socket.of('/chat').on('connect', function () {
|
130 | connect++;
|
131 | }).on('message', function (msg) {
|
132 | msg.should().equal('connected to chat');
|
133 | --namespaces || finish();
|
134 | }).on('error', function (msg) {
|
135 | throw new Error(msg || 'Received an error');
|
136 | });
|
137 |
|
138 | },
|
139 |
|
140 | 'test disconnecting from namespaces': function (next) {
|
141 | var socket = create().socket
|
142 | , namespaces = 2
|
143 | , disconnections = 0;
|
144 |
|
145 | function finish () {
|
146 | socket.of('').disconnect();
|
147 | next();
|
148 | };
|
149 |
|
150 | socket.of('/a').on('error', function (msg) {
|
151 | throw new Error(msg || 'Received an error');
|
152 | });
|
153 |
|
154 | socket.of('/a').on('connect', function () {
|
155 | socket.of('/a').disconnect();
|
156 | });
|
157 |
|
158 | socket.of('/a').on('disconnect', function () {
|
159 | --namespaces || finish();
|
160 | });
|
161 |
|
162 | socket.of('/b').on('error', function (msg) {
|
163 | throw new Error(msg || 'Received an error');
|
164 | });
|
165 |
|
166 | socket.of('/b').on('connect', function () {
|
167 | socket.of('/b').disconnect();
|
168 | });
|
169 |
|
170 | socket.of('/b').on('disconnect', function () {
|
171 | --namespaces || finish();
|
172 | });
|
173 | },
|
174 |
|
175 | 'test authorizing for namespaces': function (next) {
|
176 | var socket = create().socket
|
177 |
|
178 | function finish () {
|
179 | socket.of('').disconnect();
|
180 | next();
|
181 | };
|
182 |
|
183 | socket.of('/a')
|
184 | .on('connect_failed', function (msg) {
|
185 | next();
|
186 | })
|
187 | .on('error', function (msg) {
|
188 | throw new Error(msg || 'Received an error');
|
189 | });
|
190 | },
|
191 |
|
192 | 'test sending json from server': function (next) {
|
193 | var socket = create();
|
194 |
|
195 | socket.on('error', function (msg) {
|
196 | throw new Error(msg || 'Received an error');
|
197 | });
|
198 |
|
199 | socket.on('message', function (msg) {
|
200 | msg.should().eql(3141592);
|
201 | socket.disconnect();
|
202 | next();
|
203 | });
|
204 | },
|
205 |
|
206 | 'test sending json from client': function (next) {
|
207 | var socket = create();
|
208 |
|
209 | socket.on('error', function (msg) {
|
210 | throw new Error(msg || 'Received an error');
|
211 | });
|
212 |
|
213 | socket.json.send([1, 2, 3]);
|
214 | socket.on('message', function (msg) {
|
215 | msg.should().equal('echo');
|
216 | socket.disconnect();
|
217 | next();
|
218 | });
|
219 | },
|
220 |
|
221 | 'test emitting an event from server': function (next) {
|
222 | var socket = create();
|
223 |
|
224 | socket.on('error', function (msg) {
|
225 | throw new Error(msg || 'Received an error');
|
226 | });
|
227 |
|
228 | socket.on('woot', function () {
|
229 | socket.disconnect();
|
230 | next();
|
231 | });
|
232 | },
|
233 |
|
234 | 'test emitting an event to server': function (next) {
|
235 | var socket = create();
|
236 |
|
237 | socket.on('error', function (msg) {
|
238 | throw new Error(msg || 'Received an error');
|
239 | });
|
240 |
|
241 | socket.emit('woot');
|
242 | socket.on('echo', function () {
|
243 | socket.disconnect();
|
244 | next();
|
245 | })
|
246 | },
|
247 |
|
248 | 'test emitting an event from server and sending back data': function (next) {
|
249 | var socket = create();
|
250 |
|
251 | socket.on('error', function (msg) {
|
252 | throw new Error(msg || 'Received an error');
|
253 | });
|
254 |
|
255 | socket.on('woot', function (a, fn) {
|
256 | a.should().eql(1);
|
257 | fn('test');
|
258 |
|
259 | socket.on('done', function () {
|
260 | socket.disconnect();
|
261 | next();
|
262 | });
|
263 | });
|
264 | },
|
265 |
|
266 | 'test emitting an event to server and sending back data': function (next) {
|
267 | var socket = create();
|
268 |
|
269 | socket.on('error', function (msg) {
|
270 | throw new Error(msg || 'Received an error');
|
271 | });
|
272 |
|
273 | socket.emit('tobi', 1, 2, function (a) {
|
274 | a.should().eql({ hello: 'world' });
|
275 | socket.disconnect();
|
276 | next();
|
277 | });
|
278 | }
|
279 |
|
280 | };
|
281 |
|
282 | })(
|
283 | 'undefined' == typeof module ? module = {} : module
|
284 | , 'undefined' == typeof io ? require('socket.io-client') : io
|
285 | , 'undefined' == typeof should ? require('should-browser') : should
|
286 | );
|