signal-desktop/libtextsecure/test/websocket-resources_test.js

238 lines
7.3 KiB
JavaScript
Raw Normal View History

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
describe('WebSocket-Resource', () => {
describe('requests and responses', () => {
it('receives requests and sends responses', done => {
// mock socket
const requestId = '1';
const socket = {
send(data) {
const message = window.textsecure.protobuf.WebSocketMessage.decode(
data
);
2018-11-02 18:02:53 +00:00
assert.strictEqual(
message.type,
window.textsecure.protobuf.WebSocketMessage.Type.RESPONSE
2018-11-02 18:02:53 +00:00
);
assert.strictEqual(message.response.message, 'OK');
assert.strictEqual(message.response.status, 200);
assert.strictEqual(message.response.id.toString(), requestId);
done();
},
addEventListener() {},
};
2015-07-28 20:48:51 +00:00
2018-11-02 18:02:53 +00:00
// actual test
this.resource = new window.textsecure.WebSocketResource(socket, {
2018-11-02 18:02:53 +00:00
handleRequest(request) {
assert.strictEqual(request.verb, 'PUT');
assert.strictEqual(request.path, '/some/path');
assertEqualArrayBuffers(
request.body.toArrayBuffer(),
window.Signal.Crypto.typedArrayToArrayBuffer(
new Uint8Array([1, 2, 3])
)
2018-11-02 18:02:53 +00:00
);
request.respond(200, 'OK');
},
2018-05-02 16:51:22 +00:00
});
2015-07-28 20:48:51 +00:00
2018-11-02 18:02:53 +00:00
// mock socket request
socket.onmessage({
data: new Blob([
new window.textsecure.protobuf.WebSocketMessage({
type: window.textsecure.protobuf.WebSocketMessage.Type.REQUEST,
2018-11-02 18:02:53 +00:00
request: {
id: requestId,
verb: 'PUT',
path: '/some/path',
body: window.Signal.Crypto.typedArrayToArrayBuffer(
new Uint8Array([1, 2, 3])
),
2018-11-02 18:02:53 +00:00
},
})
.encode()
.toArrayBuffer(),
]),
2018-05-02 16:51:22 +00:00
});
});
2018-11-02 18:02:53 +00:00
it('sends requests and receives responses', done => {
// mock socket and request handler
let requestId;
const socket = {
send(data) {
const message = window.textsecure.protobuf.WebSocketMessage.decode(
data
);
2018-11-02 18:02:53 +00:00
assert.strictEqual(
message.type,
window.textsecure.protobuf.WebSocketMessage.Type.REQUEST
2018-11-02 18:02:53 +00:00
);
assert.strictEqual(message.request.verb, 'PUT');
assert.strictEqual(message.request.path, '/some/path');
assertEqualArrayBuffers(
message.request.body.toArrayBuffer(),
window.Signal.Crypto.typedArrayToArrayBuffer(
new Uint8Array([1, 2, 3])
)
2018-11-02 18:02:53 +00:00
);
requestId = message.request.id;
},
addEventListener() {},
};
// actual test
const resource = new window.textsecure.WebSocketResource(socket);
2018-11-02 18:02:53 +00:00
resource.sendRequest({
verb: 'PUT',
path: '/some/path',
body: window.Signal.Crypto.typedArrayToArrayBuffer(
new Uint8Array([1, 2, 3])
),
2018-11-02 18:02:53 +00:00
error: done,
success(message, status) {
assert.strictEqual(message, 'OK');
assert.strictEqual(status, 200);
done();
},
2018-05-02 16:51:22 +00:00
});
2018-11-02 18:02:53 +00:00
// mock socket response
socket.onmessage({
data: new Blob([
new window.textsecure.protobuf.WebSocketMessage({
type: window.textsecure.protobuf.WebSocketMessage.Type.RESPONSE,
2018-11-02 18:02:53 +00:00
response: { id: requestId, message: 'OK', status: 200 },
})
.encode()
.toArrayBuffer(),
]),
2018-05-02 16:51:22 +00:00
});
});
2018-11-02 18:02:53 +00:00
});
2015-07-28 20:48:51 +00:00
2018-11-02 18:02:53 +00:00
describe('close', () => {
before(() => {
window.WebSocket = MockSocket;
});
after(() => {
window.WebSocket = WebSocket;
});
it('closes the connection', done => {
const mockServer = new MockServer('ws://localhost:8081');
mockServer.on('connection', server => {
server.on('close', done);
2018-05-02 16:51:22 +00:00
});
const resource = new window.textsecure.WebSocketResource(
2018-11-02 18:02:53 +00:00
new WebSocket('ws://localhost:8081')
);
resource.close();
});
});
describe.skip('with a keepalive config', function thisNeeded() {
before(() => {
window.WebSocket = MockSocket;
});
after(() => {
window.WebSocket = WebSocket;
});
this.timeout(60000);
it('sends keepalives once a minute', done => {
const mockServer = new MockServer('ws://localhost:8081');
mockServer.on('connection', server => {
server.on('message', data => {
const message = window.textsecure.protobuf.WebSocketMessage.decode(
data
);
2018-11-02 18:02:53 +00:00
assert.strictEqual(
message.type,
window.textsecure.protobuf.WebSocketMessage.Type.REQUEST
2018-11-02 18:02:53 +00:00
);
assert.strictEqual(message.request.verb, 'GET');
assert.strictEqual(message.request.path, '/v1/keepalive');
server.close();
done();
2018-05-02 16:51:22 +00:00
});
});
this.resource = new window.textsecure.WebSocketResource(
2018-11-02 18:02:53 +00:00
new WebSocket('ws://loc1alhost:8081'),
{
keepalive: { path: '/v1/keepalive' },
}
);
});
2018-11-02 18:02:53 +00:00
it('uses / as a default path', done => {
const mockServer = new MockServer('ws://localhost:8081');
mockServer.on('connection', server => {
server.on('message', data => {
const message = window.textsecure.protobuf.WebSocketMessage.decode(
data
);
2018-11-02 18:02:53 +00:00
assert.strictEqual(
message.type,
window.textsecure.protobuf.WebSocketMessage.Type.REQUEST
2018-11-02 18:02:53 +00:00
);
assert.strictEqual(message.request.verb, 'GET');
assert.strictEqual(message.request.path, '/');
server.close();
done();
2018-05-02 16:51:22 +00:00
});
});
this.resource = new window.textsecure.WebSocketResource(
2018-11-02 18:02:53 +00:00
new WebSocket('ws://localhost:8081'),
{
keepalive: true,
}
);
});
2018-11-02 18:02:53 +00:00
it('optionally disconnects if no response', function thisNeeded1(done) {
this.timeout(65000);
const mockServer = new MockServer('ws://localhost:8081');
const socket = new WebSocket('ws://localhost:8081');
mockServer.on('connection', server => {
server.on('close', done);
2018-05-02 16:51:22 +00:00
});
this.resource = new window.textsecure.WebSocketResource(socket, {
keepalive: true,
});
2018-11-02 18:02:53 +00:00
});
2015-07-28 21:59:16 +00:00
2018-11-02 18:02:53 +00:00
it('allows resetting the keepalive timer', function thisNeeded2(done) {
this.timeout(65000);
const mockServer = new MockServer('ws://localhost:8081');
const socket = new WebSocket('ws://localhost:8081');
const startTime = Date.now();
mockServer.on('connection', server => {
server.on('message', data => {
const message = window.textsecure.protobuf.WebSocketMessage.decode(
data
);
2018-11-02 18:02:53 +00:00
assert.strictEqual(
message.type,
window.textsecure.protobuf.WebSocketMessage.Type.REQUEST
2018-11-02 18:02:53 +00:00
);
assert.strictEqual(message.request.verb, 'GET');
assert.strictEqual(message.request.path, '/');
assert(
Date.now() > startTime + 60000,
'keepalive time should be longer than a minute'
);
server.close();
done();
});
2018-05-02 16:51:22 +00:00
});
const resource = new window.textsecure.WebSocketResource(socket, {
keepalive: true,
});
2018-11-02 18:02:53 +00:00
setTimeout(() => {
resource.resetKeepAliveTimer();
}, 5000);
});
2018-05-02 16:51:22 +00:00
});
2018-11-02 18:02:53 +00:00
});