Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hmpo-form-controller

Package Overview
Dependencies
Maintainers
3
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hmpo-form-controller - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

23

lib/form.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc