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

Ensure application/json on library flows reqs

This commit is contained in:
Nick O'Leary 2014-11-05 22:41:05 +00:00
parent e7eb02fcb7
commit a520240b25
5 changed files with 47 additions and 31 deletions

View File

@ -236,7 +236,12 @@ RED.editor = (function() {
//TODO: move this to RED.library //TODO: move this to RED.library
var flowName = $("#node-input-filename").val(); var flowName = $("#node-input-filename").val();
if (!/^\s*$/.test(flowName)) { if (!/^\s*$/.test(flowName)) {
$.post('library/flows/'+flowName,$("#node-input-filename").attr('nodes'),function() { $.ajax({
url:'library/flows/'+flowName,
type: "POST",
data: $("#node-input-filename").attr('nodes'),
contentType: "application/json; charset=utf-8"
}).done(function() {
RED.library.loadFlowLibrary(); RED.library.loadFlowLibrary();
RED.notify("Saved nodes","success"); RED.notify("Saved nodes","success");
}); });

View File

@ -31,10 +31,10 @@ var errorHandler = function(err,req,res,next) {
function init(adminApp) { function init(adminApp) {
library.init(adminApp);
adminApp.use(express.json()); adminApp.use(express.json());
library.init(adminApp);
// Editor // Editor
if (!settings.disableEditor) { if (!settings.disableEditor) {
adminApp.get("/",ui.ensureSlash); adminApp.get("/",ui.ensureSlash);

View File

@ -47,19 +47,19 @@ function createLibrary(type) {
var path = req.params[0]; var path = req.params[0];
var fullBody = ''; var fullBody = '';
req.on('data', function(chunk) { req.on('data', function(chunk) {
fullBody += chunk.toString(); fullBody += chunk.toString();
}); });
req.on('end', function() { req.on('end', function() {
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() { storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
res.send(204); res.send(204);
}).otherwise(function(err) { }).otherwise(function(err) {
util.log("[red] Error saving library entry '"+path+"' : "+err); util.log("[red] Error saving library entry '"+path+"' : "+err);
if (err.message.indexOf('forbidden') === 0) { if (err.message.indexOf('forbidden') === 0) {
res.send(403); res.send(403);
return; return;
} }
res.send(500); res.send(500);
}); });
}); });
}); });
} }
@ -91,22 +91,16 @@ module.exports = {
}); });
}, },
post: function(req,res) { post: function(req,res) {
//TODO: do content-type properly var flow = JSON.stringify(req.body);
var fullBody = ''; storage.saveFlow(req.params[0],flow).then(function() {
req.on('data', function(chunk) { res.send(204);
fullBody += chunk.toString(); }).otherwise(function(err) {
}); util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
req.on('end', function() { if (err.message.indexOf('forbidden') === 0) {
storage.saveFlow(req.params[0],fullBody).then(function() { res.send(403);
res.send(204); return;
}).otherwise(function(err) { }
util.log("[red] Error loading flow '"+req.params[0]+"' : "+err); res.send(500);
if (err.message.indexOf('forbidden') === 0) {
res.send(403);
return;
}
res.send(500);
});
}); });
} }
} }

View File

@ -103,7 +103,7 @@ describe("library api", function() {
var flow = '[]'; var flow = '[]';
request(app) request(app)
.post('/library/flows/foo') .post('/library/flows/foo')
.set('Content-Type', 'text/plain') .set('Content-Type', 'application/json')
.send(flow) .send(flow)
.expect(204).end(function (err, res) { .expect(204).end(function (err, res) {
if (err) { if (err) {

View File

@ -150,6 +150,23 @@ describe("nodes api", function() {
}); });
}); });
it('returns 400 if request is invalid', function(done) {
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
request(app)
.post('/nodes')
.send({})
.expect(400)
.end(function(err,res) {
settingsAvailable.restore();
if (err) {
throw err;
}
done();
});
});
describe('by module', function() { describe('by module', function() {
it('installs the module and returns node info', function(done) { it('installs the module and returns node info', function(done) {
var settingsAvailable = sinon.stub(settings,'available', function() { var settingsAvailable = sinon.stub(settings,'available', function() {