]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.websock.js
1 var expect
= chai
.expect
;
3 import Websock
from '../core/websock.js';
4 import FakeWebSocket
from './fake.websocket.js';
6 import sinon
from '../vendor/sinon.js';
8 describe('Websock', function() {
11 describe('Queue methods', function () {
13 var RQ_TEMPLATE
= new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
15 beforeEach(function () {
18 sock
._allocate_buffers();
19 sock
._rQ
.set(RQ_TEMPLATE
);
20 sock
._rQlen
= RQ_TEMPLATE
.length
;
22 describe('rQlen', function () {
23 it('should return the length of the receive queue', function () {
26 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
);
29 it("should return the proper length if we read some from the receive queue", function () {
32 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
- 1);
36 describe('rQpeek8', function () {
37 it('should peek at the next byte without poping it off the queue', function () {
38 var bef_len
= sock
.rQlen();
39 var peek
= sock
.rQpeek8();
40 expect(sock
.rQpeek8()).to
.equal(peek
);
41 expect(sock
.rQlen()).to
.equal(bef_len
);
45 describe('rQshift8', function () {
46 it('should pop a single byte from the receive queue', function () {
47 var peek
= sock
.rQpeek8();
48 var bef_len
= sock
.rQlen();
49 expect(sock
.rQshift8()).to
.equal(peek
);
50 expect(sock
.rQlen()).to
.equal(bef_len
- 1);
54 describe('rQshift16', function () {
55 it('should pop two bytes from the receive queue and return a single number', function () {
56 var bef_len
= sock
.rQlen();
57 var expected
= (RQ_TEMPLATE
[0] << 8) + RQ_TEMPLATE
[1];
58 expect(sock
.rQshift16()).to
.equal(expected
);
59 expect(sock
.rQlen()).to
.equal(bef_len
- 2);
63 describe('rQshift32', function () {
64 it('should pop four bytes from the receive queue and return a single number', function () {
65 var bef_len
= sock
.rQlen();
66 var expected
= (RQ_TEMPLATE
[0] << 24) +
67 (RQ_TEMPLATE
[1] << 16) +
68 (RQ_TEMPLATE
[2] << 8) +
70 expect(sock
.rQshift32()).to
.equal(expected
);
71 expect(sock
.rQlen()).to
.equal(bef_len
- 4);
75 describe('rQshiftStr', function () {
76 it('should shift the given number of bytes off of the receive queue and return a string', function () {
77 var bef_len
= sock
.rQlen();
78 var bef_rQi
= sock
.get_rQi();
79 var shifted
= sock
.rQshiftStr(3);
80 expect(shifted
).to
.be
.a('string');
81 expect(shifted
).to
.equal(String
.fromCharCode
.apply(null, Array
.prototype.slice
.call(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3))));
82 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
85 it('should shift the entire rest of the queue off if no length is given', function () {
87 expect(sock
.rQlen()).to
.equal(0);
91 describe('rQshiftBytes', function () {
92 it('should shift the given number of bytes of the receive queue and return an array', function () {
93 var bef_len
= sock
.rQlen();
94 var bef_rQi
= sock
.get_rQi();
95 var shifted
= sock
.rQshiftBytes(3);
96 expect(shifted
).to
.be
.an
.instanceof(Uint8Array
);
97 expect(shifted
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3));
98 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
101 it('should shift the entire rest of the queue off if no length is given', function () {
103 expect(sock
.rQlen()).to
.equal(0);
107 describe('rQslice', function () {
108 beforeEach(function () {
112 it('should not modify the receive queue', function () {
113 var bef_len
= sock
.rQlen();
115 expect(sock
.rQlen()).to
.equal(bef_len
);
118 it('should return an array containing the given slice of the receive queue', function () {
119 var sl
= sock
.rQslice(0, 2);
120 expect(sl
).to
.be
.an
.instanceof(Uint8Array
);
121 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 0, 2));
124 it('should use the rest of the receive queue if no end is given', function () {
125 var sl
= sock
.rQslice(1);
126 expect(sl
).to
.have
.length(RQ_TEMPLATE
.length
- 1);
127 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1));
130 it('should take the current rQi in to account', function () {
132 expect(sock
.rQslice(0, 2)).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1, 2));
136 describe('rQwait', function () {
137 beforeEach(function () {
141 it('should return true if there are not enough bytes in the receive queue', function () {
142 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
+ 1)).to
.be
.true;
145 it('should return false if there are enough bytes in the receive queue', function () {
146 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
)).to
.be
.false;
149 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
151 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
, 4)).to
.be
.true;
152 expect(sock
.get_rQi()).to
.equal(1);
155 it('should raise an error if we try to go back more than possible', function () {
157 expect(function () { sock
.rQwait('hi', RQ_TEMPLATE
.length
, 6); }).to
.throw(Error
);
160 it('should not reduce rQi if there are enough bytes', function () {
162 sock
.rQwait('hi', 1, 6);
163 expect(sock
.get_rQi()).to
.equal(5);
167 describe('flush', function () {
168 beforeEach(function () {
174 it('should actually send on the websocket', function () {
175 sock
._websocket
.bufferedAmount
= 8;
176 sock
._websocket
.readyState
= WebSocket
.OPEN
177 sock
._sQ
= new Uint8Array([1, 2, 3]);
179 var encoded
= sock
._encode_message();
182 expect(sock
._websocket
.send
).to
.have
.been
.calledOnce
;
183 expect(sock
._websocket
.send
).to
.have
.been
.calledWith(encoded
);
186 it('should not call send if we do not have anything queued up', function () {
188 sock
._websocket
.bufferedAmount
= 8;
192 expect(sock
._websocket
.send
).not
.to
.have
.been
.called
;
196 describe('send', function () {
197 beforeEach(function () {
198 sock
.flush
= sinon
.spy();
201 it('should add to the send queue', function () {
202 sock
.send([1, 2, 3]);
203 var sq
= sock
.get_sQ();
204 expect(new Uint8Array(sq
.buffer
, sock
._sQlen
- 3, 3)).to
.array
.equal(new Uint8Array([1, 2, 3]));
207 it('should call flush', function () {
208 sock
.send([1, 2, 3]);
209 expect(sock
.flush
).to
.have
.been
.calledOnce
;
213 describe('send_string', function () {
214 beforeEach(function () {
215 sock
.send
= sinon
.spy();
218 it('should call send after converting the string to an array', function () {
219 sock
.send_string("\x01\x02\x03");
220 expect(sock
.send
).to
.have
.been
.calledWith([1, 2, 3]);
225 describe('lifecycle methods', function () {
232 beforeEach(function () {
233 sock
= new Websock();
234 // eslint-disable-next-line no-global-assign
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
;
241 WebSocket
.prototype.binaryType
= 'arraybuffer';
244 describe('opening', function () {
245 it('should pick the correct protocols if none are given' , function () {
249 it('should open the actual websocket', function () {
250 sock
.open('ws://localhost:8675', 'binary');
251 expect(WebSocket
).to
.have
.been
.calledWith('ws://localhost:8675', 'binary');
254 // it('should initialize the event handlers')?
257 describe('closing', function () {
258 beforeEach(function () {
260 sock
._websocket
.close
= sinon
.spy();
263 it('should close the actual websocket if it is open', function () {
264 sock
._websocket
.readyState
= WebSocket
.OPEN
;
266 expect(sock
._websocket
.close
).to
.have
.been
.calledOnce
;
269 it('should close the actual websocket if it is connecting', function () {
270 sock
._websocket
.readyState
= WebSocket
.CONNECTING
;
272 expect(sock
._websocket
.close
).to
.have
.been
.calledOnce
;
275 it('should not try to close the actual websocket if closing', function () {
276 sock
._websocket
.readyState
= WebSocket
.CLOSING
;
278 expect(sock
._websocket
.close
).not
.to
.have
.been
.called
;
281 it('should not try to close the actual websocket if closed', function () {
282 sock
._websocket
.readyState
= WebSocket
.CLOSED
;
284 expect(sock
._websocket
.close
).not
.to
.have
.been
.called
;
287 it('should reset onmessage to not call _recv_message', function () {
288 sinon
.spy(sock
, '_recv_message');
290 sock
._websocket
.onmessage(null);
292 expect(sock
._recv_message
).not
.to
.have
.been
.called
;
294 sock
._recv_message
.restore();
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());
308 it('should call _recv_message on a message', function () {
309 sock
._websocket
.onmessage(null);
310 expect(sock
._recv_message
).to
.have
.been
.calledOnce
;
313 it('should call the open event handler on opening', function () {
314 sock
._websocket
.onopen();
315 expect(sock
._eventHandlers
.open
).to
.have
.been
.calledOnce
;
318 it('should call the close event handler on closing', function () {
319 sock
._websocket
.onclose();
320 expect(sock
._eventHandlers
.close
).to
.have
.been
.calledOnce
;
323 it('should call the error event handler on error', function () {
324 sock
._websocket
.onerror();
325 expect(sock
._eventHandlers
.error
).to
.have
.been
.calledOnce
;
330 // eslint-disable-next-line no-global-assign
335 describe('WebSocket Receiving', function () {
337 beforeEach(function () {
338 sock
= new Websock();
339 sock
._allocate_buffers();
342 it('should support adding binary Uint8Array data to the receive queue', function () {
343 var msg
= { data
: new Uint8Array([1, 2, 3]) };
344 sock
._mode
= 'binary';
345 sock
._recv_message(msg
);
346 expect(sock
.rQshiftStr(3)).to
.equal('\x01\x02\x03');
349 it('should call the message event handler if present', function () {
350 sock
._eventHandlers
.message
= sinon
.spy();
351 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
352 sock
._mode
= 'binary';
353 sock
._recv_message(msg
);
354 expect(sock
._eventHandlers
.message
).to
.have
.been
.calledOnce
;
357 it('should not call the message event handler if there is nothing in the receive queue', function () {
358 sock
._eventHandlers
.message
= sinon
.spy();
359 var msg
= { data
: new Uint8Array([]).buffer
};
360 sock
._mode
= 'binary';
361 sock
._recv_message(msg
);
362 expect(sock
._eventHandlers
.message
).not
.to
.have
.been
.called
;
365 it('should compact the receive queue', function () {
366 // NB(sross): while this is an internal implementation detail, it's important to
367 // test, otherwise the receive queue could become very large very quickly
368 sock
._rQ
= new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
372 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
373 sock
._mode
= 'binary';
374 sock
._recv_message(msg
);
375 expect(sock
._rQlen
).to
.equal(3);
376 expect(sock
.get_rQi()).to
.equal(0);
379 it('should automatically resize the receive queue if the incoming message is too large', function () {
380 sock
._rQ
= new Uint8Array(20);
383 sock
._rQbufferSize
= 20;
385 var msg
= { data
: new Uint8Array(30).buffer
};
386 sock
._mode
= 'binary';
387 sock
._recv_message(msg
);
388 expect(sock
._rQlen
).to
.equal(30);
389 expect(sock
.get_rQi()).to
.equal(0);
390 expect(sock
._rQ
.length
).to
.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
394 describe('Data encoding', function () {
395 before(function () { FakeWebSocket
.replace(); });
396 after(function () { FakeWebSocket
.restore(); });
398 describe('as binary data', function () {
400 beforeEach(function () {
401 sock
= new Websock();
402 sock
.open('ws://', 'binary');
403 sock
._websocket
._open();
406 it('should only send the send queue up to the send queue length', function () {
407 sock
._sQ
= new Uint8Array([1, 2, 3, 4, 5]);
409 var res
= sock
._encode_message();
410 expect(res
).to
.array
.equal(new Uint8Array([1, 2, 3]));
413 it('should properly pass the encoded data off to the actual WebSocket', function () {
414 sock
.send([1, 2, 3]);
415 expect(sock
._websocket
._get_sent_data()).to
.array
.equal(new Uint8Array([1, 2, 3]));