Permalink
Browse files

added filtering on http verb

  • Loading branch information...
1 parent e4e7953 commit 43d8da7ecf692e3ba09c5a10bd97635c8751b666 @mikefrey mikefrey committed Feb 12, 2013
Showing with 77 additions and 12 deletions.
  1. +30 −12 controller/lifecycle.js
  2. +47 −0 test/lifecycleTest.js
@@ -51,20 +51,38 @@ var createExecFunction = module.exports.createExecFunction = function(name) {
function runItem(item, callback) {
var filter = item.filter
if (filter) {
- var format = this.mediaType()
- var action = this.route.action
+ for (var key in filter) {
+ var func = filterFunctions[key]
+ if (func && func(this, filter[key])) return callback()
+ }
+ }
+ item.method.call(this, callback)
+}
+
+var filterFunctions = {
+ format: function(ctlr, format) {
// filter out non matching media type formats
- if (filter.format && !getRegexp(filter.format).test(format)) return callback()
+ var mediaType = ctlr.mediaType()
+ return format && !getRegexp(format).test(mediaType)
+ },
+ except: function(ctlr, except) {
// filter out excepted actions
- if (filter.except && action) {
- if (Array.isArray(filter.except) && ~filter.except.indexOf(action)) return callback()
- if (filter.except == action) return callback()
- }
+ var action = ctlr.route.action
+ if (!action) return false
+ if (Array.isArray(except) && ~except.indexOf(action)) return true
+ return except == action
+ },
+ only: function(ctlr, only) {
// filter out actions not in `only`
- if (filter.only && action) {
- if (Array.isArray(filter.only) && !~filter.only.indexOf(action)) return callback()
- if (!Array.isArray(filter.only) && filter.only != action) return callback()
- }
+ var action = ctlr.route.action
+ if (!action) return false
+ if (Array.isArray(only) && !~only.indexOf(action)) return true
+ return !Array.isArray(only) && only != action
+ },
+ verb: function(ctlr, verb) {
+ var method = ctlr.req.method
+ // if (!method) return false
+ if (Array.isArray(verb) && !~verb.indexOf(method)) return true
+ return !Array.isArray(verb) && verb != method
}
- item.method.call(this, callback)
}
View
@@ -5,13 +5,15 @@ var lifecycle = require('../controller/lifecycle')
function createTestClass() {
var TC = function(){
this.route = {}
+ this.req = {}
}
TC.prototype.mediaType = sinon.spy(function() {
return this.format || 'text/html'
})
TC.prototype.before = lifecycle.createRegFunction('before')
TC.prototype.runBefore = lifecycle.createExecFunction('before')
TC.prototype.method = sinon.spy()
+ TC.prototype.func = sinon.spy()
TC.prototype.asyncMethod = sinon.spy(function(cb) {
process.nextTick(cb)
})
@@ -221,4 +223,49 @@ describe('Lifecycle', function() {
})
+ describe('Verb filter', function() {
+
+ beforeEach(function(done) {
+ TestClass = createTestClass()
+ done()
+ })
+
+ it('should not filter if not present', function(done) {
+ var tc = new TestClass()
+ tc.req.method = 'GET'
+ tc.before('func', {})
+ tc.runBefore()
+ assert(tc.func.calledOnce)
+ done()
+ })
+
+ it('should not filter if verbs match', function(done) {
+ var tc = new TestClass()
+ tc.req.method = 'GET'
+ tc.before('func', { verb:['GET'] })
+ tc.runBefore()
+ assert(tc.func.calledOnce)
+ done()
+ })
+
+ it('should filter if verbs do not match', function(done) {
+ var tc = new TestClass()
+ tc.req.method = 'GET'
+ tc.before('func', { verb:['POST'] })
+ tc.runBefore()
+ assert(!tc.func.called)
+ done()
+ })
+
+ it('should filter if request.method unset', function(done) {
+ var tc = new TestClass()
+ tc.req.method = ''
+ tc.before('func', { verb:['POST'] })
+ tc.runBefore()
+ assert(!tc.func.called)
+ done()
+ })
+
+ })
+
})

0 comments on commit 43d8da7

Please sign in to comment.