]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.websock.js
1 var assert
= chai
.assert
;
2 var expect
= chai
.expect
;
4 import Websock
from '../core/websock.js';
5 import FakeWebSocket
from './fake.websocket.js';
7 import sinon
from '../vendor/sinon.js';
9 describe('Websock', function() {
12 describe('Queue methods', function () {
14 var RQ_TEMPLATE
= new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
16 beforeEach(function () {
19 sock
._allocate_buffers();
20 sock
._rQ
.set(RQ_TEMPLATE
);
21 sock
._rQlen
= RQ_TEMPLATE
.length
;
23 describe('rQlen', function () {
24 it('should return the length of the receive queue', function () {
27 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
);
30 it("should return the proper length if we read some from the receive queue", function () {
33 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
- 1);
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
);
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);
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);
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) +
71 expect(sock
.rQshift32()).to
.equal(expected
);
72 expect(sock
.rQlen()).to
.equal(bef_len
- 4);
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');
82 expect(shifted
).to
.equal(String
.fromCharCode
.apply(null, Array
.prototype.slice
.call(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3))));
83 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
86 it('should shift the entire rest of the queue off if no length is given', function () {
88 expect(sock
.rQlen()).to
.equal(0);
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);
97 expect(shifted
).to
.be
.an
.instanceof(Uint8Array
);
98 expect(shifted
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3));
99 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
102 it('should shift the entire rest of the queue off if no length is given', function () {
104 expect(sock
.rQlen()).to
.equal(0);
108 describe('rQslice', function () {
109 beforeEach(function () {
113 it('should not modify the receive queue', function () {
114 var bef_len
= sock
.rQlen();
116 expect(sock
.rQlen()).to
.equal(bef_len
);
119 it('should return an array containing the given slice of the receive queue', function () {
120 var sl
= sock
.rQslice(0, 2);
121 expect(sl
).to
.be
.an
.instanceof(Uint8Array
);
122 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 0, 2));
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);
128 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1));
131 it('should take the current rQi in to account', function () {
133 expect(sock
.rQslice(0, 2)).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1, 2));
137 describe('rQwait', function () {
138 beforeEach(function () {
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;
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;
150 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
152 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
, 4)).to
.be
.true;
153 expect(sock
.get_rQi()).to
.equal(1);
156 it('should raise an error if we try to go back more than possible', function () {
158 expect(function () { sock
.rQwait('hi', RQ_TEMPLATE
.length
, 6); }).to
.throw(Error
);
161 it('should not reduce rQi if there are enough bytes', function () {
163 sock
.rQwait('hi', 1, 6);
164 expect(sock
.get_rQi()).to
.equal(5);
168 describe('flush', function () {
169 beforeEach(function () {
175 it('should actually send on the websocket', function () {
176 sock
._websocket
.bufferedAmount
= 8;
177 sock
._websocket
.readyState
= WebSocket
.OPEN
178 sock
._sQ
= new Uint8Array([1, 2, 3]);
180 var encoded
= sock
._encode_message();
183 expect(sock
._websocket
.send
).to
.have
.been
.calledOnce
;
184 expect(sock
._websocket
.send
).to
.have
.been
.calledWith(encoded
);
187 it('should not call send if we do not have anything queued up', function () {
189 sock
._websocket
.bufferedAmount
= 8;
193 expect(sock
._websocket
.send
).not
.to
.have
.been
.called
;
197 describe('send', function () {
198 beforeEach(function () {
199 sock
.flush
= sinon
.spy();
202 it('should add to the send queue', function () {
203 sock
.send([1, 2, 3]);
204 var sq
= sock
.get_sQ();
205 expect(new Uint8Array(sq
.buffer
, sock
._sQlen
- 3, 3)).to
.array
.equal(new Uint8Array([1, 2, 3]));
208 it('should call flush', function () {
209 sock
.send([1, 2, 3]);
210 expect(sock
.flush
).to
.have
.been
.calledOnce
;
214 describe('send_string', function () {
215 beforeEach(function () {
216 sock
.send
= sinon
.spy();
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]);
226 describe('lifecycle methods', function () {
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
;
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
;
334 describe('WebSocket Receiving', function () {
336 beforeEach(function () {
337 sock
= new Websock();
338 sock
._allocate_buffers();
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');
348 it('should call the message event handler if present', function () {
349 sock
._eventHandlers
.message
= sinon
.spy();
350 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
351 sock
._mode
= 'binary';
352 sock
._recv_message(msg
);
353 expect(sock
._eventHandlers
.message
).to
.have
.been
.calledOnce
;
356 it('should not call the message event handler if there is nothing in the receive queue', function () {
357 sock
._eventHandlers
.message
= sinon
.spy();
358 var msg
= { data
: new Uint8Array([]).buffer
};
359 sock
._mode
= 'binary';
360 sock
._recv_message(msg
);
361 expect(sock
._eventHandlers
.message
).not
.to
.have
.been
.called
;
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
367 sock
._rQ
= new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
371 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
372 sock
._mode
= 'binary';
373 sock
._recv_message(msg
);
374 expect(sock
._rQlen
).to
.equal(3);
375 expect(sock
.get_rQi()).to
.equal(0);
378 it('should automatically resize the receive queue if the incoming message is too large', function () {
379 sock
._rQ
= new Uint8Array(20);
382 sock
._rQbufferSize
= 20;
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
393 describe('Data encoding', function () {
394 before(function () { FakeWebSocket
.replace(); });
395 after(function () { FakeWebSocket
.restore(); });
397 describe('as binary data', function () {
399 beforeEach(function () {
400 sock
= new Websock();
401 sock
.open('ws://', 'binary');
402 sock
._websocket
._open();
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]);
408 var res
= sock
._encode_message();
409 expect(res
).to
.array
.equal(new Uint8Array([1, 2, 3]));
412 it('should properly pass the encoded data off to the actual WebSocket', function () {
413 sock
.send([1, 2, 3]);
414 expect(sock
._websocket
._get_sent_data()).to
.array
.equal(new Uint8Array([1, 2, 3]));