2016-08-07 12:58:20 +00:00
|
|
|
"use strict";
|
|
|
|
require("typings-test");
|
|
|
|
require("should");
|
|
|
|
const smartsocket = require("../dist/index");
|
2016-08-14 20:17:55 +00:00
|
|
|
const q = require("q");
|
2016-08-16 02:48:42 +00:00
|
|
|
const nodehash = require("nodehash");
|
2016-08-07 12:58:20 +00:00
|
|
|
let testSmartsocket;
|
2016-08-12 03:57:32 +00:00
|
|
|
let testSmartsocketClient;
|
2016-08-14 23:38:28 +00:00
|
|
|
let testSocketRole1;
|
2016-08-14 20:17:55 +00:00
|
|
|
let testSocketFunction1;
|
2016-08-12 03:57:32 +00:00
|
|
|
let testConfig = {
|
|
|
|
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 () {
|
2016-08-14 23:38:28 +00:00
|
|
|
testSocketRole1 = new smartsocket.SocketRole({
|
|
|
|
name: "testRole1",
|
2016-08-16 02:48:42 +00:00
|
|
|
passwordHash: nodehash.sha256FromStringSync("testPassword")
|
2016-08-14 23:38:28 +00:00
|
|
|
});
|
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 20:17:55 +00:00
|
|
|
});
|
|
|
|
});
|
2016-08-07 16:59:39 +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 20:17:55 +00:00
|
|
|
});
|
|
|
|
testSmartsocketClient.connect()
|
|
|
|
.then(() => {
|
|
|
|
done();
|
|
|
|
});
|
2016-08-07 12:58:20 +00:00
|
|
|
});
|
2016-08-14 20:17:55 +00:00
|
|
|
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();
|
|
|
|
});
|
2016-08-14 01:25:26 +00:00
|
|
|
});
|
2016-08-14 20:17:55 +00:00
|
|
|
it("2 clients should connect in parallel", function () {
|
2016-08-14 01:25:26 +00:00
|
|
|
});
|
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 12:58:20 +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 16:59:39 +00:00
|
|
|
});
|
2016-08-07 12:58:20 +00:00
|
|
|
});
|
2016-08-16 02:48:42 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUVoQixNQUFPLFdBQVcsV0FBVyxlQUFlLENBQUMsQ0FBQztBQUM5QyxNQUFPLENBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUN4QixNQUFPLFFBQVEsV0FBVyxVQUFVLENBQUMsQ0FBQztBQUV0QyxJQUFJLGVBQXdDLENBQUM7QUFDN0MsSUFBSSxxQkFBb0QsQ0FBQztBQUN6RCxJQUFJLGVBQXVDLENBQUM7QUFDNUMs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