UNPKG

8.85 kBJavaScriptView Raw
1
2/*!
3 * socket.io-node
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5 * MIT Licensed
6 */
7
8(function (module, io, should) {
9
10 var parser = io.parser;
11
12 module.exports = {
13
14 'decoding error packet': function () {
15 parser.decodePacket('7:::').should().eql({
16 type: 'error'
17 , reason: ''
18 , advice: ''
19 , endpoint: ''
20 });
21 },
22
23 'decoding error packet with reason': function () {
24 parser.decodePacket('7:::0').should().eql({
25 type: 'error'
26 , reason: 'transport not supported'
27 , advice: ''
28 , endpoint: ''
29 });
30 },
31
32 'decoding error packet with reason and advice': function () {
33 parser.decodePacket('7:::2+0').should().eql({
34 type: 'error'
35 , reason: 'unauthorized'
36 , advice: 'reconnect'
37 , endpoint: ''
38 });
39 },
40
41 'decoding error packet with endpoint': function () {
42 parser.decodePacket('7::/woot').should().eql({
43 type: 'error'
44 , reason: ''
45 , advice: ''
46 , endpoint: '/woot'
47 });
48 },
49
50 'decoding ack packet': function () {
51 parser.decodePacket('6:::140').should().eql({
52 type: 'ack'
53 , ackId: '140'
54 , endpoint: ''
55 , args: []
56 });
57 },
58
59 'decoding ack packet with args': function () {
60 parser.decodePacket('6:::12+["woot","wa"]').should().eql({
61 type: 'ack'
62 , ackId: '12'
63 , endpoint: ''
64 , args: ['woot', 'wa']
65 });
66 },
67
68 'decoding ack packet with bad json': function () {
69 var thrown = false;
70
71 try {
72 parser.decodePacket('6:::1+{"++]').should().eql({
73 type: 'ack'
74 , ackId: '1'
75 , endpoint: ''
76 , args: []
77 });
78 } catch (e) {
79 thrown = true;
80 }
81
82 thrown.should().be_false;
83 },
84
85 'decoding json packet': function () {
86 parser.decodePacket('4:::"2"').should().eql({
87 type: 'json'
88 , endpoint: ''
89 , data: '2'
90 });
91 },
92
93 'decoding json packet with message id and ack data': function () {
94 parser.decodePacket('4:1+::{"a":"b"}').should().eql({
95 type: 'json'
96 , id: 1
97 , ack: 'data'
98 , endpoint: ''
99 , data: { a: 'b' }
100 });
101 },
102
103 'decoding an event packet': function () {
104 parser.decodePacket('5:::{"name":"woot"}').should().eql({
105 type: 'event'
106 , name: 'woot'
107 , endpoint: ''
108 , args: []
109 });
110 },
111
112 'decoding an event packet with message id and ack': function () {
113 parser.decodePacket('5:1+::{"name":"tobi"}').should().eql({
114 type: 'event'
115 , id: 1
116 , ack: 'data'
117 , endpoint: ''
118 , name: 'tobi'
119 , args: []
120 });
121 },
122
123 'decoding an event packet with data': function () {
124 parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}')
125 .should().eql({
126 type: 'event'
127 , name: 'edwald'
128 , endpoint: ''
129 , args: [{a: 'b'}, 2, '3']
130 });
131 },
132
133 'decoding a message packet': function () {
134 parser.decodePacket('3:::woot').should().eql({
135 type: 'message'
136 , endpoint: ''
137 , data: 'woot'
138 });
139 },
140
141 'decoding a message packet with id and endpoint': function () {
142 parser.decodePacket('3:5:/tobi').should().eql({
143 type: 'message'
144 , id: 5
145 , ack: true
146 , endpoint: '/tobi'
147 , data: ''
148 });
149 },
150
151 'decoding a heartbeat packet': function () {
152 parser.decodePacket('2:::').should().eql({
153 type: 'heartbeat'
154 , endpoint: ''
155 });
156 },
157
158 'decoding a connection packet': function () {
159 parser.decodePacket('1::/tobi').should().eql({
160 type: 'connect'
161 , endpoint: '/tobi'
162 , qs: ''
163 });
164 },
165
166 'decoding a connection packet with query string': function () {
167 parser.decodePacket('1::/test:?test=1').should().eql({
168 type: 'connect'
169 , endpoint: '/test'
170 , qs: '?test=1'
171 });
172 },
173
174 'decoding a disconnection packet': function () {
175 parser.decodePacket('0::/woot').should().eql({
176 type: 'disconnect'
177 , endpoint: '/woot'
178 });
179 },
180
181 'encoding error packet': function () {
182 parser.encodePacket({
183 type: 'error'
184 , reason: ''
185 , advice: ''
186 , endpoint: ''
187 }).should().eql('7::');
188 },
189
190 'encoding error packet with reason': function () {
191 parser.encodePacket({
192 type: 'error'
193 , reason: 'transport not supported'
194 , advice: ''
195 , endpoint: ''
196 }).should().eql('7:::0');
197 },
198
199 'encoding error packet with reason and advice': function () {
200 parser.encodePacket({
201 type: 'error'
202 , reason: 'unauthorized'
203 , advice: 'reconnect'
204 , endpoint: ''
205 }).should().eql('7:::2+0');
206 },
207
208 'encoding error packet with endpoint': function () {
209 parser.encodePacket({
210 type: 'error'
211 , reason: ''
212 , advice: ''
213 , endpoint: '/woot'
214 }).should().eql('7::/woot');
215 },
216
217 'encoding ack packet': function () {
218 parser.encodePacket({
219 type: 'ack'
220 , ackId: '140'
221 , endpoint: ''
222 , args: []
223 }).should().eql('6:::140');
224 },
225
226 'encoding ack packet with args': function () {
227 parser.encodePacket({
228 type: 'ack'
229 , ackId: '12'
230 , endpoint: ''
231 , args: ['woot', 'wa']
232 }).should().eql('6:::12+["woot","wa"]');
233 },
234
235 'encoding json packet': function () {
236 parser.encodePacket({
237 type: 'json'
238 , endpoint: ''
239 , data: '2'
240 }).should().eql('4:::"2"');
241 },
242
243 'encoding json packet with message id and ack data': function () {
244 parser.encodePacket({
245 type: 'json'
246 , id: 1
247 , ack: 'data'
248 , endpoint: ''
249 , data: { a: 'b' }
250 }).should().eql('4:1+::{"a":"b"}');
251 },
252
253 'encoding an event packet': function () {
254 parser.encodePacket({
255 type: 'event'
256 , name: 'woot'
257 , endpoint: ''
258 , args: []
259 }).should().eql('5:::{"name":"woot"}');
260 },
261
262 'encoding an event packet with message id and ack': function () {
263 parser.encodePacket({
264 type: 'event'
265 , id: 1
266 , ack: 'data'
267 , endpoint: ''
268 , name: 'tobi'
269 , args: []
270 }).should().eql('5:1+::{"name":"tobi"}');
271 },
272
273 'encoding an event packet with data': function () {
274 parser.encodePacket({
275 type: 'event'
276 , name: 'edwald'
277 , endpoint: ''
278 , args: [{a: 'b'}, 2, '3']
279 }).should().eql('5:::{"name":"edwald","args":[{"a":"b"},2,"3"]}');
280 },
281
282 'encoding a message packet': function () {
283 parser.encodePacket({
284 type: 'message'
285 , endpoint: ''
286 , data: 'woot'
287 }).should().eql('3:::woot');
288 },
289
290 'encoding a message packet with id and endpoint': function () {
291 parser.encodePacket({
292 type: 'message'
293 , id: 5
294 , ack: true
295 , endpoint: '/tobi'
296 , data: ''
297 }).should().eql('3:5:/tobi');
298 },
299
300 'encoding a heartbeat packet': function () {
301 parser.encodePacket({
302 type: 'heartbeat'
303 , endpoint: ''
304 }).should().eql('2::');
305 },
306
307 'encoding a connection packet': function () {
308 parser.encodePacket({
309 type: 'connect'
310 , endpoint: '/tobi'
311 , qs: ''
312 }).should().eql('1::/tobi');
313 },
314
315 'encoding a connection packet with query string': function () {
316 parser.encodePacket({
317 type: 'connect'
318 , endpoint: '/test'
319 , qs: '?test=1'
320 }).should().eql('1::/test:?test=1');
321 },
322
323 'encoding a disconnection packet': function () {
324 parser.encodePacket({
325 type: 'disconnect'
326 , endpoint: '/woot'
327 }).should().eql('0::/woot');
328 },
329
330 'test decoding a payload': function () {
331 parser.decodePayload('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d'
332 + '\ufffd3\ufffd0::').should().eql([
333 { type: 'message', data: '5', endpoint: '' }
334 , { type: 'message', data: '53d', endpoint: '' }
335 , { type: 'disconnect', endpoint: '' }
336 ]);
337 },
338
339 'test encoding a payload': function () {
340 parser.encodePayload([
341 parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
342 , parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
343 ]).should().eql('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d')
344 }
345
346 };
347
348})(
349 'undefined' == typeof module ? module = {} : module
350 , 'undefined' == typeof io ? require('socket.io-client') : io
351 , 'undefined' == typeof should ? require('should') : should
352);