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;
|
2016-08-12 03:56:40 +00:00
|
|
|
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
2016-08-14 23:38:28 +00:00
|
|
|
let testSocketRole1: smartsocket.SocketRole;
|
2016-08-14 20:17:55 +00:00
|
|
|
let testSocketFunction1:smartsocket.SocketFunction;
|
2016-08-12 03:56:40 +00:00
|
|
|
|
|
|
|
let testConfig = {
|
2016-08-14 20:17:55 +00:00
|
|
|
port: 3000
|
2016-08-12 03:56:40 +00:00
|
|
|
}
|
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(){
|
2016-08-14 23:38:28 +00:00
|
|
|
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",
|
2016-08-14 23:38:28 +00:00
|
|
|
funcDef: (dataArg) => {
|
2016-08-14 20:17:55 +00:00
|
|
|
let done = q.defer();
|
2016-08-14 23:38:28 +00:00
|
|
|
done.resolve(dataArg);
|
2016-08-14 20:17:55 +00:00
|
|
|
return done.promise;
|
|
|
|
},
|
2016-08-14 23:38:28 +00:00
|
|
|
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",
|
2016-08-14 23:38:28 +00:00
|
|
|
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 () {
|
|
|
|
|
|
|
|
});
|
2016-08-15 00:36:17 +00:00
|
|
|
it("should be able to make a functionCall from client to server",function(done){
|
2016-08-14 23:38:28 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
testSmartsocketClient.serverCall("testFunction1",{
|
|
|
|
value1:"hello"
|
|
|
|
}).then((dataArg) => {
|
|
|
|
console.log(dataArg);
|
|
|
|
done();
|
|
|
|
});
|
2016-08-15 00:36:17 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
});
|