-// requires local modules: websock, util
-// requires test modules: fake.websocket, assertions
/* jshint expr: true */
var assert = chai.assert;
var expect = chai.expect;
+import Websock from '../core/websock.js';
+import FakeWebSocket from './fake.websocket.js';
+
+import sinon from '../vendor/sinon.js';
+
describe('Websock', function() {
"use strict";
};
});
- it('should actually send on the websocket if the websocket does not have too much buffered', function () {
- sock.maxBufferedAmount = 10;
+ it('should actually send on the websocket', function () {
sock._websocket.bufferedAmount = 8;
+ sock._websocket.readyState = WebSocket.OPEN
sock._sQ = new Uint8Array([1, 2, 3]);
sock._sQlen = 3;
var encoded = sock._encode_message();
expect(sock._websocket.send).to.have.been.calledWith(encoded);
});
- it('should return true if the websocket did not have too much buffered', function () {
- sock.maxBufferedAmount = 10;
- sock._websocket.bufferedAmount = 8;
-
- expect(sock.flush()).to.be.true;
- });
-
it('should not call send if we do not have anything queued up', function () {
sock._sQlen = 0;
- sock.maxBufferedAmount = 10;
sock._websocket.bufferedAmount = 8;
sock.flush();
expect(sock._websocket.send).not.to.have.been.called;
});
-
- it('should not send and return false if the websocket has too much buffered', function () {
- sock.maxBufferedAmount = 10;
- sock._websocket.bufferedAmount = 12;
-
- expect(sock.flush()).to.be.false;
- expect(sock._websocket.send).to.not.have.been.called;
- });
});
describe('send', function () {
expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
});
- it('should fail if we specify a protocol besides binary', function () {
- expect(function () { sock.open('ws:///', 'base64'); }).to.throw(Error);
- });
-
// it('should initialize the event handlers')?
});
expect(sock._recv_message).to.have.been.calledOnce;
});
- it('should fail if a protocol besides binary is requested', function () {
- sock._websocket.protocol = 'base64';
- expect(sock._websocket.onopen).to.throw(Error);
- });
-
- it('should assume binary if no protocol was available on opening', function () {
- sock._websocket.protocol = null;
- sock._websocket.onopen();
- expect(sock._mode).to.equal('binary');
- });
-
it('should call the open event handler on opening', function () {
sock._websocket.onopen();
expect(sock._eventHandlers.open).to.have.been.calledOnce;
expect(sock.get_rQi()).to.equal(0);
});
- it('should call the error event handler on an exception', function () {
- sock._eventHandlers.error = sinon.spy();
- sock._eventHandlers.message = sinon.stub().throws();
- var msg = { data: new Uint8Array([1, 2, 3]).buffer };
+ it('should automatically resize the receive queue if the incoming message is too large', function () {
+ sock._rQ = new Uint8Array(20);
+ sock._rQlen = 0;
+ sock.set_rQi(0);
+ sock._rQbufferSize = 20;
+ sock._rQmax = 2;
+ var msg = { data: new Uint8Array(30).buffer };
sock._mode = 'binary';
sock._recv_message(msg);
- expect(sock._eventHandlers.error).to.have.been.calledOnce;
+ expect(sock._rQlen).to.equal(30);
+ expect(sock.get_rQi()).to.equal(0);
+ expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
});
});