Fix Websock send tests

Avoid poking around in the internals and instead test what is actually
sent out on the WebSocket.
This commit is contained in:
Pierre Ossman 2023-05-28 16:30:41 +02:00
parent 336ec86997
commit e07ca6a8e2
1 changed files with 18 additions and 31 deletions

View File

@ -6,7 +6,7 @@ import FakeWebSocket from './fake.websocket.js';
describe('Websock', function () { describe('Websock', function () {
"use strict"; "use strict";
describe('Queue methods', function () { describe('Receive queue methods', function () {
let sock; let sock;
const RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); const RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
@ -185,61 +185,48 @@ describe('Websock', function () {
expect(sock.rQi).to.equal(5); expect(sock.rQi).to.equal(5);
}); });
}); });
});
describe('Send queue methods', function () {
let sock;
beforeEach(function () {
let websock = new FakeWebSocket();
websock._open();
sock = new Websock();
sock.attach(websock);
});
describe('flush', function () { describe('flush', function () {
beforeEach(function () {
sock._websocket = {
send: sinon.spy()
};
});
it('should actually send on the websocket', function () { 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._sQ = new Uint8Array([1, 2, 3]);
sock._sQlen = 3; sock._sQlen = 3;
const encoded = sock._encodeMessage(); const encoded = sock._encodeMessage();
sock.flush(); sock.flush();
expect(sock._websocket.send).to.have.been.calledOnce; expect(sock).to.have.sent(encoded);
expect(sock._websocket.send).to.have.been.calledWith(encoded);
}); });
it('should not call send if we do not have anything queued up', function () { it('should not call send if we do not have anything queued up', function () {
sock._sQlen = 0; sock._sQlen = 0;
sock._websocket.bufferedAmount = 8;
sock.flush(); sock.flush();
expect(sock._websocket.send).not.to.have.been.called; expect(sock).to.have.sent(new Uint8Array([]));
}); });
}); });
describe('send', function () { describe('send', function () {
beforeEach(function () { it('should send the given data immediately', function () {
sock.flush = sinon.spy();
});
it('should add to the send queue', function () {
sock.send([1, 2, 3]); sock.send([1, 2, 3]);
const sq = sock.sQ; expect(sock).to.have.sent(new Uint8Array([1, 2, 3]));
expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3]));
});
it('should call flush', function () {
sock.send([1, 2, 3]);
expect(sock.flush).to.have.been.calledOnce;
}); });
}); });
describe('sendString', function () { describe('sendString', function () {
beforeEach(function () { it('should send after converting the string to an array', function () {
sock.send = sinon.spy();
});
it('should call send after converting the string to an array', function () {
sock.sendString("\x01\x02\x03"); sock.sendString("\x01\x02\x03");
expect(sock.send).to.have.been.calledWith([1, 2, 3]); expect(sock).to.have.sent(new Uint8Array([1, 2, 3]));
}); });
}); });
}); });