mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add unit tests for refactored API modules
This commit is contained in:
@@ -17,6 +17,7 @@ var express = require('express');
|
||||
var fs = require("fs");
|
||||
var events = require("../events");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
|
||||
var redNodes = require("../nodes");
|
||||
var settings = require("../settings");
|
||||
|
@@ -60,6 +60,8 @@ function init(adminApp) {
|
||||
adminApp.get("/library/flows",library.getAll);
|
||||
adminApp.get(new RegExp("/library/flows\/(.*)"),library.get);
|
||||
|
||||
|
||||
// Error Handler
|
||||
adminApp.use(errorHandler);
|
||||
}
|
||||
|
||||
|
@@ -20,48 +20,49 @@ var redApp = null;
|
||||
var storage = require("../storage");
|
||||
|
||||
function createLibrary(type) {
|
||||
|
||||
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
|
||||
var path = req.params[1]||"";
|
||||
storage.getLibraryEntry(type,path).then(function(result) {
|
||||
if (typeof result === "string") {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(result);
|
||||
res.end();
|
||||
} else {
|
||||
res.json(result);
|
||||
}
|
||||
}).otherwise(function(err) {
|
||||
if (err) {
|
||||
util.log("[red] Error loading library entry '"+path+"' : "+err);
|
||||
if (err.message.indexOf('forbidden') === 0) {
|
||||
res.send(403);
|
||||
return;
|
||||
if (redApp) {
|
||||
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
|
||||
var path = req.params[1]||"";
|
||||
storage.getLibraryEntry(type,path).then(function(result) {
|
||||
if (typeof result === "string") {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(result);
|
||||
res.end();
|
||||
} else {
|
||||
res.json(result);
|
||||
}
|
||||
}
|
||||
res.send(404);
|
||||
});
|
||||
});
|
||||
|
||||
redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
|
||||
var path = req.params[0];
|
||||
var fullBody = '';
|
||||
req.on('data', function(chunk) {
|
||||
fullBody += chunk.toString();
|
||||
});
|
||||
req.on('end', function() {
|
||||
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
|
||||
res.send(204);
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error saving library entry '"+path+"' : "+err);
|
||||
}).otherwise(function(err) {
|
||||
if (err) {
|
||||
util.log("[red] Error loading library entry '"+path+"' : "+err);
|
||||
if (err.message.indexOf('forbidden') === 0) {
|
||||
res.send(403);
|
||||
return;
|
||||
}
|
||||
res.send(500);
|
||||
});
|
||||
}
|
||||
res.send(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
|
||||
var path = req.params[0];
|
||||
var fullBody = '';
|
||||
req.on('data', function(chunk) {
|
||||
fullBody += chunk.toString();
|
||||
});
|
||||
req.on('end', function() {
|
||||
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
|
||||
res.send(204);
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error saving library entry '"+path+"' : "+err);
|
||||
if (err.message.indexOf('forbidden') === 0) {
|
||||
res.send(403);
|
||||
return;
|
||||
}
|
||||
res.send(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
init: function(app) {
|
||||
|
@@ -55,7 +55,7 @@ module.exports = {
|
||||
return;
|
||||
}
|
||||
promise.then(function(info) {
|
||||
res.json(info);
|
||||
res.json(info);
|
||||
}).otherwise(function(err) {
|
||||
if (err.code === 404) {
|
||||
res.send(404);
|
||||
@@ -90,7 +90,6 @@ module.exports = {
|
||||
promise.then(function(removedNodes) {
|
||||
res.json(removedNodes);
|
||||
}).otherwise(function(err) {
|
||||
console.log(err.stack);
|
||||
res.send(400,err.toString());
|
||||
});
|
||||
} catch(err) {
|
||||
|
@@ -32,7 +32,7 @@ events.on("node-icon-dir",function(dir) {
|
||||
module.exports = {
|
||||
ensureSlash: function(req,res,next) {
|
||||
if (req.originalUrl.slice(-1) != "/") {
|
||||
res.redirect(req.originalUrl+"/");
|
||||
res.redirect(301,req.originalUrl+"/");
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
|
Reference in New Issue
Block a user