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

Refactor test cases

This commit is contained in:
KatsuyaHoshii 2018-04-23 14:31:37 +09:00
parent d2aa3d1868
commit 6cd9ccc37c
2 changed files with 67 additions and 57 deletions

View File

@ -99,7 +99,6 @@
"grunt-sass": "~2.0.0", "grunt-sass": "~2.0.0",
"grunt-simple-mocha": "~0.4.1", "grunt-simple-mocha": "~0.4.1",
"grunt-webdriver": "^2.0.3", "grunt-webdriver": "^2.0.3",
"hoek": "^4.2.1",
"http-proxy": "^1.16.2", "http-proxy": "^1.16.2",
"istanbul": "0.4.5", "istanbul": "0.4.5",
"mocha": "~3.4.2", "mocha": "~3.4.2",

View File

@ -28,7 +28,6 @@ var httpProxy = require('http-proxy');
var cookieParser = require('cookie-parser'); var cookieParser = require('cookie-parser');
var RED = require("../../../../red/red.js"); var RED = require("../../../../red/red.js");
var fs = require('fs-extra'); var fs = require('fs-extra');
var hoek = require('hoek');
var auth = require('basic-auth'); var auth = require('basic-auth');
describe('HTTP Request Node', function() { describe('HTTP Request Node', function() {
@ -41,7 +40,10 @@ describe('HTTP Request Node', function() {
var testProxyPort = 9200; var testProxyPort = 9200;
//save environment variables //save environment variables
var preEnv; var preEnvHttpProxyLowerCase;
var preEnvHttpProxyUpperCase;
var preEnvNoProxyLowerCase;
var preEnvNoProxyUpperCase;
function startServer(done) { function startServer(done) {
testPort += 1; testPort += 1;
@ -102,14 +104,43 @@ describe('HTTP Request Node', function() {
return "localhost:"+testSslPort+url; return "localhost:"+testSslPort+url;
} }
before(function(done) { function saveProxySetting() {
//save environment variables preEnvHttpProxyLowerCase = process.env.http_proxy;
preEnv = hoek.clone(process.env); preEnvHttpProxyUpperCase = process.env.HTTP_PROXY;
preEnvNoProxyLowerCase = process.env.no_proxy;
preEnvNoProxyUpperCase = process.env.NO_PROXY;
delete process.env.http_proxy;
delete process.env.HTTP_PROXY;
delete process.env.no_proxy;
delete process.env.NO_PROXY;
}
function restoreProxySetting() {
process.env.http_proxy = preEnvHttpProxyLowerCase;
process.env.HTTP_PROXY = preEnvHttpProxyUpperCase;
// On Windows, if environment variable of NO_PROXY that includes lower cases
// such as No_Proxy is replaced with NO_PROXY.
process.env.no_proxy = preEnvNoProxyLowerCase;
process.env.NO_PROXY = preEnvNoProxyUpperCase;
if (preEnvHttpProxyLowerCase == undefined){
delete process.env.http_proxy;
}
if (preEnvHttpProxyUpperCase == undefined){
delete process.env.HTTP_PROXY;
}
if (preEnvNoProxyLowerCase == undefined){
delete process.env.no_proxy;
}
if (preEnvNoProxyUpperCase == undefined){
delete process.env.NO_PROXY;
}
}
before(function(done) {
testApp = express(); testApp = express();
testApp.use(bodyParser.raw({type:"*/*"})); testApp.use(bodyParser.raw({type:"*/*"}));
testApp.use(cookieParser()); testApp.use(cookieParser());
testApp.get('/statusCode204', function(req,res) { res.status(204).end();}) testApp.get('/statusCode204', function(req,res) { res.status(204).end();});
testApp.get('/text', function(req, res){ res.send('hello'); }); testApp.get('/text', function(req, res){ res.send('hello'); });
testApp.get('/redirectToText', function(req, res){ res.status(302).set('Location', getTestURL('/text')).end(); }); testApp.get('/redirectToText', function(req, res){ res.status(302).set('Location', getTestURL('/text')).end(); });
testApp.get('/json-valid', function(req, res){ res.json({a:1}); }); testApp.get('/json-valid', function(req, res){ res.json({a:1}); });
@ -133,7 +164,7 @@ describe('HTTP Request Node', function() {
var result = { var result = {
user: user.name, user: user.name,
pass: user.pass, pass: user.pass,
} };
res.json(result); res.json(result);
}); });
testApp.get('/proxyAuthenticate', function(req, res){ testApp.get('/proxyAuthenticate', function(req, res){
@ -142,21 +173,21 @@ describe('HTTP Request Node', function() {
user: user.name, user: user.name,
pass: user.pass, pass: user.pass,
headers: req.headers headers: req.headers
} };
res.json(result); res.json(result);
}); });
testApp.post('/postInspect', function(req,res) { testApp.post('/postInspect', function(req,res) {
var result = { var result = {
body: req.body.toString(), body: req.body.toString(),
headers: req.headers headers: req.headers
} };
res.json(result); res.json(result);
}); });
testApp.put('/putInspect', function(req,res) { testApp.put('/putInspect', function(req,res) {
var result = { var result = {
body: req.body.toString(), body: req.body.toString(),
headers: req.headers headers: req.headers
} };
res.json(result); res.json(result);
}); });
testApp.delete('/deleteInspect', function(req,res) { res.status(204).end();}); testApp.delete('/deleteInspect', function(req,res) { res.status(204).end();});
@ -165,14 +196,14 @@ describe('HTTP Request Node', function() {
var result = { var result = {
body: req.body.toString(), body: req.body.toString(),
headers: req.headers headers: req.headers
} };
res.json(result); res.json(result);
}); });
testApp.trace('/traceInspect', function(req,res) { testApp.trace('/traceInspect', function(req,res) {
var result = { var result = {
body: req.body.toString(), body: req.body.toString(),
headers: req.headers headers: req.headers
} };
res.json(result); res.json(result);
}); });
testApp.options('/*', function(req,res) { testApp.options('/*', function(req,res) {
@ -184,22 +215,6 @@ describe('HTTP Request Node', function() {
}); });
after(function() { after(function() {
//delete environment variables that were not set
if (!preEnv.http_proxy){
delete process.env.http_proxy;
}
if (!preEnv.HTTP_PROXY){
delete process.env.HTTP_PROXY;
}
if (!preEnv.no_proxy){
delete process.env.no_proxy;
}
if (!preEnv.NO_PROXY){
delete process.env.NO_PROXY;
}
//compare with saved environment variables
process.env.should.be.deepEqual(preEnv);
testServer.close(); testServer.close();
testProxyServer.close(); testProxyServer.close();
}); });
@ -579,11 +594,11 @@ describe('HTTP Request Node', function() {
n1.receive({payload:"foo"}); n1.receive({payload:"foo"});
setTimeout(function() { setTimeout(function() {
if (inError) { if (inError) {
done(new Error("no url allowed though")) done(new Error("no url allowed though"));
} else { } else {
done(); done();
} }
},20) },20);
}); });
}); });
@ -666,11 +681,11 @@ describe('HTTP Request Node', function() {
n1.receive({payload:"foo"}); n1.receive({payload:"foo"});
setTimeout(function() { setTimeout(function() {
if (inError) { if (inError) {
done(new Error("non http(s):// scheme allowed through")) done(new Error("non http(s):// scheme allowed through"));
} else { } else {
done(); done();
} }
},20) },20);
}); });
}); });
@ -687,11 +702,11 @@ describe('HTTP Request Node', function() {
n1.receive({payload:"foo",url:"ftp://foo"}); n1.receive({payload:"foo",url:"ftp://foo"});
setTimeout(function() { setTimeout(function() {
if (inError) { if (inError) {
done(new Error("non http(s):// scheme allowed through")) done(new Error("non http(s):// scheme allowed through"));
} else { } else {
done(); done();
} }
},20) },20);
}); });
}); });
@ -988,7 +1003,7 @@ describe('HTTP Request Node', function() {
var flow = [ var flow = [
{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"txt",url:getSslTestURLWithoutProtocol('/text'),tls:"n3"}, {id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"txt",url:getSslTestURLWithoutProtocol('/text'),tls:"n3"},
{id:"n2", type:"helper"}, {id:"n2", type:"helper"},
{id:"n3", type:"tls-config", cert:"test/resources/ssl/server.crt", key:"test/resources/ssl/server.key", ca:""}]; {id:"n3", type:"tls-config", cert:"test/resources/ssl/server.crt", key:"test/resources/ssl/server.key", ca:"", verifyservercert:false}];
var testNodes = [httpRequestNode, tlsNode]; var testNodes = [httpRequestNode, tlsNode];
helper.load(testNodes, flow, function() { helper.load(testNodes, flow, function() {
var n3 = helper.getNode("n3"); var n3 = helper.getNode("n3");
@ -1014,13 +1029,13 @@ describe('HTTP Request Node', function() {
it('should use http_proxy', function(done) { it('should use http_proxy', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.http_proxy; saveProxySetting();
process.env.http_proxy = "http://localhost:" + testProxyPort; process.env.http_proxy = "http://localhost:" + testProxyPort;
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.http_proxy = preHttpProxy; restoreProxySetting();
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.should.have.property('headers'); msg.payload.should.have.property('headers');
@ -1037,13 +1052,13 @@ describe('HTTP Request Node', function() {
it('should use http_proxy when environment variable is invalid', function(done) { it('should use http_proxy when environment variable is invalid', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.http_proxy; saveProxySetting();
process.env.http_proxy = "invalidvalue"; process.env.http_proxy = "invalidvalue";
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.http_proxy = preHttpProxy; restoreProxySetting();
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.should.have.property('headers'); msg.payload.should.have.property('headers');
@ -1060,13 +1075,13 @@ describe('HTTP Request Node', function() {
it('should use HTTP_PROXY', function(done) { it('should use HTTP_PROXY', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.HTTP_PROXY; saveProxySetting();
process.env.HTTP_PROXY = "http://localhost:" + testProxyPort; process.env.HTTP_PROXY = "http://localhost:" + testProxyPort;
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.HTTP_PROXY = preHttpProxy; restoreProxySetting();
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.should.have.property('headers'); msg.payload.should.have.property('headers');
@ -1083,16 +1098,14 @@ describe('HTTP Request Node', function() {
it('should use no_proxy', function(done) { it('should use no_proxy', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.http_proxy; saveProxySetting();
var preNoProxy = process.env.no_proxy;
process.env.http_proxy = "http://localhost:" + testProxyPort; process.env.http_proxy = "http://localhost:" + testProxyPort;
process.env.no_proxy = "foo,localhost"; process.env.no_proxy = "foo,localhost";
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.http_proxy = preHttpProxy; restoreProxySetting();
process.env.no_proxy = preNoProxy;
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.headers.should.not.have.property('x-testproxy-header','foobar'); msg.payload.headers.should.not.have.property('x-testproxy-header','foobar');
@ -1108,16 +1121,14 @@ describe('HTTP Request Node', function() {
it('should use NO_PROXY', function(done) { it('should use NO_PROXY', function(done) {
var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')}, var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"POST",ret:"obj",url:getTestURL('/postInspect')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.HTTP_PROXY; saveProxySetting();
var preNoProxy = process.env.NO_PROXY;
process.env.HTTP_PROXY = "http://localhost:" + testProxyPort; process.env.HTTP_PROXY = "http://localhost:" + testProxyPort;
process.env.NO_PROXY = "foo,localhost"; process.env.NO_PROXY = "foo,localhost";
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.HTTP_PROXY = preHttpProxy; restoreProxySetting();
process.env.NO_PROXY = preNoProxy;
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.headers.should.not.have.property('x-testproxy-header','foobar'); msg.payload.headers.should.not.have.property('x-testproxy-header','foobar');
@ -1138,7 +1149,7 @@ describe('HTTP Request Node', function() {
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n1.credentials = {user:'userfoo', password:'passwordfoo'} n1.credentials = {user:'userfoo', password:'passwordfoo'};
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
@ -1151,18 +1162,18 @@ describe('HTTP Request Node', function() {
}); });
n1.receive({payload:"foo"}); n1.receive({payload:"foo"});
}); });
}) });
it('should authenticate on proxy server', function(done) { it('should authenticate on proxy server', function(done) {
var flow = [{id:"n1",type:"http request", wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/proxyAuthenticate')}, var flow = [{id:"n1",type:"http request", wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/proxyAuthenticate')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.http_proxy; saveProxySetting();
process.env.http_proxy = "http://foouser:barpassword@localhost:" + testProxyPort; process.env.http_proxy = "http://foouser:barpassword@localhost:" + testProxyPort;
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.http_proxy = preHttpProxy; restoreProxySetting();
try { try {
msg.should.have.property('statusCode',200); msg.should.have.property('statusCode',200);
msg.payload.should.have.property('user', 'foouser'); msg.payload.should.have.property('user', 'foouser');
@ -1181,13 +1192,13 @@ describe('HTTP Request Node', function() {
it('should output an error when proxy authentication was failed', function(done) { it('should output an error when proxy authentication was failed', function(done) {
var flow = [{id:"n1",type:"http request", wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/proxyAuthenticate')}, var flow = [{id:"n1",type:"http request", wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/proxyAuthenticate')},
{id:"n2", type:"helper"}]; {id:"n2", type:"helper"}];
var preHttpProxy = process.env.http_proxy; saveProxySetting();
process.env.http_proxy = "http://xxxuser:barpassword@localhost:" + testProxyPort; process.env.http_proxy = "http://xxxuser:barpassword@localhost:" + testProxyPort;
helper.load(httpRequestNode, flow, function() { helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
process.env.http_proxy = preHttpProxy; restoreProxySetting();
try { try {
msg.should.have.property('statusCode',407); msg.should.have.property('statusCode',407);
msg.headers.should.have.property('proxy-authenticate', 'BASIC realm="test"'); msg.headers.should.have.property('proxy-authenticate', 'BASIC realm="test"');