Skip to content

Commit

Permalink
Route id. Closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Nov 21, 2014
1 parent 33e16f9 commit e915cda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ exports.Router = internals.Router = function (options) {

this.settings = Hoek.applyToDefaults(internals.defaults, options || {});

this.routes = {}; // Key: HTTP method or * for catch-all, value: sorted array of routes
this.vhosts = null; // {} where Key: hostname, value: see this.routes
this.routes = {}; // Key: HTTP method or * for catch-all, value: sorted array of routes
this.ids = {}; // Key: route id, value: record
this.vhosts = null; // {} where Key: hostname, value: see this.routes

this.specials = {
badRequest: null,
Expand All @@ -44,7 +45,7 @@ internals.Router.prototype.add = function (config, route) {
}

var table = (vhost === '*' ? self.routes : self.vhosts[vhost]);
table[method] = table[method] || { fingerprints: {}, router: new Router() };
table[method] = table[method] || { routes: [], router: new Router() };

var analysis = config.analysis || this.analyze(config.path);
var record = {
Expand All @@ -56,16 +57,21 @@ internals.Router.prototype.add = function (config, route) {
settings: this.settings
};

// Add route and fingerprint
// Add route

table[method].fingerprints[record.fingerprint] = record;
table[method].routes.push(record);
table[method].router.add(analysis.segments, record);

var last = record.segments[record.segments.length - 1];
if (last.empty) {
table[method].router.add(analysis.segments.slice(0, -1), record);
}

if (config.id) {
Hoek.assert(!this.ids[config.id], 'Route id', config.id, 'for path', config.path, 'conflicts with existing path', this.ids[config.id] && this.ids[config.id].path);
this.ids[config.id] = record;
}

return record;
};

Expand Down Expand Up @@ -250,10 +256,8 @@ internals.Router.prototype.table = function (host) {

Object.keys(table).forEach(function (method) {

var fingerprints = Object.keys(table[method].fingerprints);
fingerprints.forEach(function (fingerprint) {
table[method].routes.forEach(function (record) {

var record = table[method].fingerprints[fingerprint];
result.push(record.route);
});
});
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ describe('Router', function () {

describe('add()', function () {

it('adds a route with id', function (done) {

var router = new Call.Router();
router.add({ method: 'get', path: '/a/b/{c}', id: 'a' });
expect(router.ids.a.path).to.equal('/a/b/{c}');
done();
});

it('throws on duplicate route', function (done) {

var router = new Call.Router();
Expand All @@ -164,6 +172,18 @@ describe('Router', function () {
done();
});

it('throws on duplicate route (id)', function (done) {

var router = new Call.Router();
router.add({ method: 'get', path: '/a/b', id: '1' });
expect(function () {

router.add({ method: 'get', path: '/b', id: '1' });
}).to.throw('Route id 1 for path /b conflicts with existing path /a/b');

done();
});

it('throws on duplicate route (optional param in first)', function (done) {

var router = new Call.Router();
Expand Down

0 comments on commit e915cda

Please sign in to comment.