1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #537 from dsundberg/websocket-client-unittests

added tests for websocket-client
This commit is contained in:
Nick O'Leary 2015-01-17 22:45:22 +00:00
commit 57b39022d0

View File

@ -21,10 +21,15 @@ var helper = require("../../helper.js");
var websocketNode = require("../../../../nodes/core/io/22-websocket.js");
var sockets = [];
function getWsUrl(path) {
return helper.url().replace(/http/, "ws") + path;
}
function createClient(listenerid) {
return when.promise(function(resolve, reject) {
var node = helper.getNode(listenerid);
var url = helper.url().replace(/http/, "ws") + node.path;
var url = getWsUrl(node.path);
var sock = new ws(url);
sockets.push(sock);
@ -39,12 +44,17 @@ function createClient(listenerid) {
}
function closeAll() {
for(var i=0;i<sockets.length;i++) {
for (var i = 0; i < sockets.length; i++) {
sockets[i].close();
}
sockets = [];
}
function getSocket(listenerid) {
var node = helper.getNode(listenerid);
return node.server;
}
describe('websocket node', function() {
before(function(done) {
@ -56,30 +66,38 @@ describe('websocket node', function() {
helper.unload();
});
it('should be loaded, listener', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }];
describe('websocket-listener', function() {
it('should load', function(done) {
var flow = [{ id: "n1", type: "websocket-listener", path: "/ws" }];
helper.load(websocketNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("path", "/ws");
helper.getNode("n1").should.have.property("path", "/ws");
done();
});
});
it('should be server', function(done) {
var flow = [{ id: "n1", type: "websocket-listener", path: "/ws" }];
helper.load(websocketNode, flow, function() {
helper.getNode("n1").should.have.property('isServer', true);
done();
});
});
it('should handle wholemsg property', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", type:"websocket-listener", wholemsg: "true", path: "/ws2" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket-listener", path: "/ws2", wholemsg: "true" }];
helper.load(websocketNode, flow, function() {
var n1 = helper.getNode("n1");
n1.should.have.property("wholemsg", false);
var n2 = helper.getNode("n2");
n2.should.have.property("wholemsg", true);
helper.getNode("n1").should.have.property("wholemsg", false);
helper.getNode("n2").should.have.property("wholemsg", true);
done();
});
});
it('should create socket', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket in", server: "n1" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
done();
@ -88,7 +106,7 @@ describe('websocket node', function() {
});
it('should close socket on delete', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }];
var flow = [{ id: "n1", type: "websocket-listener", path: "/ws" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
sock.on("close", function(code, msg) {
@ -100,13 +118,13 @@ describe('websocket node', function() {
});
it('should receive data', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
{id:"n3", type: "helper"}];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
var n3 = helper.getNode("n3");
n3.on("input", function(msg) {
createClient("n1").then(function(sock) {
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("payload", "hello");
done();
});
@ -116,14 +134,14 @@ describe('websocket node', function() {
});
it('should receive wholemsg', function(done) {
var flow = [{id:"n1", type:"websocket-listener", wholemsg: "true", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
{id:"n3", type: "helper"}];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
createClient("n1").then(function(sock) {
sock.send('{"text":"hello"}');
var n3 = helper.getNode("n3");
n3.on("input", function(msg) {
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("text", "hello");
done();
});
@ -132,44 +150,48 @@ describe('websocket node', function() {
});
it('should send', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", type:"helper", wires: [["n3"]] },
{id:"n3", server:"n1", type:"websocket out" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "helper", wires: [["n3"]] },
{ id: "n3", type: "websocket out", server: "n1" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
msg.should.equal("hello");
done();
});
var n2 = helper.getNode("n2");
n2.send({payload: "hello"});
helper.getNode("n2").send({
payload: "hello"
});
});
});
});
it('should send wholemsg', function(done) {
var flow = [{id:"n1", type:"websocket-listener", wholemsg: "true", path: "/ws" },
{id:"n2", type:"helper", wires: [["n3"]] },
{id:"n3", server:"n1", type:"websocket out" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{ id: "n2", type: "websocket out", server: "n1" },
{ id: "n3", type: "helper", wires: [["n2"]] }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
var jmsg = JSON.parse(msg);
jmsg.should.have.property("text", "hello");
JSON.parse(msg).should.have.property("text", "hello");
done();
});
var n2 = helper.getNode("n2");
n2.send({text: "hello"});
helper.getNode("n3").send({
text: "hello"
});
});
});
});
it('should echo', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
{id:"n3", server:"n1", type:"websocket out" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
{ id: "n3", type: "websocket out", server: "n1" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
msg.should.equal("hello");
done();
@ -180,14 +202,14 @@ describe('websocket node', function() {
});
it('should echo wholemsg', function(done) {
var flow = [{id:"n1", type:"websocket-listener", wholemsg: "true", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
{id:"n3", server:"n1", type:"websocket out" }];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
{ id: "n3", type: "websocket out", server: "n1" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
var jmsg = JSON.parse(msg);
jmsg.should.have.property("text","hello");
JSON.parse(msg).should.have.property("text", "hello");
done();
});
sock.send('{"text":"hello"}');
@ -196,13 +218,14 @@ describe('websocket node', function() {
});
it('should broadcast', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", server:"n1", type:"websocket out" },
{id:"n3", type: "helper", wires: [["n2"]]}
];
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket out", server: "n1" },
{ id: "n3", type: "helper", wires: [["n2"]] }];
helper.load(websocketNode, flow, function() {
var def1 = when.defer(), def2 = when.defer();
when.all([createClient("n1"), createClient("n1")]).then(function(socks){
var def1 = when.defer(),
def2 = when.defer();
when.all([createClient("n1"), createClient("n1")]).then(function(socks) {
socks[0].on("message", function(msg, flags) {
msg.should.equal("hello");
def1.resolve();
@ -211,13 +234,177 @@ describe('websocket node', function() {
msg.should.equal("hello");
def2.resolve();
});
var n3 = helper.getNode("n3");
n3.send({payload:"hello"});
helper.getNode("n3").send({
payload: "hello"
});
});
when.all([def1.promise, def2.promise]).then(function(){
when.all([def1.promise, def2.promise]).then(function() {
done();
});
});
});
});
describe('websocket-client', function() {
it('should load', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") }];
helper.load(websocketNode, flow, function() {
helper.getNode("n1").should.have.property('path', getWsUrl("/ws"));
done();
});
});
it('should not be server', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") }];
helper.load(websocketNode, flow, function() {
helper.getNode("n1").should.have.property('isServer', false);
done();
});
});
it('should handle wholemsg property', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") },
{ id: "n2", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" }];
helper.load(websocketNode, flow, function() {
helper.getNode("n1").should.have.property("wholemsg", false);
helper.getNode("n2").should.have.property("wholemsg", true);
done();
});
});
it('should connect to server', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket-client", path: getWsUrl("/ws") }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
done();
});
});
});
it('should close on delete', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket-client", path: getWsUrl("/ws") }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.on('close', function() {
done();
});
helper.getNode("n2").close();
});
});
});
it('should receive data', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") },
{ id: "n2", type: "websocket in", client: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.send('hello');
});
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("payload", "hello");
done();
});
});
});
it('should receive wholemsg data ', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
{ id: "n2", type: "websocket in", client: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.send('{"text":"hello"}');
});
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("text", "hello");
done();
});
});
});
it('should send', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") },
{ id: "n2", type: "websocket out", client: "n1" },
{ id: "n3", type: "helper", wires: [["n2"]] }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.on('message', function(msg) {
msg.should.equal("hello");
done();
});
});
getSocket("n1").on("open", function() {
helper.getNode("n3").send({
payload: "hello"
});
});
});
});
it('should send wholemsg', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
{ id: "n2", type: "websocket out", client: "n1" },
{ id: "n3", type: "helper", wires: [["n2"]] }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.on('message', function(msg) {
JSON.parse(msg).should.have.property("text", "hello");
done();
});
});
getSocket("n1").on('open', function(){
helper.getNode("n3").send({
text: "hello"
});
});
});
});
it('should feedback', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{ id: "client", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
{ id: "n1", type: "websocket in", client: "client", wires: [["n2", "output"]] },
{ id: "n2", type: "websocket out", server: "server" },
{ id: "n3", type: "helper", wires: [["n2"]] },
{ id: "output", type: "helper" }];
helper.load(websocketNode, flow, function() {
getSocket('client').on('open', function() {
helper.getNode("n3").send({
payload: "ping"
});
});
var acc = 0;
helper.getNode("output").on("input", function(msg) {
if (acc++ > 20) {
helper.clearFlows();
done();
}
});
});
});
});
});