]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.websock.js
1 // requires local modules: websock, util
2 // requires test modules: fake.websocket, assertions
3 /* jshint expr: true */
4 var assert
= chai
.assert
;
5 var expect
= chai
.expect
;
7 describe('Websock', function() {
10 describe('Queue methods', function () {
12 var RQ_TEMPLATE
= new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
14 beforeEach(function () {
17 sock
._allocate_buffers();
18 sock
._rQ
.set(RQ_TEMPLATE
);
19 sock
._rQlen
= RQ_TEMPLATE
.length
;
21 describe('rQlen', function () {
22 it('should return the length of the receive queue', function () {
25 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
);
28 it("should return the proper length if we read some from the receive queue", function () {
31 expect(sock
.rQlen()).to
.equal(RQ_TEMPLATE
.length
- 1);
35 describe('rQpeek8', function () {
36 it('should peek at the next byte without poping it off the queue', function () {
37 var bef_len
= sock
.rQlen();
38 var peek
= sock
.rQpeek8();
39 expect(sock
.rQpeek8()).to
.equal(peek
);
40 expect(sock
.rQlen()).to
.equal(bef_len
);
44 describe('rQshift8', function () {
45 it('should pop a single byte from the receive queue', function () {
46 var peek
= sock
.rQpeek8();
47 var bef_len
= sock
.rQlen();
48 expect(sock
.rQshift8()).to
.equal(peek
);
49 expect(sock
.rQlen()).to
.equal(bef_len
- 1);
53 describe('rQshift16', function () {
54 it('should pop two bytes from the receive queue and return a single number', function () {
55 var bef_len
= sock
.rQlen();
56 var expected
= (RQ_TEMPLATE
[0] << 8) + RQ_TEMPLATE
[1];
57 expect(sock
.rQshift16()).to
.equal(expected
);
58 expect(sock
.rQlen()).to
.equal(bef_len
- 2);
62 describe('rQshift32', function () {
63 it('should pop four bytes from the receive queue and return a single number', function () {
64 var bef_len
= sock
.rQlen();
65 var expected
= (RQ_TEMPLATE
[0] << 24) +
66 (RQ_TEMPLATE
[1] << 16) +
67 (RQ_TEMPLATE
[2] << 8) +
69 expect(sock
.rQshift32()).to
.equal(expected
);
70 expect(sock
.rQlen()).to
.equal(bef_len
- 4);
74 describe('rQshiftStr', function () {
75 it('should shift the given number of bytes off of the receive queue and return a string', function () {
76 var bef_len
= sock
.rQlen();
77 var bef_rQi
= sock
.get_rQi();
78 var shifted
= sock
.rQshiftStr(3);
79 expect(shifted
).to
.be
.a('string');
80 expect(shifted
).to
.equal(String
.fromCharCode
.apply(null, Array
.prototype.slice
.call(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3))));
81 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
84 it('should shift the entire rest of the queue off if no length is given', function () {
86 expect(sock
.rQlen()).to
.equal(0);
90 describe('rQshiftBytes', function () {
91 it('should shift the given number of bytes of the receive queue and return an array', function () {
92 var bef_len
= sock
.rQlen();
93 var bef_rQi
= sock
.get_rQi();
94 var shifted
= sock
.rQshiftBytes(3);
95 expect(shifted
).to
.be
.an
.instanceof(Uint8Array
);
96 expect(shifted
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, bef_rQi
, 3));
97 expect(sock
.rQlen()).to
.equal(bef_len
- 3);
100 it('should shift the entire rest of the queue off if no length is given', function () {
102 expect(sock
.rQlen()).to
.equal(0);
106 describe('rQslice', function () {
107 beforeEach(function () {
111 it('should not modify the receive queue', function () {
112 var bef_len
= sock
.rQlen();
114 expect(sock
.rQlen()).to
.equal(bef_len
);
117 it('should return an array containing the given slice of the receive queue', function () {
118 var sl
= sock
.rQslice(0, 2);
119 expect(sl
).to
.be
.an
.instanceof(Uint8Array
);
120 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 0, 2));
123 it('should use the rest of the receive queue if no end is given', function () {
124 var sl
= sock
.rQslice(1);
125 expect(sl
).to
.have
.length(RQ_TEMPLATE
.length
- 1);
126 expect(sl
).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1));
129 it('should take the current rQi in to account', function () {
131 expect(sock
.rQslice(0, 2)).to
.array
.equal(new Uint8Array(RQ_TEMPLATE
.buffer
, 1, 2));
135 describe('rQwait', function () {
136 beforeEach(function () {
140 it('should return true if there are not enough bytes in the receive queue', function () {
141 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
+ 1)).to
.be
.true;
144 it('should return false if there are enough bytes in the receive queue', function () {
145 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
)).to
.be
.false;
148 it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
150 expect(sock
.rQwait('hi', RQ_TEMPLATE
.length
, 4)).to
.be
.true;
151 expect(sock
.get_rQi()).to
.equal(1);
154 it('should raise an error if we try to go back more than possible', function () {
156 expect(function () { sock
.rQwait('hi', RQ_TEMPLATE
.length
, 6); }).to
.throw(Error
);
159 it('should not reduce rQi if there are enough bytes', function () {
161 sock
.rQwait('hi', 1, 6);
162 expect(sock
.get_rQi()).to
.equal(5);
166 describe('flush', function () {
167 beforeEach(function () {
173 it('should actually send on the websocket if the websocket does not have too much buffered', function () {
174 sock
.maxBufferedAmount
= 10;
175 sock
._websocket
.bufferedAmount
= 8;
176 sock
._sQ
= new Uint8Array([1, 2, 3]);
178 var encoded
= sock
._encode_message();
181 expect(sock
._websocket
.send
).to
.have
.been
.calledOnce
;
182 expect(sock
._websocket
.send
).to
.have
.been
.calledWith(encoded
);
185 it('should return true if the websocket did not have too much buffered', function () {
186 sock
.maxBufferedAmount
= 10;
187 sock
._websocket
.bufferedAmount
= 8;
189 expect(sock
.flush()).to
.be
.true;
192 it('should not call send if we do not have anything queued up', function () {
194 sock
.maxBufferedAmount
= 10;
195 sock
._websocket
.bufferedAmount
= 8;
199 expect(sock
._websocket
.send
).not
.to
.have
.been
.called
;
202 it('should not send and return false if the websocket has too much buffered', function () {
203 sock
.maxBufferedAmount
= 10;
204 sock
._websocket
.bufferedAmount
= 12;
206 expect(sock
.flush()).to
.be
.false;
207 expect(sock
._websocket
.send
).to
.not
.have
.been
.called
;
211 describe('send', function () {
212 beforeEach(function () {
213 sock
.flush
= sinon
.spy();
216 it('should add to the send queue', function () {
217 sock
.send([1, 2, 3]);
218 var sq
= sock
.get_sQ();
219 expect(new Uint8Array(sq
.buffer
, sock
._sQlen
- 3, 3)).to
.array
.equal(new Uint8Array([1, 2, 3]));
222 it('should call flush', function () {
223 sock
.send([1, 2, 3]);
224 expect(sock
.flush
).to
.have
.been
.calledOnce
;
228 describe('send_string', function () {
229 beforeEach(function () {
230 sock
.send
= sinon
.spy();
233 it('should call send after converting the string to an array', function () {
234 sock
.send_string("\x01\x02\x03");
235 expect(sock
.send
).to
.have
.been
.calledWith([1, 2, 3]);
240 describe('lifecycle methods', function () {
247 beforeEach(function () {
248 sock
= new Websock();
249 WebSocket
= sinon
.spy();
250 WebSocket
.OPEN
= old_WS
.OPEN
;
251 WebSocket
.CONNECTING
= old_WS
.CONNECTING
;
252 WebSocket
.CLOSING
= old_WS
.CLOSING
;
253 WebSocket
.CLOSED
= old_WS
.CLOSED
;
255 WebSocket
.prototype.binaryType
= 'arraybuffer';
258 describe('opening', function () {
259 it('should pick the correct protocols if none are given' , function () {
263 it('should open the actual websocket', function () {
264 sock
.open('ws://localhost:8675', 'binary');
265 expect(WebSocket
).to
.have
.been
.calledWith('ws://localhost:8675', 'binary');
268 it('should fail if we specify a protocol besides binary', function () {
269 expect(function () { sock
.open('ws:///', 'base64'); }).to
.throw(Error
);
272 // it('should initialize the event handlers')?
275 describe('closing', function () {
276 beforeEach(function () {
278 sock
._websocket
.close
= sinon
.spy();
281 it('should close the actual websocket if it is open', function () {
282 sock
._websocket
.readyState
= WebSocket
.OPEN
;
284 expect(sock
._websocket
.close
).to
.have
.been
.calledOnce
;
287 it('should close the actual websocket if it is connecting', function () {
288 sock
._websocket
.readyState
= WebSocket
.CONNECTING
;
290 expect(sock
._websocket
.close
).to
.have
.been
.calledOnce
;
293 it('should not try to close the actual websocket if closing', function () {
294 sock
._websocket
.readyState
= WebSocket
.CLOSING
;
296 expect(sock
._websocket
.close
).not
.to
.have
.been
.called
;
299 it('should not try to close the actual websocket if closed', function () {
300 sock
._websocket
.readyState
= WebSocket
.CLOSED
;
302 expect(sock
._websocket
.close
).not
.to
.have
.been
.called
;
305 it('should reset onmessage to not call _recv_message', function () {
306 sinon
.spy(sock
, '_recv_message');
308 sock
._websocket
.onmessage(null);
310 expect(sock
._recv_message
).not
.to
.have
.been
.called
;
312 sock
._recv_message
.restore();
317 describe('event handlers', function () {
318 beforeEach(function () {
319 sock
._recv_message
= sinon
.spy();
320 sock
.on('open', sinon
.spy());
321 sock
.on('close', sinon
.spy());
322 sock
.on('error', sinon
.spy());
326 it('should call _recv_message on a message', function () {
327 sock
._websocket
.onmessage(null);
328 expect(sock
._recv_message
).to
.have
.been
.calledOnce
;
331 it('should fail if a protocol besides binary is requested', function () {
332 sock
._websocket
.protocol
= 'base64';
333 expect(sock
._websocket
.onopen
).to
.throw(Error
);
336 it('should assume binary if no protocol was available on opening', function () {
337 sock
._websocket
.protocol
= null;
338 sock
._websocket
.onopen();
339 expect(sock
._mode
).to
.equal('binary');
342 it('should call the open event handler on opening', function () {
343 sock
._websocket
.onopen();
344 expect(sock
._eventHandlers
.open
).to
.have
.been
.calledOnce
;
347 it('should call the close event handler on closing', function () {
348 sock
._websocket
.onclose();
349 expect(sock
._eventHandlers
.close
).to
.have
.been
.calledOnce
;
352 it('should call the error event handler on error', function () {
353 sock
._websocket
.onerror();
354 expect(sock
._eventHandlers
.error
).to
.have
.been
.calledOnce
;
363 describe('WebSocket Receiving', function () {
365 beforeEach(function () {
366 sock
= new Websock();
367 sock
._allocate_buffers();
370 it('should support adding binary Uint8Array data to the receive queue', function () {
371 var msg
= { data
: new Uint8Array([1, 2, 3]) };
372 sock
._mode
= 'binary';
373 sock
._recv_message(msg
);
374 expect(sock
.rQshiftStr(3)).to
.equal('\x01\x02\x03');
377 it('should call the message event handler if present', function () {
378 sock
._eventHandlers
.message
= sinon
.spy();
379 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
380 sock
._mode
= 'binary';
381 sock
._recv_message(msg
);
382 expect(sock
._eventHandlers
.message
).to
.have
.been
.calledOnce
;
385 it('should not call the message event handler if there is nothing in the receive queue', function () {
386 sock
._eventHandlers
.message
= sinon
.spy();
387 var msg
= { data
: new Uint8Array([]).buffer
};
388 sock
._mode
= 'binary';
389 sock
._recv_message(msg
);
390 expect(sock
._eventHandlers
.message
).not
.to
.have
.been
.called
;
393 it('should compact the receive queue', function () {
394 // NB(sross): while this is an internal implementation detail, it's important to
395 // test, otherwise the receive queue could become very large very quickly
396 sock
._rQ
= new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
400 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
401 sock
._mode
= 'binary';
402 sock
._recv_message(msg
);
403 expect(sock
._rQlen
).to
.equal(3);
404 expect(sock
.get_rQi()).to
.equal(0);
407 it('should automatically resize the receive queue if the incoming message is too large', function () {
408 sock
._rQ
= new Uint8Array(20);
411 sock
._rQbufferSize
= 20;
413 var msg
= { data
: new Uint8Array(30).buffer
};
414 sock
._mode
= 'binary';
415 sock
._recv_message(msg
);
416 expect(sock
._rQlen
).to
.equal(30);
417 expect(sock
.get_rQi()).to
.equal(0);
418 expect(sock
._rQ
.length
).to
.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
421 it('should call the error event handler on an exception', function () {
422 sock
._eventHandlers
.error
= sinon
.spy();
423 sock
._eventHandlers
.message
= sinon
.stub().throws();
424 var msg
= { data
: new Uint8Array([1, 2, 3]).buffer
};
425 sock
._mode
= 'binary';
426 sock
._recv_message(msg
);
427 expect(sock
._eventHandlers
.error
).to
.have
.been
.calledOnce
;
431 describe('Data encoding', function () {
432 before(function () { FakeWebSocket
.replace(); });
433 after(function () { FakeWebSocket
.restore(); });
435 describe('as binary data', function () {
437 beforeEach(function () {
438 sock
= new Websock();
439 sock
.open('ws://', 'binary');
440 sock
._websocket
._open();
443 it('should only send the send queue up to the send queue length', function () {
444 sock
._sQ
= new Uint8Array([1, 2, 3, 4, 5]);
446 var res
= sock
._encode_message();
447 expect(res
).to
.array
.equal(new Uint8Array([1, 2, 3]));
450 it('should properly pass the encoded data off to the actual WebSocket', function () {
451 sock
.send([1, 2, 3]);
452 expect(sock
._websocket
._get_sent_data()).to
.array
.equal(new Uint8Array([1, 2, 3]));