smartsocket/test/test.ts

112 lines
4.0 KiB
TypeScript
Raw Normal View History

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
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 });
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",
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;
},
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",
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) {
this.timeout(5000);
2016-09-04 22:34:09 +00:00
testSmartsocketClient.serverCall("testFunction1", {
value1: "hello"
}).then((dataArg) => {
console.log(dataArg);
done();
});
});
2016-09-04 22:34:09 +00:00
it("should be able to make a functionCall from server to client", function (done) {
this.timeout(5000);
let targetSocket = (() => {
2016-09-04 22:34:09 +00:00
return smartsocket.allSocketConnections.find((socketConnectionArg) => {
return socketConnectionArg.alias === "testClient1";
});
})();
2016-09-04 22:34:09 +00:00
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
});