hmpo-form-controller
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -24,2 +24,4 @@ var util = require('util'), | ||
this.validator = dataValidator(this.options.fields); | ||
this.router = require('express').Router(); | ||
}; | ||
@@ -31,17 +33,10 @@ | ||
requestHandler: function () { | ||
return function (req, res, callback) { | ||
var method = req.method.toLowerCase(); | ||
if (typeof this[method] === 'function') { | ||
this[method](req, res, function (err) { | ||
if (err) { | ||
this.errorHandler(err, req, res, callback); | ||
} else { | ||
callback(); | ||
} | ||
}.bind(this)); | ||
} else { | ||
res.send(405, 'Method ' + method + ' not allowed.'); | ||
} | ||
}.bind(this); | ||
this.router.get('/', this.get.bind(this)); | ||
this.router.post('/', this.post.bind(this)); | ||
this.router.use(this.errorHandler.bind(this)); | ||
return this.router; | ||
}, | ||
use: function () { | ||
this.router.use.apply(this.router, arguments); | ||
}, | ||
get: function (req, res, callback) { | ||
@@ -48,0 +43,0 @@ var errors = this.getErrors(req, res); |
{ | ||
"name": "hmpo-form-controller", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -23,2 +23,3 @@ "main": "index.js", | ||
"debug": "^2.1.1", | ||
"express": "^4.12.3", | ||
"moment": "^2.9.0", | ||
@@ -25,0 +26,0 @@ "underscore": "^1.7.0" |
@@ -48,5 +48,6 @@ var Form = require('../../'); | ||
form = new Form({ template: 'index' }); | ||
sinon.stub(form, 'get'); | ||
sinon.stub(form, 'post'); | ||
sinon.stub(form, 'errorHandler'); | ||
sinon.stub(form, 'get').yields(); | ||
sinon.stub(form, 'post').yields(); | ||
// use a spy instead of a stub so that the length is unaffected | ||
sinon.spy(form, 'errorHandler'); | ||
req = request({ | ||
@@ -59,3 +60,2 @@ params: {} | ||
cb = function callback() {}; | ||
handler = form.requestHandler(); | ||
}); | ||
@@ -71,2 +71,3 @@ | ||
req.method = 'GET'; | ||
handler = form.requestHandler(); | ||
handler(req, res, cb); | ||
@@ -79,2 +80,3 @@ form.get.should.have.been.calledWith(req, res); | ||
req.method = 'POST'; | ||
handler = form.requestHandler(); | ||
handler(req, res, cb); | ||
@@ -85,23 +87,26 @@ form.post.should.have.been.calledWith(req, res); | ||
it('sends error in response to put requests', function () { | ||
req.method = 'PUT'; | ||
handler(req, res, cb); | ||
res.send.should.have.been.calledWith(405); | ||
it('calls error handler if method calls back with an error', function (done) { | ||
req.method = 'POST'; | ||
form.post.yields({ error: 'message' }); | ||
handler = form.requestHandler(); | ||
handler(req, res, function () { | ||
form.errorHandler.should.have.been.calledOnce; | ||
form.errorHandler.should.have.been.calledWith({ error: 'message' }, req, res); | ||
form.errorHandler.should.have.been.calledOn(form); | ||
done(); | ||
}); | ||
}); | ||
it('sends error in response to delete requests', function () { | ||
req.method = 'DELETE'; | ||
handler(req, res, cb); | ||
res.send.should.have.been.calledWith(405); | ||
it('calls any additional middlewares before invoking request handlers', function (done) { | ||
var middleware = sinon.stub().yields(); | ||
req.method = 'GET'; | ||
form.use(middleware); | ||
handler = form.requestHandler(); | ||
handler(req, res, function () { | ||
middleware.should.have.been.calledWith(req, res); | ||
middleware.should.have.been.calledBefore(form.get); | ||
done(); | ||
}); | ||
}); | ||
it('calls error handler if method calls back with an error', function () { | ||
req.method = 'POST'; | ||
form.post.yields({ error: 'message' }); | ||
handler(req, res, cb); | ||
form.errorHandler.should.have.been.calledOnce; | ||
form.errorHandler.should.have.been.calledWith({ error: 'message' }, req, res, cb); | ||
form.errorHandler.should.have.been.calledOn(form); | ||
}); | ||
}); | ||
@@ -108,0 +113,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60449
4
+ Addedexpress@^4.12.3
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.1(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.10(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)