2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-04-30 22:51:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
describe('TextSecureWebSocket', function() {
|
2015-05-01 00:54:45 +00:00
|
|
|
var RealWebSocket = window.WebSocket;
|
2015-04-30 22:51:06 +00:00
|
|
|
before(function() { window.WebSocket = MockSocket; });
|
2015-05-01 00:54:45 +00:00
|
|
|
after (function() { window.WebSocket = RealWebSocket; });
|
|
|
|
it('connects and disconnects', function(done) {
|
2015-04-30 22:51:06 +00:00
|
|
|
var mockServer = new MockServer('ws://localhost:8080');
|
|
|
|
mockServer.on('connection', function(server) {
|
2015-05-01 00:54:45 +00:00
|
|
|
socket.close();
|
|
|
|
server.close();
|
|
|
|
done();
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
|
|
|
var socket = new TextSecureWebSocket('ws://localhost:8080');
|
|
|
|
});
|
|
|
|
|
2015-05-01 19:51:55 +00:00
|
|
|
it('sends and receives', function(done) {
|
|
|
|
var mockServer = new MockServer('ws://localhost:8080');
|
2015-04-30 22:51:06 +00:00
|
|
|
mockServer.on('connection', function(server) {
|
|
|
|
server.on('message', function(data) {
|
2015-05-01 19:51:55 +00:00
|
|
|
server.send('ack');
|
2015-05-01 00:54:45 +00:00
|
|
|
server.close();
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
|
|
|
});
|
2015-05-01 19:51:55 +00:00
|
|
|
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();
|
|
|
|
};
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('reconnects', function(done) {
|
|
|
|
this.timeout(60000);
|
2015-05-01 00:54:45 +00:00
|
|
|
var mockServer = new MockServer('ws://localhost:8082');
|
|
|
|
var socket = new TextSecureWebSocket('ws://localhost:8082');
|
2015-04-30 22:51:06 +00:00
|
|
|
socket.onclose = function() {
|
2015-05-01 00:54:45 +00:00
|
|
|
var mockServer = new MockServer('ws://localhost:8082');
|
|
|
|
mockServer.on('connection', function(server) {
|
|
|
|
socket.close();
|
|
|
|
server.close();
|
2015-04-30 22:51:06 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
mockServer.close();
|
|
|
|
});
|
2015-05-01 19:51:55 +00:00
|
|
|
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|