This commit is contained in:
gongwenxin
2025-08-28 16:20:24 +08:00
parent 61e533cddf
commit 73e887ebf4
1539 changed files with 360926 additions and 0 deletions
@@ -0,0 +1,364 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require('should');
var fs = require('fs-extra');
var helper = require('../../editor_helper');
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = '/api';
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('working with data formats', function () {
it('convert to/from JSON', function () {
var injectNode1 = workspace.addNode('inject');
var jsonNode1 = workspace.addNode('json');
var debugNode1 = workspace.addNode('debug');
injectNode1.edit();
injectNode1.setPayload('str', '{"a":1}');
injectNode1.clickOk();
jsonNode1.edit();
jsonNode1.setProperty('payload');
jsonNode1.clickOk();
injectNode1.connect(jsonNode1);
jsonNode1.connect(debugNode1);
var injectNode2 = workspace.addNode('inject');
var jsonNode2 = workspace.addNode('json');
var debugNode2 = workspace.addNode('debug');
injectNode2.edit();
injectNode2.setPayload('json', '{"a":1}');
injectNode2.clickOk();
jsonNode2.edit();
jsonNode2.setProperty('payload');
jsonNode2.clickOk();
injectNode2.connect(jsonNode2);
jsonNode2.connect(debugNode2);
workspace.deploy();
debugTab.open();
injectNode1.clickLeftButton();
debugTab.getMessage().should.eql('1');
debugTab.clearMessage();
injectNode2.clickLeftButton();
debugTab.getMessage().should.eql('"{"a":1}"');
});
it('convert to/from XML', function () {
var injectNode1 = workspace.addNode('inject', 0);
var templateNode1 = workspace.addNode('template', 200);
var xmlNode1 = workspace.addNode('xml', 400);
var debugNode1 = workspace.addNode('debug', 600);
injectNode1.edit();
injectNode1.setPayload('str', '{"a":1}');
injectNode1.clickOk();
templateNode1.edit();
templateNode1.setFormat('text');
templateNode1.setSyntax('plain');
templateNode1.setTemplate('<note priority="high">'
+ ' <to>Nick</to>'
+ ' <from>Dave</from>'
+ ' <heading>Reminder</heading>'
+ ' <body>Update the website</body>'
+ '</note>');
templateNode1.clickOk();
xmlNode1.edit();
xmlNode1.setProperty('payload');
xmlNode1.clickOk();
injectNode1.connect(templateNode1);
templateNode1.connect(xmlNode1);
xmlNode1.connect(debugNode1);
var injectNode2 = workspace.addNode('inject');
var xmlNode2 = workspace.addNode('xml');
var debugNode2 = workspace.addNode('debug');
injectNode2.edit();
injectNode2.setPayload('json', '{'
+ ' "note": {'
+ ' "$": { "priority": "high" },'
+ ' "to": [ "Nick" ],'
+ ' "from": [ "Dave" ],'
+ ' "heading": [ "Reminder" ],'
+ ' "body": [ "Update the website" ]'
+ ' }'
+ '}');
injectNode2.clickOk();
xmlNode2.edit();
xmlNode2.setProperty('payload');
xmlNode2.clickOk();
injectNode2.connect(xmlNode2);
xmlNode2.connect(debugNode2);
workspace.deploy();
debugTab.open();
injectNode1.clickLeftButton();
debugTab.getMessage().should.eql('object');
debugTab.clearMessage();
injectNode2.clickLeftButton();
debugTab.getMessage().should.eql('"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
+ '<note priority="high">'
+ '<to>Nick</to>'
+ '<from>Dave</from>'
+ '<heading>Reminder</heading>'
+ '<body>Update the website</body>'
+ '</note>"');
});
it('convert to/from YAML', function () {
var injectNode1 = workspace.addNode('inject', 0);
var templateNode1 = workspace.addNode('template', 200);
var yamlNode1 = workspace.addNode('yaml', 400);
var debugNode1 = workspace.addNode('debug', 600);
injectNode1.edit();
injectNode1.setPayload('str', '{"a":1}');
injectNode1.clickOk();
templateNode1.edit();
templateNode1.setFormat('yaml');
templateNode1.setSyntax('plain');
templateNode1.setTemplate('a: 1\n'
+ 'b:\n'
+ ' - 1\n'
+ '- 2\n'
+ '- 3');
templateNode1.clickOk();
yamlNode1.edit();
yamlNode1.setProperty('payload');
yamlNode1.clickOk();
injectNode1.connect(templateNode1);
templateNode1.connect(yamlNode1);
yamlNode1.connect(debugNode1);
var injectNode2 = workspace.addNode('inject');
var yamlNode2 = workspace.addNode('yaml');
var debugNode2 = workspace.addNode('debug');
injectNode2.edit();
injectNode2.setPayload('json', '{"a":1, "b":[1,2,3]}');
injectNode2.clickOk();
yamlNode2.edit();
yamlNode2.setProperty('payload');
yamlNode2.clickOk();
injectNode2.connect(yamlNode2);
yamlNode2.connect(debugNode2);
workspace.deploy();
debugTab.open();
injectNode1.clickLeftButton();
debugTab.getMessage().should.eql([ '1', 'array[3]' ]);
debugTab.clearMessage();
injectNode2.clickLeftButton();
debugTab.getMessage().should.eql('"a: 1↵b:↵ - 1↵ - 2↵ - 3↵"');
});
it('generate CSV output', function () {
var injectNode1 = workspace.addNode('inject', 0);
var changeNode1 = workspace.addNode('change', 200);
var csvNode1 = workspace.addNode('csv', 400);
var debugNode1 = workspace.addNode('debug', 600);
changeNode1.edit();
changeNode1.ruleSet('payload', 'msg', '{'
+ ' "a": $floor(100*$random()),'
+ ' "b": $floor(100*$random()),'
+ ' "c": $floor(100*$random())'
+ '}', 'jsonata');
changeNode1.clickOk();
csvNode1.edit();
csvNode1.setColumns('a,b,c');
csvNode1.clickOk();
injectNode1.connect(changeNode1);
changeNode1.connect(csvNode1);
csvNode1.connect(debugNode1);
var injectNode2 = workspace.addNode('inject', 0, 80);
var changeNode2 = workspace.addNode('change', 200, 80);
var csvNode2 = workspace.addNode('csv', 400, 80);
var debugNode2 = workspace.addNode('debug', 600, 80);
changeNode2.edit();
changeNode2.ruleSet('payload', 'msg', '['
+ ' {'
+ ' "a": $floor(100*$random()),'
+ ' "b": $floor(100*$random()),'
+ ' "c": $floor(100*$random())'
+ ' }, {'
+ ' "a": $floor(100*$random()),'
+ ' "b": $floor(100*$random()),'
+ ' "c": $floor(100*$random())'
+ ' }, {'
+ ' "a": $floor(100*$random()),'
+ ' "b": $floor(100*$random()),'
+ ' "c": $floor(100*$random())'
+ ' }, {'
+ ' "a": $floor(100*$random()),'
+ ' "b": $floor(100*$random()),'
+ ' "c": $floor(100*$random())'
+ ' }'
+ ']', 'jsonata');
changeNode2.clickOk();
csvNode2.edit();
csvNode2.setColumns('a,b,c');
csvNode2.setIncludeRow(true);
csvNode2.clickOk();
injectNode2.connect(changeNode2);
changeNode2.connect(csvNode2);
csvNode2.connect(debugNode2);
workspace.deploy();
debugTab.open();
injectNode1.clickLeftButton();
debugTab.getMessage().should.match(/^"([1-9]?[0-9],){2}[1-9]?[0-9]↵"$/);
debugTab.clearMessage();
injectNode2.clickLeftButton();
debugTab.getMessage().should.match(/^"(([1-9]?[0-9],){2}[1-9]?[0-9]↵){4}"$/);
});
it('parse CSV input', function () {
var injectNode = workspace.addNode('inject');
var templateNode = workspace.addNode('template');
var csvNode = workspace.addNode('csv');
var debugNode = workspace.addNode('debug');
templateNode.edit();
templateNode.setFormat('handlebars');
templateNode.setSyntax('mustache');
templateNode.setTemplate('# This is some random data\n'
+ 'a,b,c\n'
+ '80,18,2\n'
+ '52,36,10\n'
+ '91,18,61\n'
+ '32,47,65');
templateNode.clickOk();
csvNode.edit();
csvNode.setSkipLines(1);
csvNode.setFirstRow4Names(true);
csvNode.setOutput('mult');
csvNode.clickOk();
injectNode.connect(templateNode);
templateNode.connect(csvNode);
csvNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql([ 'object', 'object', 'object', 'object' ]);
});
it('simple GET request', function () {
var injectNode = workspace.addNode('inject');
var httpRequestNode = workspace.addNode('httpRequest');
var htmlNode = workspace.addNode('html');
var debugNode = workspace.addNode('debug');
httpRequestNode.edit();
httpRequestNode.setMethod('GET');
httpRequestNode.setUrl('https://nodered.org');
httpRequestNode.clickOk();
htmlNode.edit();
htmlNode.setSelector('.node-red-latest-version');
htmlNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(htmlNode);
htmlNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.match(/^"v[0-9]+\.[0-9]+\.[0-9]"$/);
});
it('split text into one message per line', function () {
var injectNode = workspace.addNode('inject');
var templateNode = workspace.addNode('template');
var splitNode = workspace.addNode('split');
var changeNode = workspace.addNode('change');
var joinNode = workspace.addNode('join');
var debugNode = workspace.addNode('debug');
templateNode.edit();
templateNode.setFormat('handlebars');
templateNode.setSyntax('mustache');
templateNode.setTemplate('one\ntwo\nthree\nfour\nfive');
templateNode.clickOk();
changeNode.edit();
changeNode.ruleSet('payload', 'msg', '(parts.index+1) & ": " & payload', 'jsonata');
changeNode.clickOk();
injectNode.connect(templateNode);
templateNode.connect(splitNode);
splitNode.connect(changeNode);
changeNode.connect(joinNode);
joinNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"1: one↵2: two↵3: three↵4: four↵5: five"');
});
});
});
@@ -0,0 +1,74 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require('should');
var fs = require('fs-extra');
var helper = require('../../editor_helper');
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = '/api';
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('messages', function () {
it('trigger a flow when a node throws an error', function () {
var injectNode = workspace.addNode('inject');
var functionNode = workspace.addNode('function');
var catchNode = workspace.addNode('catch', 0 , 80);
var debugNode = workspace.addNode('debug');
functionNode.edit();
functionNode.setFunction('node.error("an example error", msg);');
functionNode.clickOk();
catchNode.edit();
catchNode.setScope(functionNode);
catchNode.clickOk();
debugNode.edit();
debugNode.setOutput('error');
debugNode.clickOk();
injectNode.connect(functionNode);
catchNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql(['"an example error"', 'function']);
});
// skip this case since the flow outputs random results.
it.skip('automatically retry an action after an error');
});
});
@@ -0,0 +1,81 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require('should');
var fs = require('fs-extra');
var helper = require('../../editor_helper');
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = '/api';
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('flow control', function () {
it('trigger a flow whenever Node-RED starts', function () {
var injectNode = workspace.addNode('inject');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setPayload('str', 'Started!');
injectNode.setOnce(true);
injectNode.clickOk();
injectNode.connect(debugNode);
debugTab.open();
workspace.deploy();
debugTab.getMessage().should.eql('"Started!"');
});
it('trigger a flow at regular intervals', function () {
var injectNode = workspace.addNode('inject');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setRepeat('interval');
injectNode.setRepeatInterval(1);
injectNode.clickOk();
injectNode.connect(debugNode);
workspace.deploy();
debugTab.open();
specUtil.pause(1000);
var t1 = Number(debugTab.getMessage(1));
t1.should.within(1500000000000, 3000000000000);
specUtil.pause(1000);
debugTab.getMessage(2).should.within(t1 + 900, 3000000000000);
});
// skip this case since it needs up to one minite.
it.skip('trigger a flow at a specific time');
});
});
@@ -0,0 +1,572 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var helper = require("../../editor_helper");
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var httpNodeRoot = "/api";
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('HTTP endpoints', function () {
it('create an HTTP endpoint', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello World!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello');
httpRequestNode.setMethod("GET");
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello World!').should.not.eql(-1);
});
it('handle query parameters passed to an HTTP endpoint', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-query");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello {{req.query.name}}!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-query?name=Nick');
httpRequestNode.setMethod("GET");
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello Nick!').should.not.eql(-1);
});
it('handle url parameters in an HTTP endpoint', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-param/:name");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello {{req.params.name}}!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-param/Dave');
httpRequestNode.setMethod("GET");
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello Dave!').should.not.eql(-1);
});
it('access HTTP request headers', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-headers");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>User agent: {{req.headers.user-agent}}</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject", 0, 100);
var changeNode = workspace.addNode("change");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
changeNode.edit();
changeNode.ruleSet("headers", "msg", '{"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}', "json");
changeNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-headers');
httpRequestNode.setMethod("GET");
httpRequestNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Mozilla').should.not.eql(-1);
});
it('include data captured in another flow', function () {
var injectNodeTimestamp = workspace.addNode("inject");
var changeNodeStore = workspace.addNode("change");
var httpInNode = workspace.addNode("httpIn", 0, 100);
var changeNodeCopy = workspace.addNode("change");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
injectNodeTimestamp.edit();
injectNodeTimestamp.setPayload("date");
injectNodeTimestamp.clickOk();
changeNodeStore.edit();
changeNodeStore.ruleSet("timestamp", "flow", "payload", "msg");
changeNodeStore.clickOk();
injectNodeTimestamp.connect(changeNodeStore);
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-data");
httpInNode.clickOk();
changeNodeCopy.edit();
changeNodeCopy.ruleSet("timestamp", "msg", "timestamp", "flow");
changeNodeCopy.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Time: {{ timestamp }}</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(changeNodeCopy);
changeNodeCopy.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNodeCheck = workspace.addNode("inject", 0, 300);
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setMethod("GET");
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-data');
httpRequestNode.clickOk();
injectNodeCheck.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNodeTimestamp.clickLeftButton();
injectNodeCheck.clickLeftButton();
var index = debugTab.getMessage().indexOf('Time: ') + 6;
debugTab.getMessage().substring(index, index + 13).should.within(1500000000000, 3000000000000);
});
it('serve JSON content', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var changeNode = workspace.addNode("change");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-json");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate('{ "Hello": "World" }');
templateNode.clickOk();
changeNode.edit();
changeNode.ruleSet("headers", "msg", "{}", "json", "1");
changeNode.addRule();
changeNode.ruleSet("headers.content-type", "msg", "application/json", "str", "2");
changeNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(changeNode);
changeNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject", 0, 200);
var httpRequestNode = workspace.addNode("httpRequest");
var changeNodeCheck = workspace.addNode("change");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setMethod("GET");
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-json');
httpRequestNode.clickOk();
changeNodeCheck.edit();
changeNodeCheck.ruleSet("payload", "msg", "headers.content-type", "msg", "1");
changeNodeCheck.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(changeNodeCheck);
changeNodeCheck.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
var messages = debugTab.getMessage();
messages.indexOf('application/json').should.not.eql(-1);
});
it('serve a local file', function () {
var httpInNode = workspace.addNode("httpIn");
var fileInNode = workspace.addNode("fileIn");
var changeNode = workspace.addNode("change", 200, 100);
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("get");
httpInNode.setUrl("/hello-file");
httpInNode.clickOk();
fileInNode.edit();
fileInNode.setFilename("test/resources/file-in-node/test.txt");
fileInNode.setOutput("");
fileInNode.clickOk();
changeNode.edit();
changeNode.ruleSet("headers", "msg", "{}", "json");
changeNode.addRule();
changeNode.ruleSet("headers.content-type", "msg", "text/plain", "str", "2");
changeNode.clickOk();
httpInNode.connect(fileInNode);
fileInNode.connect(changeNode);
changeNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject", 0, 200);
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-file');
httpRequestNode.setMethod("GET");
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Text file').should.not.eql(-1);
});
it('post raw data to a flow', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("post");
httpInNode.setUrl("/hello-raw");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello {{ payload }}!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
injectNode.edit();
injectNode.setPayload("str", "Nick");
injectNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-raw');
httpRequestNode.setMethod("POST");
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello Nick!').should.not.eql(-1);
});
it('post form data to a flow', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("post");
httpInNode.setUrl("/hello-form");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello {{ payload.name }}!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject", 0, 100);
var changeNode = workspace.addNode("change");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
injectNode.edit();
injectNode.setPayload("str", "name=Nick");
injectNode.clickOk();
changeNode.edit();
changeNode.ruleSet("headers", "msg", '{"content-type":"application/x-www-form-urlencoded"}', "json");
changeNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-form');
httpRequestNode.setMethod("POST");
httpRequestNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello Nick!').should.not.eql(-1);
});
it('post JSON data to a flow', function () {
var httpInNode = workspace.addNode("httpIn");
var templateNode = workspace.addNode("template");
var httpResponseNode = workspace.addNode("httpResponse");
httpInNode.edit();
httpInNode.setMethod("post");
httpInNode.setUrl("/hello-json");
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate("<html>\n<head></head>\n<body>\n<h1>Hello {{ payload.name }}!</h1>\n</body>\n</html>");
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation starts from here.
var injectNode = workspace.addNode("inject", 0, 100);
var changeNode = workspace.addNode("change");
var httpRequestNode = workspace.addNode("httpRequest");
var debugNode = workspace.addNode("debug");
injectNode.edit();
injectNode.setPayload("json", '{"name":"Nick"}');
injectNode.clickOk();
changeNode.edit();
changeNode.ruleSet("headers", "msg", '{"content-type":"application/json"}', "json");
changeNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/hello-json');
httpRequestNode.setMethod("POST");
httpRequestNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().indexOf('Hello Nick!').should.not.eql(-1);
});
it('work with cookies', function () {
var httpInNodeFormat = workspace.addNode("httpIn");
var functionNodeFormat = workspace.addNode("function", 240);
var templateNode = workspace.addNode("template", 400);
var httpResponseNode = workspace.addNode("httpResponse", 600);
var httpInNodeAdd = workspace.addNode("httpIn", 0, 100);
var functionNodeAdd = workspace.addNode("function", 240);
var changeNode = workspace.addNode("change", 400);
var httpInNodeClear = workspace.addNode("httpIn", 0, 200);
var functionNodeClear = workspace.addNode("function", 250);
httpInNodeFormat.edit();
httpInNodeFormat.setMethod("get");
httpInNodeFormat.setUrl("/hello-cookie");
httpInNodeFormat.clickOk();
functionNodeFormat.edit();
functionNodeFormat.setFunction("msg.payload = JSON.stringify(msg.req.cookies,null,4);\nreturn msg;");
functionNodeFormat.clickOk();
templateNode.edit();
templateNode.setSyntax("mustache");
templateNode.setFormat("handlebars");
templateNode.setTemplate('<html>\n<head></head>\n<body>\n<h1>Cookies</h1>\n<p></p><a href="hello-cookie/add">Add a cookie</a> &bull; <a href="hello-cookie/clear">Clear cookies</a></p>\n<pre>{{ payload }}</pre>\n</body>\n</html>');
templateNode.clickOk();
httpInNodeFormat.connect(functionNodeFormat);
functionNodeFormat.connect(templateNode);
templateNode.connect(httpResponseNode);
httpInNodeAdd.edit();
httpInNodeAdd.setMethod("get");
httpInNodeAdd.setUrl("/hello-cookie/add");
httpInNodeAdd.clickOk();
functionNodeAdd.edit();
functionNodeAdd.setFunction('msg.cookies = { };\n msg.cookies["demo-"+(Math.floor(Math.random()*1000))] = Date.now();\nreturn msg;');
functionNodeAdd.clickOk();
changeNode.edit();
changeNode.ruleSet("statusCode", "msg", "302", "num");
changeNode.addRule();
changeNode.ruleSet("headers", "msg", "{}", "json", "2");
changeNode.addRule();
changeNode.ruleSet("headers.location", "msg", httpNodeRoot + "/hello-cookie", "str", "3");
changeNode.clickOk();
httpInNodeAdd.connect(functionNodeAdd);
functionNodeAdd.connect(changeNode);
changeNode.connect(httpResponseNode);
httpInNodeClear.edit();
httpInNodeClear.setMethod("get");
httpInNodeClear.setUrl("/hello-cookie/clear");
httpInNodeClear.clickOk();
functionNodeClear.edit();
functionNodeClear.setFunction("var cookieNames = Object.keys(msg.req.cookies).filter(function(cookieName) { return /^demo-/.test(cookieName);});\nmsg.cookies = {};\n\ncookieNames.forEach(function(cookieName) {\n msg.cookies[cookieName] = null;\n});\nreturn msg;\n");
functionNodeClear.clickOk();
httpInNodeClear.connect(functionNodeClear);
functionNodeClear.connect(changeNode);
workspace.deploy();
// This case cannot be checked since http request node does not transfer cookies when redirected.
});
});
});
@@ -0,0 +1,300 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require('should');
var fs = require('fs-extra');
var helper = require('../../editor_helper');
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = '/api';
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('HTTP requests', function () {
it('simple get request', function () {
var injectNode = workspace.addNode('inject');
var httpRequestNode = workspace.addNode('httpRequest');
var htmlNode = workspace.addNode('html');
var debugNode = workspace.addNode('debug');
httpRequestNode.edit();
httpRequestNode.setMethod('GET');
httpRequestNode.setUrl(helper.url());
httpRequestNode.clickOk();
htmlNode.edit();
htmlNode.setSelector('title');
htmlNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(htmlNode);
htmlNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"Node-RED"');
});
it('set the URL of a request', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setPayload('str', helper.url());
injectNode.clickOk();
changeNode.edit();
changeNode.ruleSet('url', 'msg', 'payload', 'msg');
changeNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.containEql('<title>Node-RED</title>');
});
it('set the URL of a request using a template', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setPayload('str', 'settings');
injectNode.clickOk();
changeNode.edit();
changeNode.ruleSet('query', 'msg', 'payload', 'msg');
changeNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + '/{{{query}}}');
httpRequestNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.containEql('httpNodeRoot');
});
it('set the query string parameters', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setPayload('str', 'Nick');
injectNode.clickOk();
changeNode.edit();
changeNode.ruleSet('query', 'msg', 'payload', 'msg');
changeNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setUrl(helper.url() + httpNodeRoot + '/set-query?q={{{query}}}');
httpRequestNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation starts from here.
var httpInNode = workspace.addNode('httpIn', 0, 200);
var templateNode = workspace.addNode('template');
var httpResponseNode = workspace.addNode('httpResponse');
httpInNode.edit();
httpInNode.setMethod('get');
httpInNode.setUrl('/set-query');
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax('mustache');
templateNode.setFormat('handlebars');
templateNode.setTemplate('Hello {{req.query.q}}');
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"Hello Nick"');
});
it('get a parsed JSON response', function () {
var injectNode = workspace.addNode('inject');
var changeNodeSetPost = workspace.addNode('change');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setPayload('str', 'json-response');
injectNode.clickOk();
changeNodeSetPost.edit();
changeNodeSetPost.ruleSet('post', 'msg', 'payload', 'msg');
changeNodeSetPost.clickOk();
httpRequestNode.edit();
httpRequestNode.setMethod('GET');
var url = helper.url() + httpNodeRoot + '/{{post}}';
httpRequestNode.setUrl(url);
httpRequestNode.setReturn('obj');
httpRequestNode.clickOk();
debugNode.edit();
debugNode.setOutput('payload.title');
debugNode.clickOk();
injectNode.connect(changeNodeSetPost);
changeNodeSetPost.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation starts from here.
var httpInNode = workspace.addNode('httpIn', 0, 200);
var templateNode = workspace.addNode('template');
var changeNodeSetHeader = workspace.addNode('change');
var httpResponseNode = workspace.addNode('httpResponse');
httpInNode.edit();
httpInNode.setMethod('get');
httpInNode.setUrl('/json-response');
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax('mustache');
templateNode.setFormat('handlebars');
templateNode.setTemplate('{"title": "Hello"}');
templateNode.clickOk();
changeNodeSetHeader.edit();
changeNodeSetHeader.ruleSet('headers', 'msg', '{"content-type":"application/json"}', 'json');
changeNodeSetHeader.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(changeNodeSetHeader);
changeNodeSetHeader.connect(httpResponseNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"Hello"');
});
it('get a binary response', function () {
var injectNode = workspace.addNode('inject');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
httpRequestNode.edit();
httpRequestNode.setMethod('GET');
httpRequestNode.setUrl(helper.url() + '/settings');
httpRequestNode.setReturn('bin');
httpRequestNode.clickOk();
injectNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql(['123', '34', '104', '116', '116', '112', '78', '111', '100', '101']);
});
it('set a request header', function () {
var injectNode = workspace.addNode('inject');
var functionNode = workspace.addNode('function');
var httpRequestNode = workspace.addNode('httpRequest');
var debugNode = workspace.addNode('debug');
functionNode.edit();
functionNode.setFunction('msg.payload = "data to post";\nreturn msg;');
functionNode.clickOk();
httpRequestNode.edit();
httpRequestNode.setMethod('POST');
var url = helper.url() + httpNodeRoot + '/set-header';
httpRequestNode.setUrl(url);
httpRequestNode.clickOk();
injectNode.connect(functionNode);
functionNode.connect(httpRequestNode);
httpRequestNode.connect(debugNode);
// The code for confirmation starts from here.
var httpInNode = workspace.addNode('httpIn', 0, 200);
var templateNode = workspace.addNode('template');
var httpResponseNode = workspace.addNode('httpResponse');
httpInNode.edit();
httpInNode.setMethod('post');
httpInNode.setUrl('/set-header');
httpInNode.clickOk();
templateNode.edit();
templateNode.setSyntax('mustache');
templateNode.setFormat('handlebars');
templateNode.setTemplate('{{ payload }}');
templateNode.clickOk();
httpInNode.connect(templateNode);
templateNode.connect(httpResponseNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"data to post"');
});
});
});
@@ -0,0 +1,142 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require('should');
var fs = require('fs-extra');
var helper = require('../../editor_helper');
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = '/api';
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
helper.startServer();
});
after(function () {
helper.stopServer();
});
describe('messages', function () {
it('set a message property to a fixed value', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var debugNode = workspace.addNode('debug');
changeNode.edit();
changeNode.ruleSet('payload', 'msg', 'Hello World!');
changeNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"Hello World!"');
});
it('delete a message property', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var debugNode = workspace.addNode('debug');
changeNode.edit();
changeNode.ruleDelete();
changeNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('undefined');
});
it('move a message property', function () {
var injectNode = workspace.addNode('inject');
var changeNode = workspace.addNode('change');
var debugNode = workspace.addNode('debug');
injectNode.edit();
injectNode.setTopic('Hello');
injectNode.clickOk();
changeNode.edit();
changeNode.ruleMove('topic', 'msg', 'payload', 'msg');
changeNode.clickOk();
injectNode.connect(changeNode);
changeNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"Hello"');
});
it('map a property between different numeric ranges', function () {
var injectNode1 = workspace.addNode('inject');
var injectNode2 = workspace.addNode('inject', 0, 100);
var injectNode3 = workspace.addNode('inject', 0, 200);
var rangeNode = workspace.addNode('range', 200, 100);
var debugNode = workspace.addNode('debug', 400);
injectNode1.edit();
injectNode1.setPayload('num', 0);
injectNode1.clickOk();
injectNode2.edit();
injectNode2.setPayload('num', 512);
injectNode2.clickOk();
injectNode3.edit();
injectNode3.setPayload('num', 1023);
injectNode3.clickOk();
rangeNode.edit();
rangeNode.setAction('clamp');
rangeNode.setRange(0, 1023, 0, 5);
rangeNode.clickOk();
injectNode1.connect(rangeNode);
injectNode2.connect(rangeNode);
injectNode3.connect(rangeNode);
rangeNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode1.clickLeftButton();
debugTab.getMessage(1).should.eql('0');
injectNode2.clickLeftButton();
debugTab.getMessage(2).should.eql('2.5024437927663734');
injectNode3.clickLeftButton();
debugTab.getMessage(3).should.eql('5');
});
});
});
@@ -0,0 +1,224 @@
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var when = require('when');
var should = require("should");
var fs = require('fs-extra');
var helper = require("../../editor_helper");
var debugTab = require('../../pageobjects/editor/debugTab_page');
var workspace = require('../../pageobjects/editor/workspace_page');
var specUtil = require('../../pageobjects/util/spec_util_page');
var httpNodeRoot = "/api";
var mqttServer;
var mosca = require('mosca');
var moscaSettings = {
port: parseInt(Math.random() * 16383 + 49152),
persistence: {
// Needs for retaining messages.
factory: mosca.persistence.Memory
}
};
// https://cookbook.nodered.org/
describe('cookbook', function () {
beforeEach(function () {
workspace.init();
});
before(function () {
browser.call(function () {
return new Promise(function (resolve, reject) {
mqttServer = new mosca.Server(moscaSettings, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
helper.startServer();
});
after(function () {
browser.call(function () {
return new Promise(function (resolve, reject) {
mqttServer.close(function () {
resolve();
});
});
});
helper.stopServer();
});
describe('MQTT', function () {
it('Add an MQTT broker to prepare for UI test', function () {
var mqttOutNode = workspace.addNode("mqttOut");
mqttOutNode.edit();
mqttOutNode.mqttBrokerNode.edit();
mqttOutNode.mqttBrokerNode.setServer("localhost", moscaSettings.port);
mqttOutNode.mqttBrokerNode.clickOk();
mqttOutNode.clickOk();
workspace.deploy();
});
it('Connect to an MQTT broker', function () {
var injectNode = workspace.addNode("inject");
var mqttOutNode = workspace.addNode("mqttOut");
var mqttInNode = workspace.addNode("mqttIn", 0, 100);
var debugNode = workspace.addNode("debug");
injectNode.edit();
injectNode.setPayload("num", 22);
injectNode.clickOk();
mqttOutNode.edit();
mqttOutNode.setTopic("sensors/livingroom/temp");
mqttOutNode.clickOk();
injectNode.connect(mqttOutNode);
mqttInNode.edit();
mqttInNode.setTopic("sensors/livingroom/temp");
mqttInNode.setQoS("2");
mqttInNode.clickOk();
mqttInNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"22"');
});
// skip this case since it is same as other cases.
it.skip('Publish messages to a topic');
it('Set the topic of a published message', function () {
var injectNode = workspace.addNode("inject");
var mqttOutNode = workspace.addNode("mqttOut");
injectNode.edit();
injectNode.setPayload("num", 22);
injectNode.setTopic("sensors/kitchen/temperature");
injectNode.clickOk();
mqttOutNode.edit();
mqttOutNode.clickOk();
injectNode.connect(mqttOutNode);
// The code for confirmation starts from here.
var mqttInNode = workspace.addNode("mqttIn", 0, 100);
var debugNode = workspace.addNode("debug");
mqttInNode.edit();
mqttInNode.setTopic("sensors/kitchen/temperature");
mqttInNode.clickOk();
mqttInNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql('"22"');
});
it('Publish a retained message to a topic', function () {
var injectNode = workspace.addNode("inject");
var mqttOutNode = workspace.addNode("mqttOut");
injectNode.edit();
injectNode.setPayload("num", 22);
injectNode.clickOk();
mqttOutNode.edit();
mqttOutNode.setTopic("sensors/livingroom/temp");
mqttOutNode.setRetain("true");
mqttOutNode.clickOk();
injectNode.connect(mqttOutNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
// The code for confirmation starts from here.
var mqttInNode = workspace.addNode("mqttIn", 0, 100);
var debugNode = workspace.addNode("debug");
mqttInNode.edit();
mqttInNode.setTopic("sensors/livingroom/temp");
mqttInNode.clickOk();
mqttInNode.connect(debugNode);
// The code for confirmation ends here.
workspace.deploy();
debugTab.open(true);
debugTab.getMessage().should.eql('"22"');
});
// skip this case since it is same as other cases.
it.skip('Subscribe to a topic');
it('Receive a parsed JSON message', function () {
var injectNode = workspace.addNode("inject");
var mqttOutNode = workspace.addNode("mqttOut");
var mqttInNode = workspace.addNode("mqttIn", 0, 100);
var jsonNode = workspace.addNode("json");
var debugNode = workspace.addNode("debug");
injectNode.edit();
injectNode.setPayload("json", '{"sensor_id": 1234, "temperature": 13 }');
injectNode.clickOk();
mqttOutNode.edit();
mqttOutNode.setTopic("sensors/livingroom/temp");
mqttOutNode.clickOk();
injectNode.connect(mqttOutNode);
mqttInNode.edit();
mqttInNode.setTopic("sensors/#");
mqttInNode.setQoS("2");
mqttInNode.clickOk();
jsonNode.edit();
jsonNode.setProperty("payload");
jsonNode.clickOk();
mqttInNode.connect(jsonNode);
jsonNode.connect(debugNode);
workspace.deploy();
debugTab.open();
injectNode.clickLeftButton();
debugTab.getMessage().should.eql(['1234', '13']);
});
});
});