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:
commit
57b39022d0
@ -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);
|
||||||
|
|
||||||
@ -45,6 +50,11 @@ function closeAll() {
|
|||||||
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) {
|
||||||
@ -56,30 +66,38 @@ describe('websocket node', function() {
|
|||||||
helper.unload();
|
helper.unload();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be loaded, listener', function(done) {
|
describe('websocket-listener', function() {
|
||||||
|
it('should load', 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() {
|
||||||
var n1 = helper.getNode("n1");
|
helper.getNode("n1").should.have.property("path", "/ws");
|
||||||
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle wholemsg property', function(done) {
|
it('should handle wholemsg property', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
{id:"n2", type:"websocket-listener", wholemsg: "true", path: "/ws2" }];
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
|
{ id: "n2", type: "websocket-listener", path: "/ws2", wholemsg: "true" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var n1 = helper.getNode("n1");
|
helper.getNode("n1").should.have.property("wholemsg", false);
|
||||||
n1.should.have.property("wholemsg", false);
|
helper.getNode("n2").should.have.property("wholemsg", true);
|
||||||
var n2 = helper.getNode("n2");
|
|
||||||
n2.should.have.property("wholemsg", true);
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create socket', function(done) {
|
it('should create socket', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
{id:"n2", server:"n1", type:"websocket in" }];
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
|
{ id: "n2", type: "websocket in", server: "n1" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
done();
|
done();
|
||||||
@ -100,13 +118,13 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should receive data', function(done) {
|
it('should receive data', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
|
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
|
||||||
{ id: "n3", type: "helper" }];
|
{ id: "n3", type: "helper" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
var n3 = helper.getNode("n3");
|
helper.getNode("n3").on("input", function(msg) {
|
||||||
n3.on("input", function(msg) {
|
|
||||||
msg.should.have.property("payload", "hello");
|
msg.should.have.property("payload", "hello");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -116,14 +134,14 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should receive wholemsg', function(done) {
|
it('should receive wholemsg', 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: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
|
||||||
|
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
|
||||||
{ id: "n3", type: "helper" }];
|
{ id: "n3", type: "helper" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
sock.send('{"text":"hello"}');
|
sock.send('{"text":"hello"}');
|
||||||
var n3 = helper.getNode("n3");
|
helper.getNode("n3").on("input", function(msg) {
|
||||||
n3.on("input", function(msg) {
|
|
||||||
msg.should.have.property("text", "hello");
|
msg.should.have.property("text", "hello");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@ -132,44 +150,48 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should send', function(done) {
|
it('should send', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
{ id: "n2", type: "helper", wires: [["n3"]] },
|
{ id: "n2", type: "helper", wires: [["n3"]] },
|
||||||
{id:"n3", server:"n1", type:"websocket out" }];
|
{ id: "n3", type: "websocket out", server: "n1" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
sock.on("message", function(msg, flags) {
|
sock.on("message", function(msg, flags) {
|
||||||
msg.should.equal("hello");
|
msg.should.equal("hello");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
var n2 = helper.getNode("n2");
|
helper.getNode("n2").send({
|
||||||
n2.send({payload: "hello"});
|
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: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
|
||||||
{id:"n3", server:"n1", type:"websocket out" }];
|
{ id: "n2", type: "websocket out", server: "n1" },
|
||||||
|
{ id: "n3", type: "helper", wires: [["n2"]] }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
sock.on("message", function(msg, flags) {
|
sock.on("message", function(msg, flags) {
|
||||||
var jmsg = JSON.parse(msg);
|
JSON.parse(msg).should.have.property("text", "hello");
|
||||||
jmsg.should.have.property("text", "hello");
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
var n2 = helper.getNode("n2");
|
helper.getNode("n3").send({
|
||||||
n2.send({text: "hello"});
|
text: "hello"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should echo', function(done) {
|
it('should echo', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
{id:"n2", server:"n1", type:"websocket in", wires: [["n3"]] },
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
{id:"n3", server:"n1", type:"websocket out" }];
|
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
|
||||||
|
{ id: "n3", type: "websocket out", server: "n1" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
sock.on("message", function(msg, flags) {
|
sock.on("message", function(msg, flags) {
|
||||||
msg.should.equal("hello");
|
msg.should.equal("hello");
|
||||||
done();
|
done();
|
||||||
@ -180,14 +202,14 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should echo wholemsg', function(done) {
|
it('should echo wholemsg', 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: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
|
||||||
{id:"n3", server:"n1", type:"websocket out" }];
|
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
|
||||||
|
{ id: "n3", type: "websocket out", server: "n1" }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var sock = createClient("n1").then(function(sock) {
|
createClient("n1").then(function(sock) {
|
||||||
sock.on("message", function(msg, flags) {
|
sock.on("message", function(msg, flags) {
|
||||||
var jmsg = JSON.parse(msg);
|
JSON.parse(msg).should.have.property("text", "hello");
|
||||||
jmsg.should.have.property("text","hello");
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
sock.send('{"text":"hello"}');
|
sock.send('{"text":"hello"}');
|
||||||
@ -196,12 +218,13 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should broadcast', function(done) {
|
it('should broadcast', function(done) {
|
||||||
var flow = [{id:"n1", type:"websocket-listener", path: "/ws" },
|
var flow = [
|
||||||
{id:"n2", server:"n1", type:"websocket out" },
|
{ id: "n1", type: "websocket-listener", path: "/ws" },
|
||||||
{id:"n3", type: "helper", wires: [["n2"]]}
|
{ id: "n2", type: "websocket out", server: "n1" },
|
||||||
];
|
{ id: "n3", type: "helper", wires: [["n2"]] }];
|
||||||
helper.load(websocketNode, flow, function() {
|
helper.load(websocketNode, flow, function() {
|
||||||
var def1 = when.defer(), def2 = when.defer();
|
var def1 = when.defer(),
|
||||||
|
def2 = when.defer();
|
||||||
when.all([createClient("n1"), createClient("n1")]).then(function(socks) {
|
when.all([createClient("n1"), createClient("n1")]).then(function(socks) {
|
||||||
socks[0].on("message", function(msg, flags) {
|
socks[0].on("message", function(msg, flags) {
|
||||||
msg.should.equal("hello");
|
msg.should.equal("hello");
|
||||||
@ -211,8 +234,9 @@ describe('websocket node', function() {
|
|||||||
msg.should.equal("hello");
|
msg.should.equal("hello");
|
||||||
def2.resolve();
|
def2.resolve();
|
||||||
});
|
});
|
||||||
var n3 = helper.getNode("n3");
|
helper.getNode("n3").send({
|
||||||
n3.send({payload:"hello"});
|
payload: "hello"
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
when.all([def1.promise, def2.promise]).then(function() {
|
when.all([def1.promise, def2.promise]).then(function() {
|
||||||
@ -221,3 +245,166 @@ describe('websocket node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user