Cable-Desktop/libtextsecure/test/websocket_test.js
lilia 7d3d634a2d Move/refactor keepalive logic and add disconnect timer
We now disconnect ourselves if we don't get the server's response to a
keepalive request within 30s. This way we will eventually disconnect if
the network goes away but the socket is not closed.*

* See code.google.com/p/chromium/issues/detail?id=197841 and
https://stackoverflow.com/questions/11755605/chrome-websocket-connection-not-closed-when-browser-closed

We will then try to reconnect once a minute (See 8a10c96);

Keepalives belong at this level anyway, since the format is defined by
both the websocket resource protocol and our specific server url
structure.

// FREEBIE
2015-07-16 15:01:34 -07:00

78 lines
2.8 KiB
JavaScript

/* vim: ts=4:sw=4:expandtab
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
describe('TextSecureWebSocket', function() {
var RealWebSocket = window.WebSocket;
before(function() { window.WebSocket = MockSocket; });
after (function() { window.WebSocket = RealWebSocket; });
it('connects and disconnects', function(done) {
var mockServer = new MockServer('ws://localhost:8080');
mockServer.on('connection', function(server) {
socket.close();
server.close();
done();
});
var socket = new TextSecureWebSocket('ws://localhost:8080');
});
it('sends and receives', function(done) {
var mockServer = new MockServer('ws://localhost:8080');
mockServer.on('connection', function(server) {
server.on('message', function(data) {
server.send('ack');
server.close();
});
});
var socket = new TextSecureWebSocket('ws://localhost:8080');
socket.onmessage = function(response) {
assert.strictEqual(response.data, 'ack');
socket.close();
done();
};
socket.send('syn');
});
it('exposes the socket status', function(done) {
var mockServer = new MockServer('ws://localhost:8082');
mockServer.on('connection', function(server) {
assert.strictEqual(socket.getStatus(), WebSocket.OPEN);
server.close();
socket.close();
});
var socket = new TextSecureWebSocket('ws://localhost:8082');
socket.onclose = function() {
assert.strictEqual(socket.getStatus(), WebSocket.CLOSING);
done();
};
});
it('reconnects', function(done) {
this.timeout(60000);
var mockServer = new MockServer('ws://localhost:8082');
var socket = new TextSecureWebSocket('ws://localhost:8082');
socket.onclose = function() {
var mockServer = new MockServer('ws://localhost:8082');
mockServer.on('connection', function(server) {
socket.close();
server.close();
done();
});
};
mockServer.close();
});
});