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="fake_api.js"></script>
<script type="text/javascript" src="helpers_test.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="storage_test.js"></script>
<script type="text/javascript" src="device_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="contacts_parser_test.js"></script>
<script type="text/javascript" src="generate_keys_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_test.js"></script>
<script type="text/javascript" src="websocket-resources_test.js"></script>
</body> </body>
</html> </html>

View file

@ -84,22 +84,4 @@ describe('MessageReceiver', function() {
var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8080', window); 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,80 +18,149 @@
'use strict'; 'use strict';
describe('WebSocket-Resource', function() { describe('WebSocket-Resource', function() {
it('receives requests and sends responses', function(done) { describe('requests and responses', function () {
// mock socket it('receives requests and sends responses', function(done) {
var request_id = '1'; // mock socket
var socket = { var request_id = '1';
send: function(data) { var socket = {
var message = textsecure.protobuf.WebSocketMessage.decode(data); send: function(data) {
assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.RESPONSE); var message = textsecure.protobuf.WebSocketMessage.decode(data);
assert.strictEqual(message.response.message, 'OK'); assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.RESPONSE);
assert.strictEqual(message.response.status, 200); assert.strictEqual(message.response.message, 'OK');
assert.strictEqual(message.response.id.toString(), request_id); assert.strictEqual(message.response.status, 200);
done(); assert.strictEqual(message.response.id.toString(), request_id);
} done();
}; }
};
// actual test // actual test
var resource = new WebSocketResource(socket, function (request) { var resource = new WebSocketResource(socket, {
assert.strictEqual(request.verb, 'PUT'); handleRequest: function (request) {
assert.strictEqual(request.path, '/some/path'); assert.strictEqual(request.verb, 'PUT');
assertEqualArrayBuffers(request.body.toArrayBuffer(), new Uint8Array([1,2,3]).buffer); assert.strictEqual(request.path, '/some/path');
request.respond(200, 'OK'); assertEqualArrayBuffers(request.body.toArrayBuffer(), new Uint8Array([1,2,3]).buffer);
request.respond(200, 'OK');
}
});
// mock socket request
socket.onmessage({
data: new Blob([
new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: {
id: request_id,
verb: 'PUT',
path: '/some/path',
body: new Uint8Array([1,2,3]).buffer
}
}).encode().toArrayBuffer()
])
});
}); });
// mock socket request it('sends requests and receives responses', function(done) {
socket.onmessage({ // mock socket and request handler
data: new Blob([ var request_id;
new textsecure.protobuf.WebSocketMessage({ var socket = {
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST, send: function(data) {
request: { var message = textsecure.protobuf.WebSocketMessage.decode(data);
id: request_id, assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST);
verb: 'PUT', assert.strictEqual(message.request.verb, 'PUT');
path: '/some/path', assert.strictEqual(message.request.path, '/some/path');
body: new Uint8Array([1,2,3]).buffer assertEqualArrayBuffers(message.request.body.toArrayBuffer(), new Uint8Array([1,2,3]).buffer);
} request_id = message.request.id;
}).encode().toArrayBuffer() }
]) };
// actual test
var resource = new WebSocketResource(socket);
resource.sendRequest({
verb: 'PUT',
path: '/some/path',
body: new Uint8Array([1,2,3]).buffer,
error: done,
success: function(message, status, request) {
assert.strictEqual(message, 'OK');
assert.strictEqual(status, 200);
done();
}
});
// mock socket response
socket.onmessage({
data: new Blob([
new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE,
response: { id: request_id, message: 'OK', status: 200 }
}).encode().toArrayBuffer()
])
});
}); });
}); });
it('sends requests and receives responses', function(done) { describe('close', function() {
// mock socket and request handler before(function() { window.WebSocket = MockSocket; });
var request_id; after (function() { window.WebSocket = WebSocket; });
var socket = { it('closes the connection', function(done) {
send: function(data) { var mockServer = new MockServer('ws://localhost:8081');
var message = textsecure.protobuf.WebSocketMessage.decode(data); mockServer.on('connection', function(server) {
assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST); server.on('close', done);
assert.strictEqual(message.request.verb, 'PUT'); });
assert.strictEqual(message.request.path, '/some/path'); var resource = new WebSocketResource(new WebSocket('ws://localhost:8081'));
assertEqualArrayBuffers(message.request.body.toArrayBuffer(), new Uint8Array([1,2,3]).buffer); resource.close();
request_id = message.request.id; });
} });
};
// actual test describe('with a keepalive config', function() {
var resource = new WebSocketResource(socket, function() {}); before(function() { window.WebSocket = MockSocket; });
resource.sendRequest({ after (function() { window.WebSocket = WebSocket; });
verb: 'PUT', this.timeout(60000);
path: '/some/path', it('sends keepalives once a minute', function(done) {
body: new Uint8Array([1,2,3]).buffer, var mockServer = new MockServer('ws://localhost:8081');
error: done, mockServer.on('connection', function(server) {
success: function(message, status, request) { server.on('message', function(data) {
assert.strictEqual(message, 'OK'); var message = textsecure.protobuf.WebSocketMessage.decode(data);
assert.strictEqual(status, 200); assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST);
done(); 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' }
});
}); });
// mock socket response it('uses / as a default path', function() {
socket.onmessage({ var mockServer = new MockServer('ws://localhost:8081');
data: new Blob([ mockServer.on('connection', function(server) {
new textsecure.protobuf.WebSocketMessage({ server.on('message', function(data) {
type: textsecure.protobuf.WebSocketMessage.Type.RESPONSE, var message = textsecure.protobuf.WebSocketMessage.decode(data);
response: { id: request_id, message: 'OK', status: 200 } assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST);
}).encode().toArrayBuffer() 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 }
});
}); });
}); });
}); });