Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
added prefix support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-aladev committed Aug 1, 2016
1 parent 983b3a9 commit 0bd49c4
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
110 changes: 110 additions & 0 deletions test/test-prefix.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 0bd49c4

Please sign in to comment.