]>
Commit | Line | Data |
---|---|---|
2cccf753 SR |
1 | var assert = chai.assert; |
2 | var expect = chai.expect; | |
3 | ||
dfae3209 SR |
4 | import Websock from '../core/websock.js'; |
5 | import FakeWebSocket from './fake.websocket.js'; | |
6 | ||
0aaf59c2 | 7 | import sinon from '../vendor/sinon.js'; |
dfae3209 | 8 | |
2cccf753 SR |
9 | describe('Websock', function() { |
10 | "use strict"; | |
11 | ||
12 | describe('Queue methods', function () { | |
13 | var sock; | |
38781d93 | 14 | var RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); |
2cccf753 SR |
15 | |
16 | beforeEach(function () { | |
17 | sock = new Websock(); | |
38781d93 SR |
18 | // skip init |
19 | sock._allocate_buffers(); | |
20 | sock._rQ.set(RQ_TEMPLATE); | |
21 | sock._rQlen = RQ_TEMPLATE.length; | |
2cccf753 SR |
22 | }); |
23 | describe('rQlen', function () { | |
24 | it('should return the length of the receive queue', function () { | |
25 | sock.set_rQi(0); | |
26 | ||
27 | expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length); | |
28 | }); | |
29 | ||
30 | it("should return the proper length if we read some from the receive queue", function () { | |
31 | sock.set_rQi(1); | |
32 | ||
33 | expect(sock.rQlen()).to.equal(RQ_TEMPLATE.length - 1); | |
34 | }); | |
35 | }); | |
36 | ||
37 | describe('rQpeek8', function () { | |
38 | it('should peek at the next byte without poping it off the queue', function () { | |
39 | var bef_len = sock.rQlen(); | |
40 | var peek = sock.rQpeek8(); | |
41 | expect(sock.rQpeek8()).to.equal(peek); | |
42 | expect(sock.rQlen()).to.equal(bef_len); | |
43 | }); | |
44 | }); | |
45 | ||
46 | describe('rQshift8', function () { | |
47 | it('should pop a single byte from the receive queue', function () { | |
48 | var peek = sock.rQpeek8(); | |
49 | var bef_len = sock.rQlen(); | |
50 | expect(sock.rQshift8()).to.equal(peek); | |
51 | expect(sock.rQlen()).to.equal(bef_len - 1); | |
52 | }); | |
53 | }); | |
54 | ||
2cccf753 SR |
55 | describe('rQshift16', function () { |
56 | it('should pop two bytes from the receive queue and return a single number', function () { | |
57 | var bef_len = sock.rQlen(); | |
58 | var expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1]; | |
59 | expect(sock.rQshift16()).to.equal(expected); | |
60 | expect(sock.rQlen()).to.equal(bef_len - 2); | |
61 | }); | |
62 | }); | |
63 | ||
64 | describe('rQshift32', function () { | |
65 | it('should pop four bytes from the receive queue and return a single number', function () { | |
66 | var bef_len = sock.rQlen(); | |
67 | var expected = (RQ_TEMPLATE[0] << 24) + | |
68 | (RQ_TEMPLATE[1] << 16) + | |
69 | (RQ_TEMPLATE[2] << 8) + | |
70 | RQ_TEMPLATE[3]; | |
71 | expect(sock.rQshift32()).to.equal(expected); | |
72 | expect(sock.rQlen()).to.equal(bef_len - 4); | |
73 | }); | |
74 | }); | |
75 | ||
76 | describe('rQshiftStr', function () { | |
77 | it('should shift the given number of bytes off of the receive queue and return a string', function () { | |
78 | var bef_len = sock.rQlen(); | |
79 | var bef_rQi = sock.get_rQi(); | |
80 | var shifted = sock.rQshiftStr(3); | |
81 | expect(shifted).to.be.a('string'); | |
38781d93 | 82 | expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3)))); |
2cccf753 SR |
83 | expect(sock.rQlen()).to.equal(bef_len - 3); |
84 | }); | |
85 | ||
86 | it('should shift the entire rest of the queue off if no length is given', function () { | |
87 | sock.rQshiftStr(); | |
88 | expect(sock.rQlen()).to.equal(0); | |
89 | }); | |
90 | }); | |
91 | ||
92 | describe('rQshiftBytes', function () { | |
93 | it('should shift the given number of bytes of the receive queue and return an array', function () { | |
94 | var bef_len = sock.rQlen(); | |
95 | var bef_rQi = sock.get_rQi(); | |
96 | var shifted = sock.rQshiftBytes(3); | |
38781d93 SR |
97 | expect(shifted).to.be.an.instanceof(Uint8Array); |
98 | expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, bef_rQi, 3)); | |
2cccf753 SR |
99 | expect(sock.rQlen()).to.equal(bef_len - 3); |
100 | }); | |
101 | ||
102 | it('should shift the entire rest of the queue off if no length is given', function () { | |
103 | sock.rQshiftBytes(); | |
104 | expect(sock.rQlen()).to.equal(0); | |
105 | }); | |
106 | }); | |
107 | ||
108 | describe('rQslice', function () { | |
109 | beforeEach(function () { | |
110 | sock.set_rQi(0); | |
111 | }); | |
112 | ||
113 | it('should not modify the receive queue', function () { | |
114 | var bef_len = sock.rQlen(); | |
115 | sock.rQslice(0, 2); | |
116 | expect(sock.rQlen()).to.equal(bef_len); | |
117 | }); | |
118 | ||
119 | it('should return an array containing the given slice of the receive queue', function () { | |
120 | var sl = sock.rQslice(0, 2); | |
38781d93 SR |
121 | expect(sl).to.be.an.instanceof(Uint8Array); |
122 | expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2)); | |
2cccf753 SR |
123 | }); |
124 | ||
125 | it('should use the rest of the receive queue if no end is given', function () { | |
126 | var sl = sock.rQslice(1); | |
127 | expect(sl).to.have.length(RQ_TEMPLATE.length - 1); | |
38781d93 | 128 | expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1)); |
2cccf753 SR |
129 | }); |
130 | ||
131 | it('should take the current rQi in to account', function () { | |
132 | sock.set_rQi(1); | |
38781d93 | 133 | expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2)); |
2cccf753 SR |
134 | }); |
135 | }); | |
136 | ||
137 | describe('rQwait', function () { | |
138 | beforeEach(function () { | |
139 | sock.set_rQi(0); | |
140 | }); | |
141 | ||
142 | it('should return true if there are not enough bytes in the receive queue', function () { | |
143 | expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true; | |
144 | }); | |
145 | ||
146 | it('should return false if there are enough bytes in the receive queue', function () { | |
147 | expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false; | |
148 | }); | |
149 | ||
150 | it('should return true and reduce rQi by "goback" if there are not enough bytes', function () { | |
151 | sock.set_rQi(5); | |
152 | expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true; | |
153 | expect(sock.get_rQi()).to.equal(1); | |
154 | }); | |
155 | ||
156 | it('should raise an error if we try to go back more than possible', function () { | |
157 | sock.set_rQi(5); | |
158 | expect(function () { sock.rQwait('hi', RQ_TEMPLATE.length, 6); }).to.throw(Error); | |
159 | }); | |
160 | ||
161 | it('should not reduce rQi if there are enough bytes', function () { | |
162 | sock.set_rQi(5); | |
163 | sock.rQwait('hi', 1, 6); | |
164 | expect(sock.get_rQi()).to.equal(5); | |
165 | }); | |
166 | }); | |
167 | ||
168 | describe('flush', function () { | |
169 | beforeEach(function () { | |
170 | sock._websocket = { | |
171 | send: sinon.spy() | |
172 | }; | |
173 | }); | |
174 | ||
c4482d2d | 175 | it('should actually send on the websocket', function () { |
2cccf753 | 176 | sock._websocket.bufferedAmount = 8; |
cf0623ff | 177 | sock._websocket.readyState = WebSocket.OPEN |
9ff86fb7 SR |
178 | sock._sQ = new Uint8Array([1, 2, 3]); |
179 | sock._sQlen = 3; | |
2cccf753 SR |
180 | var encoded = sock._encode_message(); |
181 | ||
182 | sock.flush(); | |
183 | expect(sock._websocket.send).to.have.been.calledOnce; | |
184 | expect(sock._websocket.send).to.have.been.calledWith(encoded); | |
185 | }); | |
186 | ||
2cccf753 | 187 | it('should not call send if we do not have anything queued up', function () { |
9ff86fb7 | 188 | sock._sQlen = 0; |
2cccf753 SR |
189 | sock._websocket.bufferedAmount = 8; |
190 | ||
191 | sock.flush(); | |
192 | ||
193 | expect(sock._websocket.send).not.to.have.been.called; | |
194 | }); | |
2cccf753 SR |
195 | }); |
196 | ||
197 | describe('send', function () { | |
198 | beforeEach(function () { | |
199 | sock.flush = sinon.spy(); | |
200 | }); | |
201 | ||
202 | it('should add to the send queue', function () { | |
203 | sock.send([1, 2, 3]); | |
204 | var sq = sock.get_sQ(); | |
9ff86fb7 | 205 | expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3])); |
2cccf753 SR |
206 | }); |
207 | ||
208 | it('should call flush', function () { | |
209 | sock.send([1, 2, 3]); | |
210 | expect(sock.flush).to.have.been.calledOnce; | |
211 | }); | |
212 | }); | |
213 | ||
214 | describe('send_string', function () { | |
215 | beforeEach(function () { | |
216 | sock.send = sinon.spy(); | |
217 | }); | |
218 | ||
219 | it('should call send after converting the string to an array', function () { | |
220 | sock.send_string("\x01\x02\x03"); | |
221 | expect(sock.send).to.have.been.calledWith([1, 2, 3]); | |
222 | }); | |
223 | }); | |
224 | }); | |
225 | ||
226 | describe('lifecycle methods', function () { | |
227 | var old_WS; | |
228 | before(function () { | |
229 | old_WS = WebSocket; | |
230 | }); | |
231 | ||
232 | var sock; | |
233 | beforeEach(function () { | |
234 | sock = new Websock(); | |
235 | WebSocket = sinon.spy(); | |
236 | WebSocket.OPEN = old_WS.OPEN; | |
237 | WebSocket.CONNECTING = old_WS.CONNECTING; | |
238 | WebSocket.CLOSING = old_WS.CLOSING; | |
239 | WebSocket.CLOSED = old_WS.CLOSED; | |
38781d93 SR |
240 | |
241 | WebSocket.prototype.binaryType = 'arraybuffer'; | |
2cccf753 SR |
242 | }); |
243 | ||
244 | describe('opening', function () { | |
245 | it('should pick the correct protocols if none are given' , function () { | |
246 | ||
247 | }); | |
248 | ||
249 | it('should open the actual websocket', function () { | |
38781d93 SR |
250 | sock.open('ws://localhost:8675', 'binary'); |
251 | expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary'); | |
2cccf753 SR |
252 | }); |
253 | ||
2cccf753 SR |
254 | // it('should initialize the event handlers')? |
255 | }); | |
256 | ||
257 | describe('closing', function () { | |
258 | beforeEach(function () { | |
259 | sock.open('ws://'); | |
260 | sock._websocket.close = sinon.spy(); | |
261 | }); | |
262 | ||
263 | it('should close the actual websocket if it is open', function () { | |
264 | sock._websocket.readyState = WebSocket.OPEN; | |
265 | sock.close(); | |
266 | expect(sock._websocket.close).to.have.been.calledOnce; | |
267 | }); | |
268 | ||
269 | it('should close the actual websocket if it is connecting', function () { | |
270 | sock._websocket.readyState = WebSocket.CONNECTING; | |
271 | sock.close(); | |
272 | expect(sock._websocket.close).to.have.been.calledOnce; | |
273 | }); | |
274 | ||
275 | it('should not try to close the actual websocket if closing', function () { | |
276 | sock._websocket.readyState = WebSocket.CLOSING; | |
277 | sock.close(); | |
278 | expect(sock._websocket.close).not.to.have.been.called; | |
279 | }); | |
280 | ||
281 | it('should not try to close the actual websocket if closed', function () { | |
282 | sock._websocket.readyState = WebSocket.CLOSED; | |
283 | sock.close(); | |
284 | expect(sock._websocket.close).not.to.have.been.called; | |
285 | }); | |
286 | ||
287 | it('should reset onmessage to not call _recv_message', function () { | |
288 | sinon.spy(sock, '_recv_message'); | |
289 | sock.close(); | |
290 | sock._websocket.onmessage(null); | |
291 | try { | |
292 | expect(sock._recv_message).not.to.have.been.called; | |
293 | } finally { | |
294 | sock._recv_message.restore(); | |
295 | } | |
296 | }); | |
297 | }); | |
298 | ||
299 | describe('event handlers', function () { | |
300 | beforeEach(function () { | |
301 | sock._recv_message = sinon.spy(); | |
302 | sock.on('open', sinon.spy()); | |
303 | sock.on('close', sinon.spy()); | |
304 | sock.on('error', sinon.spy()); | |
305 | sock.open('ws://'); | |
306 | }); | |
307 | ||
308 | it('should call _recv_message on a message', function () { | |
309 | sock._websocket.onmessage(null); | |
310 | expect(sock._recv_message).to.have.been.calledOnce; | |
311 | }); | |
312 | ||
2cccf753 SR |
313 | it('should call the open event handler on opening', function () { |
314 | sock._websocket.onopen(); | |
315 | expect(sock._eventHandlers.open).to.have.been.calledOnce; | |
316 | }); | |
317 | ||
318 | it('should call the close event handler on closing', function () { | |
319 | sock._websocket.onclose(); | |
320 | expect(sock._eventHandlers.close).to.have.been.calledOnce; | |
321 | }); | |
322 | ||
323 | it('should call the error event handler on error', function () { | |
324 | sock._websocket.onerror(); | |
325 | expect(sock._eventHandlers.error).to.have.been.calledOnce; | |
326 | }); | |
327 | }); | |
328 | ||
329 | after(function () { | |
330 | WebSocket = old_WS; | |
331 | }); | |
332 | }); | |
333 | ||
334 | describe('WebSocket Receiving', function () { | |
335 | var sock; | |
336 | beforeEach(function () { | |
337 | sock = new Websock(); | |
38781d93 | 338 | sock._allocate_buffers(); |
2cccf753 SR |
339 | }); |
340 | ||
341 | it('should support adding binary Uint8Array data to the receive queue', function () { | |
342 | var msg = { data: new Uint8Array([1, 2, 3]) }; | |
343 | sock._mode = 'binary'; | |
344 | sock._recv_message(msg); | |
345 | expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03'); | |
346 | }); | |
347 | ||
348 | it('should call the message event handler if present', function () { | |
349 | sock._eventHandlers.message = sinon.spy(); | |
38781d93 SR |
350 | var msg = { data: new Uint8Array([1, 2, 3]).buffer }; |
351 | sock._mode = 'binary'; | |
2cccf753 SR |
352 | sock._recv_message(msg); |
353 | expect(sock._eventHandlers.message).to.have.been.calledOnce; | |
354 | }); | |
355 | ||
356 | it('should not call the message event handler if there is nothing in the receive queue', function () { | |
357 | sock._eventHandlers.message = sinon.spy(); | |
38781d93 SR |
358 | var msg = { data: new Uint8Array([]).buffer }; |
359 | sock._mode = 'binary'; | |
2cccf753 SR |
360 | sock._recv_message(msg); |
361 | expect(sock._eventHandlers.message).not.to.have.been.called; | |
362 | }); | |
363 | ||
364 | it('should compact the receive queue', function () { | |
365 | // NB(sross): while this is an internal implementation detail, it's important to | |
366 | // test, otherwise the receive queue could become very large very quickly | |
38781d93 SR |
367 | sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]); |
368 | sock._rQlen = 6; | |
2cccf753 SR |
369 | sock.set_rQi(6); |
370 | sock._rQmax = 3; | |
38781d93 SR |
371 | var msg = { data: new Uint8Array([1, 2, 3]).buffer }; |
372 | sock._mode = 'binary'; | |
2cccf753 | 373 | sock._recv_message(msg); |
38781d93 | 374 | expect(sock._rQlen).to.equal(3); |
2cccf753 SR |
375 | expect(sock.get_rQi()).to.equal(0); |
376 | }); | |
377 | ||
40037b6a SR |
378 | it('should automatically resize the receive queue if the incoming message is too large', function () { |
379 | sock._rQ = new Uint8Array(20); | |
380 | sock._rQlen = 0; | |
381 | sock.set_rQi(0); | |
382 | sock._rQbufferSize = 20; | |
383 | sock._rQmax = 2; | |
384 | var msg = { data: new Uint8Array(30).buffer }; | |
385 | sock._mode = 'binary'; | |
386 | sock._recv_message(msg); | |
387 | expect(sock._rQlen).to.equal(30); | |
388 | expect(sock.get_rQi()).to.equal(0); | |
389 | expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen | |
390 | }); | |
2cccf753 SR |
391 | }); |
392 | ||
393 | describe('Data encoding', function () { | |
394 | before(function () { FakeWebSocket.replace(); }); | |
395 | after(function () { FakeWebSocket.restore(); }); | |
396 | ||
397 | describe('as binary data', function () { | |
398 | var sock; | |
399 | beforeEach(function () { | |
400 | sock = new Websock(); | |
401 | sock.open('ws://', 'binary'); | |
402 | sock._websocket._open(); | |
403 | }); | |
404 | ||
9ff86fb7 SR |
405 | it('should only send the send queue up to the send queue length', function () { |
406 | sock._sQ = new Uint8Array([1, 2, 3, 4, 5]); | |
407 | sock._sQlen = 3; | |
408 | var res = sock._encode_message(); | |
409 | expect(res).to.array.equal(new Uint8Array([1, 2, 3])); | |
2cccf753 SR |
410 | }); |
411 | ||
412 | it('should properly pass the encoded data off to the actual WebSocket', function () { | |
413 | sock.send([1, 2, 3]); | |
9ff86fb7 | 414 | expect(sock._websocket._get_sent_data()).to.array.equal(new Uint8Array([1, 2, 3])); |
2cccf753 SR |
415 | }); |
416 | }); | |
2cccf753 SR |
417 | }); |
418 | }); |