2016-08-07 12:58:20 +00:00
|
|
|
import "typings-test";
|
2016-09-04 22:34:09 +00:00
|
|
|
import should = require("should");
|
2016-08-07 12:58:20 +00:00
|
|
|
import socketIoClient = require("socket.io-client");
|
|
|
|
import smartsocket = require("../dist/index");
|
2016-08-14 20:17:55 +00:00
|
|
|
import q = require("q");
|
2016-09-25 22:23:17 +00:00
|
|
|
import nodehash = require("nodehash")
|
2016-08-07 12:58:20 +00:00
|
|
|
|
2016-09-25 22:23:17 +00:00
|
|
|
let testSmartsocket: smartsocket.Smartsocket
|
|
|
|
let testSmartsocketClient: smartsocket.SmartsocketClient
|
|
|
|
let testSocketRole1: smartsocket.SocketRole
|
|
|
|
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 });
|
2016-09-04 22:34:09 +00:00
|
|
|
should(testSmartsocket).be.instanceOf(smartsocket.Smartsocket);
|
2016-08-14 20:17:55 +00:00
|
|
|
});
|
|
|
|
it("should start listening when .started is called", function () {
|
|
|
|
testSmartsocket.startServer();
|
|
|
|
});
|
2016-08-07 12:58:20 +00:00
|
|
|
});
|
2016-09-04 22:34:09 +00:00
|
|
|
describe("class SocketRole", function () {
|
|
|
|
it("should add a socketrole", function () {
|
|
|
|
testSocketRole1 = new smartsocket.SocketRole({
|
|
|
|
name: "testRole1",
|
|
|
|
passwordHash: nodehash.sha256FromStringSync("testPassword")
|
|
|
|
});
|
|
|
|
testSmartsocket.addSocketRoles([testSocketRole1]);
|
|
|
|
});
|
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({
|
2016-09-04 22:34:09 +00:00
|
|
|
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-09-04 22:34:09 +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-09-04 22:34:09 +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);
|
2016-09-04 22:34:09 +00:00
|
|
|
testSmartsocketClient.serverCall("testFunction1", {
|
|
|
|
value1: "hello"
|
2016-08-14 23:38:28 +00:00
|
|
|
}).then((dataArg) => {
|
|
|
|
console.log(dataArg);
|
|
|
|
done();
|
|
|
|
});
|
2016-08-15 00:36:17 +00:00
|
|
|
});
|
2016-09-04 22:34:09 +00:00
|
|
|
it("should be able to make a functionCall from server to client", function (done) {
|
2016-08-15 00:36:17 +00:00
|
|
|
this.timeout(5000);
|
|
|
|
let targetSocket = (() => {
|
2016-09-04 22:34:09 +00:00
|
|
|
return smartsocket.allSocketConnections.find((socketConnectionArg) => {
|
2016-08-15 00:36:17 +00:00
|
|
|
return socketConnectionArg.alias === "testClient1";
|
|
|
|
});
|
|
|
|
})();
|
2016-09-04 22:34:09 +00:00
|
|
|
testSmartsocket.clientCall("testFunction1", {
|
|
|
|
value1: "helloFromServer"
|
|
|
|
}, targetSocket).then((dataArg) => {
|
2016-08-15 00:36:17 +00:00
|
|
|
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
|
|
|
});
|