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