2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2015-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
/* global TextSecureWebSocket */
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('TextSecureWebSocket', () => {
|
|
|
|
const RealWebSocket = window.WebSocket;
|
|
|
|
before(() => {
|
2018-05-02 16:51:22 +00:00
|
|
|
window.WebSocket = MockSocket;
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
after(() => {
|
2018-05-02 16:51:22 +00:00
|
|
|
window.WebSocket = RealWebSocket;
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('connects and disconnects', done => {
|
|
|
|
const mockServer = new MockServer('ws://localhost:8080');
|
|
|
|
mockServer.on('connection', server => {
|
2018-05-02 16:51:22 +00:00
|
|
|
socket.close();
|
|
|
|
server.close();
|
|
|
|
done();
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
const socket = new TextSecureWebSocket('ws://localhost:8080');
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2015-04-30 22:51:06 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
it('sends and receives', done => {
|
|
|
|
const mockServer = new MockServer('ws://localhost:8080');
|
|
|
|
mockServer.on('connection', server => {
|
2018-11-02 18:02:53 +00:00
|
|
|
server.on('message', () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
server.send('ack');
|
|
|
|
server.close();
|
|
|
|
});
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
const socket = new TextSecureWebSocket('ws://localhost:8080');
|
2018-11-02 18:02:53 +00:00
|
|
|
socket.onmessage = response => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(response.data, 'ack');
|
|
|
|
socket.close();
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
socket.send('syn');
|
|
|
|
});
|
2015-04-30 22:51:06 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
it('exposes the socket status', done => {
|
|
|
|
const mockServer = new MockServer('ws://localhost:8082');
|
|
|
|
mockServer.on('connection', server => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(socket.getStatus(), WebSocket.OPEN);
|
|
|
|
server.close();
|
|
|
|
socket.close();
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
const socket = new TextSecureWebSocket('ws://localhost:8082');
|
|
|
|
socket.onclose = () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
assert.strictEqual(socket.getStatus(), WebSocket.CLOSING);
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
});
|
2015-05-01 19:51:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('reconnects', function thisNeeded(done) {
|
2018-05-02 16:51:22 +00:00
|
|
|
this.timeout(60000);
|
2018-07-21 21:51:20 +00:00
|
|
|
const mockServer = new MockServer('ws://localhost:8082');
|
|
|
|
const socket = new TextSecureWebSocket('ws://localhost:8082');
|
2018-11-02 18:02:53 +00:00
|
|
|
socket.onclose = () => {
|
|
|
|
const secondServer = new MockServer('ws://localhost:8082');
|
|
|
|
secondServer.on('connection', server => {
|
2018-05-02 16:51:22 +00:00
|
|
|
socket.close();
|
|
|
|
server.close();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
mockServer.close();
|
|
|
|
});
|
2015-04-30 22:51:06 +00:00
|
|
|
});
|