signal-desktop/libtextsecure/test/websocket_test.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-04-30 22:51:06 +00:00
describe('TextSecureWebSocket', function() {
2018-05-02 16:51:22 +00:00
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();
2015-04-30 22:51:06 +00:00
});
2018-05-02 16:51:22 +00:00
var socket = new TextSecureWebSocket('ws://localhost:8080');
});
2015-04-30 22:51:06 +00:00
2018-05-02 16:51:22 +00:00
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();
});
2015-04-30 22:51:06 +00:00
});
2018-05-02 16:51:22 +00:00
var socket = new TextSecureWebSocket('ws://localhost:8080');
socket.onmessage = function(response) {
assert.strictEqual(response.data, 'ack');
socket.close();
done();
};
socket.send('syn');
});
2015-04-30 22:51:06 +00:00
2018-05-02 16:51:22 +00:00
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();
2015-04-30 22:51:06 +00:00
});
2018-05-02 16:51:22 +00:00
var socket = new TextSecureWebSocket('ws://localhost:8082');
socket.onclose = function() {
assert.strictEqual(socket.getStatus(), WebSocket.CLOSING);
done();
};
});
2015-05-01 19:51:55 +00:00
2018-05-02 16:51:22 +00:00
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();
});
2015-04-30 22:51:06 +00:00
});