summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2021-04-18 11:26:51 +0200
committerPierre Ossman <ossman@cendio.se>2021-04-18 14:27:57 +0200
commitdbd519558c1efd98afd08126594efc334eb88dd6 (patch)
treea00556b43c41f8986392994f9880eb22bcafaa48 /tests
parentde9fc9508cb4bc6776792f0c7731da17519d4f24 (diff)
downloadnovnc-dbd519558c1efd98afd08126594efc334eb88dd6.tar.gz
Initiate connection from RFB constructor
We need to do this in order to safely attach to existing WebSocket objects. There may be message events already pending so we must set up our event handlers before returning. This means we will now throw errors instead of generating "disconnect" events on problems as the caller no longer has the opportunity to set up event handlers. This might have been the correct approach from the start as it mimics how e.g. the WebSocket constructor works.
Diffstat (limited to 'tests')
-rw-r--r--tests/test.rfb.js38
1 files changed, 7 insertions, 31 deletions
diff --git a/tests/test.rfb.js b/tests/test.rfb.js
index c99edc7..404f7db 100644
--- a/tests/test.rfb.js
+++ b/tests/test.rfb.js
@@ -151,34 +151,21 @@ describe('Remote Frame Buffer Protocol Client', function () {
attach.restore();
});
- it('should not connect from constructor', function () {
- new RFB(document.createElement('div'), 'wss://host:8675');
- expect(open).to.not.have.been.called;
- this.clock.tick(); // Flush the pending connection
- });
-
it('should actually connect to the websocket', function () {
new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
- this.clock.tick();
expect(open).to.have.been.calledOnceWithExactly('ws://HOST:8675/PATH', []);
});
- it('should report connection problems via event', function () {
+ it('should pass on connection problems', function () {
open.restore();
open = sinon.stub(Websock.prototype, 'open');
- open.throws(Error('Failure'));
- const client = new RFB(document.createElement('div'), 'ws://HOST:8675/PATH');
- let callback = sinon.spy();
- client.addEventListener('disconnect', callback);
- this.clock.tick();
- expect(callback).to.have.been.calledOnce;
- expect(callback.args[0][0].detail.clean).to.be.false;
+ open.throws(new Error('Failure'));
+ expect(() => new RFB(document.createElement('div'), 'ws://HOST:8675/PATH')).to.throw('Failure');
});
it('should handle WebSocket/RTCDataChannel objects', function () {
let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
new RFB(document.createElement('div'), sock);
- this.clock.tick();
expect(open).to.not.have.been.called;
expect(attach).to.have.been.calledOnceWithExactly(sock);
});
@@ -189,7 +176,6 @@ describe('Remote Frame Buffer Protocol Client', function () {
const client = new RFB(document.createElement('div'), sock);
let callback = sinon.spy();
client.addEventListener('disconnect', callback);
- this.clock.tick();
expect(open).to.not.have.been.called;
expect(attach).to.have.been.calledOnceWithExactly(sock);
// Check if it is ready for some data
@@ -200,25 +186,15 @@ describe('Remote Frame Buffer Protocol Client', function () {
it('should refuse closed WebSocket/RTCDataChannel objects', function () {
let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
sock.readyState = WebSocket.CLOSED;
- const client = new RFB(document.createElement('div'), sock);
- let callback = sinon.spy();
- client.addEventListener('disconnect', callback);
- this.clock.tick();
- expect(callback).to.have.been.calledOnce;
- expect(callback.args[0][0].detail.clean).to.be.false;
+ expect(() => new RFB(document.createElement('div'), sock)).to.throw();
});
- it('should report attach problems via event', function () {
+ it('should pass on attach problems', function () {
attach.restore();
attach = sinon.stub(Websock.prototype, 'attach');
- attach.throws(Error('Failure'));
+ attach.throws(new Error('Failure'));
let sock = new FakeWebSocket('ws://HOST:8675/PATH', []);
- const client = new RFB(document.createElement('div'), sock);
- let callback = sinon.spy();
- client.addEventListener('disconnect', callback);
- this.clock.tick();
- expect(callback).to.have.been.calledOnce;
- expect(callback.args[0][0].detail.clean).to.be.false;
+ expect(() => new RFB(document.createElement('div'), sock)).to.throw('Failure');
});
});