Update tests

This commit is contained in:
lilia 2015-07-28 13:48:51 -07:00
parent 637cbcbf99
commit 44b7b32385
3 changed files with 135 additions and 84 deletions

View file

@ -50,11 +50,11 @@
<script type="text/javascript" src="fake_api.js"></script>
<script type="text/javascript" src="helpers_test.js"></script>
<script type="text/javascript" src="websocket-resources_test.js"></script>
<script type="text/javascript" src="storage_test.js"></script>
<script type="text/javascript" src="device_storage_test.js"></script>
<script type="text/javascript" src="contacts_parser_test.js"></script>
<script type="text/javascript" src="generate_keys_test.js"></script>
<script type="text/javascript" src="websocket_test.js"></script>
<script type="text/javascript" src="websocket-resources_test.js"></script>
</body>
</html>

View file

@ -84,22 +84,4 @@ describe('MessageReceiver', function() {
var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8080', window);
});
});
describe('keepalives', function() {
it('sends a keepalive once a minute', function(done) {
this.timeout(60000);
var mockServer = new MockServer('ws://localhost:8081/v1/websocket/?login='+ encodeURIComponent(number) +'.1&password=password');
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');
server.close();
done();
});
});
var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8081', window);
});
});
});

View file

@ -18,6 +18,7 @@
'use strict';
describe('WebSocket-Resource', function() {
describe('requests and responses', function () {
it('receives requests and sends responses', function(done) {
// mock socket
var request_id = '1';
@ -33,11 +34,13 @@
};
// actual test
var resource = new WebSocketResource(socket, function (request) {
var resource = new WebSocketResource(socket, {
handleRequest: function (request) {
assert.strictEqual(request.verb, 'PUT');
assert.strictEqual(request.path, '/some/path');
assertEqualArrayBuffers(request.body.toArrayBuffer(), new Uint8Array([1,2,3]).buffer);
request.respond(200, 'OK');
}
});
// mock socket request
@ -71,7 +74,7 @@
};
// actual test
var resource = new WebSocketResource(socket, function() {});
var resource = new WebSocketResource(socket);
resource.sendRequest({
verb: 'PUT',
path: '/some/path',
@ -95,4 +98,70 @@
});
});
});
describe('close', function() {
before(function() { window.WebSocket = MockSocket; });
after (function() { window.WebSocket = WebSocket; });
it('closes the connection', function(done) {
var mockServer = new MockServer('ws://localhost:8081');
mockServer.on('connection', function(server) {
server.on('close', done);
});
var resource = new WebSocketResource(new WebSocket('ws://localhost:8081'));
resource.close();
});
});
describe('with a keepalive config', function() {
before(function() { window.WebSocket = MockSocket; });
after (function() { window.WebSocket = WebSocket; });
this.timeout(60000);
it('sends keepalives once a minute', function(done) {
var mockServer = new MockServer('ws://localhost:8081');
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');
server.close();
done();
});
});
var resource = new WebSocketResource(new WebSocket('ws://localhost:8081'), {
keepalive: { path: '/v1/keepalive' }
});
});
it('uses / as a default path', function() {
var mockServer = new MockServer('ws://localhost:8081');
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, '/');
server.close();
done();
});
});
var resource = new WebSocketResource(new WebSocket('ws://localhost:8081'), {
keepalive: true
});
});
it('optionally disconnects if no response', function(done) {
this.timeout(65000);
var mockServer = new MockServer('ws://localhost:8081');
var socket = new WebSocket('ws://localhost:8081');
mockServer.on('connection', function(server) {
server.on('close', done);
});
var resource = new WebSocketResource(socket, {
keepalive: { disconnect: true }
});
});
});
});
}());