diff --git a/jive-sdk-service/lib/baseSetup.js b/jive-sdk-service/lib/baseSetup.js index 12a90a3..905789b 100644 --- a/jive-sdk-service/lib/baseSetup.js +++ b/jive-sdk-service/lib/baseSetup.js @@ -109,7 +109,12 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) { httpVerb = candidate['verb'] || 'get'; // default to GET verb httpVerb = httpVerb.toLowerCase(); - app[httpVerb](routeContextPath, candidate['route']); + + if (candidate.validate) { + app[httpVerb](routeContextPath, candidate.validate, candidate['route']); + } else { + app[httpVerb](routeContextPath, candidate['route']); + } // lock the route if its marked to be locked if ( candidate['jiveLocked'] ) { @@ -280,4 +285,4 @@ exports.setupServices = function( app, definitionName, svcDir, setupEventListene return exports.legalServiceFileExtensions.indexOf(path.extname( currentFsItem ) ) > -1; } ); -}; \ No newline at end of file +}; diff --git a/test/resources/services/tile_routes/tiles/samplelist/backend/routes/validate.js b/test/resources/services/tile_routes/tiles/samplelist/backend/routes/validate.js new file mode 100644 index 0000000..f8b662f --- /dev/null +++ b/test/resources/services/tile_routes/tiles/samplelist/backend/routes/validate.js @@ -0,0 +1,13 @@ +exports.absolute = { + 'verb' : 'get', + 'validate' : function (req, res, next) { + if (req.params.id === 1) { + return next(); + } + res.status(400).end('invalid'); + }, + 'path' : '/validated/:id', + 'route' : function(req, res) { + res.end('locked'); + } +}; diff --git a/test/unit/setup/testcases/routes.js b/test/unit/setup/testcases/routes.js index 3db0737..6a9a0d6 100644 --- a/test/unit/setup/testcases/routes.js +++ b/test/unit/setup/testcases/routes.js @@ -63,6 +63,33 @@ describe('jive', function () { }) }); + it('add validation middleware', function (done) { + var jive = this['jive']; + var testUtils = this['testUtils']; + + var options = testUtils.createBaseServiceOptions('/services/tile_routes'); + delete options['role']; + options['port'] = 5555; options['logLevel'] = 'FATAL'; options['clientUrl'] = 'http://localhost:5555'; + testUtils.setupService(jive, options).then( function(service) { + jive.util.buildRequest('http://localhost:5555/validated/1').then( function(r) { + assert.ok(r['entity']); + return jive.util.buildRequest('http://localhost:5555/validated/unexpected-value'); + }).then( + function(r) { + assert.fail(r, 'expected error'); + }, + + function(e) { + assert.ok(e); + assert.equal(e['statusCode'], 400); + } + ).then( function() { + service.stop().then( function() { + done(); + }); + }); + }); + }); }); });