smartsocket/test/test.ts

108 lines
3.8 KiB
TypeScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
import "typings-test";
import "should";
import socketIoClient = require("socket.io-client");
import smartsocket = require("../dist/index");
2016-08-14 20:17:55 +00:00
import q = require("q");
2016-08-07 12:58:20 +00:00
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketRole1: smartsocket.SocketRole;
2016-08-14 20:17:55 +00:00
let testSocketFunction1:smartsocket.SocketFunction;
let testConfig = {
2016-08-14 20:17:55 +00:00
port: 3000
}
2016-08-07 12:58:20 +00:00
describe("smartsocket", function () {
2016-08-14 20:17:55 +00:00
describe("class Smartsocket", function () {
it("should create a new smartsocket", function () {
testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port });
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
});
it("should start listening when .started is called", function () {
testSmartsocket.startServer();
});
2016-08-07 12:58:20 +00:00
});
2016-08-14 20:17:55 +00:00
describe("class SocketRole", function(){
testSocketRole1 = new smartsocket.SocketRole({
name:"testRole1",
passwordHash:"somehash"
})
2016-08-07 16:59:39 +00:00
})
2016-08-14 20:17:55 +00:00
describe("class SocketFunction", function () {
it("should register a new Function", function () {
testSocketFunction1 = new smartsocket.SocketFunction({
funcName:"testFunction1",
funcDef: (dataArg) => {
2016-08-14 20:17:55 +00:00
let done = q.defer();
done.resolve(dataArg);
2016-08-14 20:17:55 +00:00
return done.promise;
},
allowedRoles:[testSocketRole1]
2016-08-14 01:25:26 +00:00
});
2016-08-14 20:17:55 +00:00
});
2016-08-14 01:25:26 +00:00
});
2016-08-14 20:17:55 +00:00
describe("class SmartsocketClient", function () {
it("should react to a new websocket connection from client", function (done) {
this.timeout(10000);
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: "http://localhost",
password: "testPassword",
alias: "testClient1",
role: "testRole1"
2016-08-14 01:25:26 +00:00
});
2016-08-14 20:17:55 +00:00
testSmartsocketClient.connect()
.then(() => {
done();
});
});
it("client should disconnect and reconnect", function (done) {
this.timeout(10000);
testSmartsocketClient.disconnect()
.then(() => {
let done = q.defer();
setTimeout(() => {
testSmartsocketClient.connect()
.then(done.resolve)
}, 0)
return done.promise;
})
.then(() => {
done();
});
});
it("2 clients should connect in parallel", function () {
});
it("should be able to make a functionCall from client to server",function(done){
this.timeout(5000);
testSmartsocketClient.serverCall("testFunction1",{
value1:"hello"
}).then((dataArg) => {
console.log(dataArg);
done();
});
});
it("should be able to make a functionCall from server to client",function(done){
this.timeout(5000);
let targetSocket = (() => {
return smartsocket.allSocketConnections.find((socketConnectionArg)=>{
return socketConnectionArg.alias === "testClient1";
});
})();
testSmartsocket.clientCall("testFunction1",{
value1:"helloFromServer"
},targetSocket).then((dataArg) => {
console.log(dataArg);
done();
});
});
2016-08-07 16:59:39 +00:00
});
2016-08-14 20:17:55 +00:00
describe("terminating smartsocket", function () {
it("should close the server", function () {
testSmartsocket.closeServer();
});
})
2016-08-07 12:58:20 +00:00
});