Cable-Desktop/libtextsecure/test/websocket_test.js

63 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-05-01 00:51:06 +02:00
/* 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() {
2015-05-01 02:54:45 +02:00
var RealWebSocket = window.WebSocket;
2015-05-01 00:51:06 +02:00
before(function() { window.WebSocket = MockSocket; });
2015-05-01 02:54:45 +02:00
after (function() { window.WebSocket = RealWebSocket; });
it('connects and disconnects', function(done) {
2015-05-01 00:51:06 +02:00
var mockServer = new MockServer('ws://localhost:8080');
mockServer.on('connection', function(server) {
2015-05-01 02:54:45 +02:00
socket.close();
server.close();
done();
2015-05-01 00:51:06 +02:00
});
var socket = new TextSecureWebSocket('ws://localhost:8080');
});
it('sends a keepalive once a minute', function(done) {
this.timeout(60000);
2015-05-01 02:54:45 +02:00
var mockServer = new MockServer('ws://localhost:8081');
2015-05-01 00:51:06 +02:00
mockServer.on('connection', function(server) {
server.on('message', function(data) {
var message = textsecure.protobuf.WebSocketMessage.decode(data);
assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST);
assert.strictEqual(message.request.verb, 'GET');
assert.strictEqual(message.request.path, '/v1/keepalive');
2015-05-01 02:54:45 +02:00
socket.close();
server.close();
2015-05-01 00:51:06 +02:00
done();
});
});
2015-05-01 02:54:45 +02:00
var socket = new TextSecureWebSocket('ws://localhost:8081');
2015-05-01 00:51:06 +02:00
});
it('reconnects', function(done) {
this.timeout(60000);
2015-05-01 02:54:45 +02:00
var mockServer = new MockServer('ws://localhost:8082');
var socket = new TextSecureWebSocket('ws://localhost:8082');
2015-05-01 00:51:06 +02:00
socket.onclose = function() {
2015-05-01 02:54:45 +02:00
var mockServer = new MockServer('ws://localhost:8082');
mockServer.on('connection', function(server) {
socket.close();
server.close();
2015-05-01 00:51:06 +02:00
done();
});
};
mockServer.close();
});
});