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 websocketNode = require("../../../../nodes/core/io/22-websocket.js");
var sockets = []; var sockets = [];
function getWsUrl(path) {
return helper.url().replace(/http/, "ws") + path;
}
function createClient(listenerid) { function createClient(listenerid) {
return when.promise(function(resolve, reject) { return when.promise(function(resolve, reject) {
var node = helper.getNode(listenerid); var node = helper.getNode(listenerid);
var url = helper.url().replace(/http/, "ws") + node.path; var url = getWsUrl(node.path);
var sock = new ws(url); var sock = new ws(url);
sockets.push(sock); sockets.push(sock);
@ -39,184 +44,366 @@ function createClient(listenerid) {
} }
function closeAll() { function closeAll() {
for(var i=0;i<sockets.length;i++) { for (var i = 0; i < sockets.length; i++) {
sockets[i].close(); sockets[i].close();
} }
sockets = []; sockets = [];
} }
function getSocket(listenerid) {
var node = helper.getNode(listenerid);
return node.server;
}
describe('websocket node', function() { describe('websocket node', function() {
before(function(done) { before(function(done) {
helper.startServer(done); helper.startServer(done);
}); });
afterEach(function() { afterEach(function() {
closeAll(); closeAll();
helper.unload(); helper.unload();
}); });
it('should be loaded, listener', function(done) { describe('websocket-listener', function() {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }]; it('should load', function(done) {
helper.load(websocketNode, flow, function() { var flow = [{ id: "n1", type: "websocket-listener", path: "/ws" }];
var n1 = helper.getNode("n1"); helper.load(websocketNode, flow, function() {
n1.should.have.property("path", "/ws"); helper.getNode("n1").should.have.property("path", "/ws");
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" }];
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);
done();
});
});
it('should create socket', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
{id:"n2", server:"n1", type:"websocket in" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
done(); done();
}); });
}); });
});
it('should close socket on delete', function(done) { it('should be server', 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() { helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) { helper.getNode("n1").should.have.property('isServer', true);
sock.on("close", function(code, msg) { done();
});
});
it('should handle wholemsg property', function(done) {
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "websocket-listener", path: "/ws2", 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 create socket', function(done) {
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();
});
});
});
it('should close socket on delete', function(done) {
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) {
done();
});
helper.clearFlows();
});
});
});
it('should receive data', function(done) {
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() {
createClient("n1").then(function(sock) {
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("payload", "hello");
done();
});
sock.send("hello");
});
});
});
it('should receive wholemsg', function(done) {
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() {
createClient("n1").then(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: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "helper", wires: [["n3"]] },
{ id: "n3", type: "websocket out", server: "n1" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
msg.should.equal("hello");
done();
});
helper.getNode("n2").send({
payload: "hello"
});
});
});
});
it('should send wholemsg', function(done) {
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() {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
JSON.parse(msg).should.have.property("text", "hello");
done();
});
helper.getNode("n3").send({
text: "hello"
});
});
});
});
it('should echo', function(done) {
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() {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
msg.should.equal("hello");
done();
});
sock.send("hello");
});
});
});
it('should echo wholemsg', function(done) {
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() {
createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
JSON.parse(msg).should.have.property("text", "hello");
done();
});
sock.send('{"text":"hello"}');
});
});
});
it('should broadcast', function(done) {
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) {
socks[0].on("message", function(msg, flags) {
msg.should.equal("hello");
def1.resolve();
});
socks[1].on("message", function(msg, flags) {
msg.should.equal("hello");
def2.resolve();
});
helper.getNode("n3").send({
payload: "hello"
});
});
when.all([def1.promise, def2.promise]).then(function() {
done(); done();
}); });
helper.clearFlows();
}); });
}); });
}); });
it('should receive data', function(done) { describe('websocket-client', function() {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }, it('should load', function(done) {
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] }, var flow = [
{id:"n3", type: "helper"}]; { id: "server", type: "websocket-listener", path: "/ws" },
helper.load(websocketNode, flow, function() { { id: "n1", type: "websocket-client", path: getWsUrl("/ws") }];
var sock = createClient("n1").then(function(sock) { helper.load(websocketNode, flow, function() {
var n3 = helper.getNode("n3"); helper.getNode("n1").should.have.property('path', getWsUrl("/ws"));
n3.on("input", function(msg) { 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"); msg.should.have.property("payload", "hello");
done(); done();
}); });
sock.send("hello");
}); });
}); });
});
it('should receive wholemsg', function(done) { it('should receive wholemsg data ', function(done) {
var flow = [{id:"n1", type:"websocket-listener", wholemsg: "true", path: "/ws" }, var flow = [
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] }, { id: "server", type: "websocket-listener", path: "/ws" },
{id:"n3", type: "helper"}]; { id: "n1", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
helper.load(websocketNode, flow, function() { { id: "n2", type: "websocket in", client: "n1", wires: [["n3"]] },
var sock = createClient("n1").then(function(sock) { { id: "n3", type: "helper" }];
sock.send('{"text":"hello"}'); helper.load(websocketNode, flow, function() {
var n3 = helper.getNode("n3"); getSocket('server').on('connection', function(sock) {
n3.on("input", function(msg) { sock.send('{"text":"hello"}');
});
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("text", "hello"); msg.should.have.property("text", "hello");
done(); done();
}); });
}); });
}); });
});
it('should send', function(done) { it('should send', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }, var flow = [
{id:"n2", type:"helper", wires: [["n3"]] }, { id: "server", type: "websocket-listener", path: "/ws" },
{id:"n3", server:"n1", type:"websocket out" }]; { id: "n1", type: "websocket-client", path: getWsUrl("/ws") },
helper.load(websocketNode, flow, function() { { id: "n2", type: "websocket out", client: "n1" },
var sock = createClient("n1").then(function(sock) { { id: "n3", type: "helper", wires: [["n2"]] }];
sock.on("message", function(msg, flags) { helper.load(websocketNode, flow, function() {
msg.should.equal("hello"); getSocket('server').on('connection', function(sock) {
done(); sock.on('message', function(msg) {
msg.should.equal("hello");
done();
});
});
getSocket("n1").on("open", function() {
helper.getNode("n3").send({
payload: "hello"
});
}); });
var n2 = helper.getNode("n2");
n2.send({payload: "hello"});
}); });
}); });
});
it('should send wholemsg', function(done) { it('should send wholemsg', function(done) {
var flow = [{id:"n1", type:"websocket-listener", wholemsg: "true", path: "/ws" }, var flow = [
{id:"n2", type:"helper", wires: [["n3"]] }, { id: "server", type: "websocket-listener", path: "/ws" },
{id:"n3", server:"n1", type:"websocket out" }]; { id: "n1", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
helper.load(websocketNode, flow, function() { { id: "n2", type: "websocket out", client: "n1" },
var sock = createClient("n1").then(function(sock) { { id: "n3", type: "helper", wires: [["n2"]] }];
sock.on("message", function(msg, flags) { helper.load(websocketNode, flow, function() {
var jmsg = JSON.parse(msg); getSocket('server').on('connection', function(sock) {
jmsg.should.have.property("text", "hello"); sock.on('message', function(msg) {
done(); JSON.parse(msg).should.have.property("text", "hello");
done();
});
});
getSocket("n1").on('open', function(){
helper.getNode("n3").send({
text: "hello"
});
}); });
var n2 = helper.getNode("n2");
n2.send({text: "hello"});
}); });
}); });
});
it('should echo', function(done) { it('should feedback', function(done) {
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" }, var flow = [
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] }, { id: "server", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{id:"n3", server:"n1", type:"websocket out" }]; { id: "client", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
helper.load(websocketNode, flow, function() { { id: "n1", type: "websocket in", client: "client", wires: [["n2", "output"]] },
var sock = createClient("n1").then(function(sock) { { id: "n2", type: "websocket out", server: "server" },
sock.on("message", function(msg, flags) { { id: "n3", type: "helper", wires: [["n2"]] },
msg.should.equal("hello"); { id: "output", type: "helper" }];
done(); helper.load(websocketNode, flow, function() {
getSocket('client').on('open', function() {
helper.getNode("n3").send({
payload: "ping"
});
}); });
sock.send("hello"); var acc = 0;
}); helper.getNode("output").on("input", function(msg) {
}); if (acc++ > 20) {
}); helper.clearFlows();
done();
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" }];
helper.load(websocketNode, flow, function() {
var sock = createClient("n1").then(function(sock) {
sock.on("message", function(msg, flags) {
var jmsg = JSON.parse(msg);
jmsg.should.have.property("text","hello");
done();
}); });
sock.send('{"text":"hello"}');
});
});
});
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"]]}
];
helper.load(websocketNode, flow, function() {
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();
});
socks[1].on("message", function(msg, flags) {
msg.should.equal("hello");
def2.resolve();
});
var n3 = helper.getNode("n3");
n3.send({payload:"hello"});
});
when.all([def1.promise, def2.promise]).then(function(){
done();
}); });
}); });
}); });