smartsocket/test/test.js

108 lines
8.0 KiB
JavaScript
Raw Normal View History

2016-08-07 12:58:20 +00:00
"use strict";
require("typings-test");
2016-09-04 22:34:09 +00:00
const should = require("should");
2016-08-07 12:58:20 +00:00
const smartsocket = require("../dist/index");
2016-08-14 20:17:55 +00:00
const q = require("q");
const nodehash = require("nodehash");
2016-08-07 12:58:20 +00:00
let testSmartsocket;
2016-08-12 03:57:32 +00:00
let testSmartsocketClient;
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 });
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-08-14 20:17:55 +00:00
describe("class SocketRole", function () {
2016-09-04 22:34:09 +00:00
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({
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 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",
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
});
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 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-09-04 22:34:09 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxNQUFNLFdBQVcsUUFBUSxDQUFDLENBQUM7QUFFbEMsTUFBTyxXQUFXLFdBQVcsZUFBZSxDQUFDLENBQUM7QUFDOUMsTUFBTyxDQUFDLFdBQVcsR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