From bf1ed75350fef64a607d3e364cbc8c23797fb5a3 Mon Sep 17 00:00:00 2001 From: Steffen Leistner Date: Wed, 25 Mar 2015 08:58:45 +0100 Subject: [PATCH 1/3] Add route validation handler. --- jive-sdk-service/lib/baseSetup.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/jive-sdk-service/lib/baseSetup.js b/jive-sdk-service/lib/baseSetup.js index 42cc802..bf165f6 100644 --- a/jive-sdk-service/lib/baseSetup.js +++ b/jive-sdk-service/lib/baseSetup.js @@ -96,7 +96,7 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) { if ( typeof candidate == 'object' && candidate['verb'] && candidate['route'] ) { // its a valid handler var path = candidate['path'] || key; - + var method; if ( path !== '/' ) { if ( path.indexOf('/') === 0 ) { // in this case of /something @@ -109,7 +109,13 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) { httpVerb = candidate['verb'] || 'get'; // default to GET verb httpVerb = httpVerb.toLowerCase(); - app[httpVerb](routeContextPath, candidate['route']); + method = app[httpVerb]; + + if (candidate.validate) { + method(routeContextPath, candidate.validate, candidate['route']); + } else { + method(routeContextPath, candidate['route']); + } // lock the route if its marked to be locked if ( candidate['jiveLocked'] ) { @@ -268,4 +274,4 @@ exports.setupServices = function( app, definitionName, svcDir, setupEventListene return exports.legalServiceFileExtensions.indexOf(path.extname( currentFsItem ) ) > -1; } ); -}; \ No newline at end of file +}; From a29efd4d33affa42ac529289fbaff09e17604499 Mon Sep 17 00:00:00 2001 From: Steffen Leistner Date: Wed, 25 Mar 2015 19:40:19 +0100 Subject: [PATCH 2/3] Change method assignment. --- jive-sdk-service/lib/baseSetup.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/jive-sdk-service/lib/baseSetup.js b/jive-sdk-service/lib/baseSetup.js index bf165f6..92b8542 100644 --- a/jive-sdk-service/lib/baseSetup.js +++ b/jive-sdk-service/lib/baseSetup.js @@ -96,7 +96,7 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) { if ( typeof candidate == 'object' && candidate['verb'] && candidate['route'] ) { // its a valid handler var path = candidate['path'] || key; - var method; + if ( path !== '/' ) { if ( path.indexOf('/') === 0 ) { // in this case of /something @@ -109,12 +109,11 @@ exports.setupRoutes = function(app, definitionName, routesPath, prefix) { httpVerb = candidate['verb'] || 'get'; // default to GET verb httpVerb = httpVerb.toLowerCase(); - method = app[httpVerb]; if (candidate.validate) { - method(routeContextPath, candidate.validate, candidate['route']); + app[httpVerb](routeContextPath, candidate.validate, candidate['route']); } else { - method(routeContextPath, candidate['route']); + app[httpVerb](routeContextPath, candidate['route']); } // lock the route if its marked to be locked From 83b0fcd9496fafb896e51d7cbde38f6a423447b3 Mon Sep 17 00:00:00 2001 From: Steffen Leistner Date: Sat, 9 May 2015 14:48:36 +0200 Subject: [PATCH 3/3] Test validation middleware option. --- .../samplelist/backend/routes/validate.js | 13 +++++++++ test/unit/setup/testcases/routes.js | 27 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/resources/services/tile_routes/tiles/samplelist/backend/routes/validate.js 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(); + }); + }); + }); + }); }); });