mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Ensure application/json on library flows reqs
This commit is contained in:
		@@ -236,7 +236,12 @@ RED.editor = (function() {
 | 
			
		||||
                                //TODO: move this to RED.library
 | 
			
		||||
                                var flowName = $("#node-input-filename").val();
 | 
			
		||||
                                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.notify("Saved nodes","success");
 | 
			
		||||
                                    });
 | 
			
		||||
 
 | 
			
		||||
@@ -31,10 +31,10 @@ var errorHandler = function(err,req,res,next) {
 | 
			
		||||
 | 
			
		||||
function init(adminApp) {
 | 
			
		||||
    
 | 
			
		||||
    library.init(adminApp);
 | 
			
		||||
    
 | 
			
		||||
    adminApp.use(express.json());
 | 
			
		||||
    
 | 
			
		||||
    library.init(adminApp);
 | 
			
		||||
 | 
			
		||||
    // Editor
 | 
			
		||||
    if (!settings.disableEditor) {
 | 
			
		||||
        adminApp.get("/",ui.ensureSlash);
 | 
			
		||||
 
 | 
			
		||||
@@ -47,19 +47,19 @@ function createLibrary(type) {
 | 
			
		||||
            var path = req.params[0];
 | 
			
		||||
            var fullBody = '';
 | 
			
		||||
            req.on('data', function(chunk) {
 | 
			
		||||
                    fullBody += chunk.toString();
 | 
			
		||||
                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);
 | 
			
		||||
                    });
 | 
			
		||||
                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);
 | 
			
		||||
                });
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
@@ -91,22 +91,16 @@ module.exports = {
 | 
			
		||||
        });
 | 
			
		||||
    },
 | 
			
		||||
    post: function(req,res) {
 | 
			
		||||
        //TODO: do content-type properly
 | 
			
		||||
        var fullBody = '';
 | 
			
		||||
        req.on('data', function(chunk) {
 | 
			
		||||
            fullBody += chunk.toString();
 | 
			
		||||
        });
 | 
			
		||||
        req.on('end', function() {
 | 
			
		||||
            storage.saveFlow(req.params[0],fullBody).then(function() {
 | 
			
		||||
                res.send(204);
 | 
			
		||||
            }).otherwise(function(err) {
 | 
			
		||||
                util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
 | 
			
		||||
                if (err.message.indexOf('forbidden') === 0) {
 | 
			
		||||
                    res.send(403);
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                res.send(500);
 | 
			
		||||
            });
 | 
			
		||||
        var flow = JSON.stringify(req.body);
 | 
			
		||||
        storage.saveFlow(req.params[0],flow).then(function() {
 | 
			
		||||
            res.send(204);
 | 
			
		||||
        }).otherwise(function(err) {
 | 
			
		||||
            util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
 | 
			
		||||
            if (err.message.indexOf('forbidden') === 0) {
 | 
			
		||||
                res.send(403);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            res.send(500);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -103,7 +103,7 @@ describe("library api", function() {
 | 
			
		||||
            var flow = '[]';
 | 
			
		||||
            request(app)
 | 
			
		||||
                .post('/library/flows/foo')
 | 
			
		||||
                .set('Content-Type', 'text/plain')
 | 
			
		||||
                .set('Content-Type', 'application/json')
 | 
			
		||||
                .send(flow)
 | 
			
		||||
                .expect(204).end(function (err, res) {
 | 
			
		||||
                    if (err) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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() {
 | 
			
		||||
            it('installs the module and returns node info', function(done) {
 | 
			
		||||
                var settingsAvailable = sinon.stub(settings,'available', function() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user