diff --git a/lib/index.js b/lib/index.js index cd760ad..ac20721 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,10 +10,18 @@ var NOTFOUND = -1; var IDTOKEN = '{:id}'; var defaults = { - stripTrailingSlash: true + stripTrailingSlash: true, + prefix: undefined }; +function stripPrefix(path, prefix) { + if (path.indexOf(prefix) === 0) { + return path.substring(prefix.length); + } + return path; +} + function stripTrailingSlash(path) { // single slash '/' is a valid path, it should be ignored var lastIndex = path.length - 1; @@ -89,8 +97,11 @@ module.exports = function(dbname, inAcl, callback, options) { debug('method: %s url: %s path: %s', req.method, req.url, req.path); var path = req.path; + if (options.prefix) { + path = stripPrefix(path, options.prefix); + } if (options.stripTrailingSlash) { - path = stripTrailingSlash(req.path); + path = stripTrailingSlash(path); } if (!req.connection.pskRole) { diff --git a/test/test-prefix.js b/test/test-prefix.js new file mode 100644 index 0000000..5a5eebf --- /dev/null +++ b/test/test-prefix.js @@ -0,0 +1,110 @@ +'use strict'; + +var request = require('supertest'), + express = require('express'), + fspath = require('path'), + colors = require('colors'), + assert = require('assert'); + +var lib = require(fspath.join(__dirname, '../lib/index')); + +function genericHandlers(router) { + var handlers = require('./handlers2'); + router.get('*', handlers.get); + return router; +} + +var acl = [{ + 'role': 'user', + 'paths': [ + { + 'path': '/', + 'verbs': ['GET'] + }, + { + 'path': '/foo', + 'verbs': ['GET'] + } + ] +}]; + +describe('test-prefix.js - should let all through', function() { + describe('strip prefix - true', function() { + var app, router; + app = express(); + router = express.Router(); + + before(function() { + router.all('*', function(req, res, next) { + req.connection.pskRole = 'user'; + next(); + }); + router.all( + '*', + lib('foobar', acl, function(){}, { + prefix: '/fit' + }) + ); + app.use('/', genericHandlers(router)); + }); + it('/ should be 200', function(done) { + request(app) + .get('/') + .expect(200, done); + }); + it('/fit/ should be 200', function(done) { + request(app) + .get('/fit/') + .expect(200, done); + }); + it('/foo should be 200', function(done) { + request(app) + .get('/foo') + .expect(200, done); + }); + it('/fit/foo should be 200', function(done) { + request(app) + .get('/fit/foo') + .expect(200, done); + }); + }); + describe('strip trailing slash - false', function() { + var app, router; + app = express(); + router = express.Router(); + + before(function() { + router.all('*', function(req, res, next) { + req.connection.pskRole = 'user'; + next(); + }) + router.all( + '*', + lib('foobar', acl, function(){}, { + prefix: undefined + }) + ); + app.use('/', genericHandlers(router)); + }); + it('/ should be 200', function(done) { + request(app) + .get('/') + .expect(200, done); + }); + it('/fit/ should be 401', function(done) { + request(app) + .get('/fit/') + .expect(401, done); + }); + it('/foo should be 200', function(done) { + request(app) + .get('/foo') + .expect(200, done); + }); + it('/fit/foo should be 401', function(done) { + request(app) + .get('/fit/foo') + .expect(401, done); + }); + }); +});