aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/checkBodyTest.js
diff options
context:
space:
mode:
authornanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
committernanalelfe <nargiza.nosirova@mail.utoronto.ca>2016-07-21 06:29:31 +0000
commitee8e1a13b60a6adfdc691b2a9b57289188397641 (patch)
tree096633208d9b8b6b59b67f4034a0cbb41e1f4c5d /node_modules/express-validator/test/checkBodyTest.js
parent689df70a38ace2f88cfef6ab50f10dc546b48f00 (diff)
need pull
Diffstat (limited to 'node_modules/express-validator/test/checkBodyTest.js')
-rw-r--r--node_modules/express-validator/test/checkBodyTest.js116
1 files changed, 116 insertions, 0 deletions
diff --git a/node_modules/express-validator/test/checkBodyTest.js b/node_modules/express-validator/test/checkBodyTest.js
new file mode 100644
index 0000000..cb14cc6
--- /dev/null
+++ b/node_modules/express-validator/test/checkBodyTest.js
@@ -0,0 +1,116 @@
+var chai = require('chai');
+var expect = chai.expect;
+var request = require('supertest');
+
+var app;
+var errorMessage = 'Parameter is not an integer';
+
+// There are three ways to pass parameters to express:
+// - as part of the URL
+// - as GET parameter in the querystring
+// - as POST parameter in the body
+// These test show that req.checkBody are only interested in req.body values, all other
+// parameters will be ignored.
+
+function validation(req, res) {
+ req.checkBody('testparam', errorMessage).notEmpty().isInt();
+ req.checkBody('arrayParam').isArray();
+
+ var errors = req.validationErrors();
+ if (errors) {
+ return res.send(errors);
+ }
+ res.send({ testparam: req.body.testparam });
+}
+
+function fail(body, length) {
+ expect(body).to.have.length(length);
+ expect(body[0]).to.have.property('msg', errorMessage);
+}
+
+function pass(body) {
+ expect(body).to.have.property('testparam', '42');
+}
+
+function getRoute(path, test, length, done) {
+ request(app)
+ .get(path)
+ .end(function(err, res) {
+ test(res.body, length);
+ done();
+ });
+}
+
+function postRoute(path, data, test, length, done) {
+ request(app)
+ .post(path)
+ .send(data)
+ .end(function(err, res) {
+ test(res.body, length);
+ done();
+ });
+}
+
+// This before() is required in each set of tests in
+// order to use a new validation function in each file
+before(function() {
+ delete require.cache[require.resolve('./helpers/app')];
+ app = require('./helpers/app')(validation);
+});
+
+describe('#checkBody()', function() {
+ describe('GET tests', function() {
+ it('should return three errors when param is missing', function(done) {
+ getRoute('/', fail, 3, done);
+ });
+
+ it('should return three errors when param is present, but not in the body', function(done) {
+ getRoute('/42', fail, 3, done);
+ });
+ });
+
+ describe('POST tests', function() {
+ it('should return three errors when param is missing', function(done) {
+ postRoute('/', null, fail, 3, done);
+ });
+
+ it('should return three errors when param is present, but not in the body', function(done) {
+ postRoute('/42', null, fail, 3, done);
+ });
+
+ // POST only
+
+ it('should return three errors when params are not present', function(done) {
+ postRoute('/test?testparam=gettest', null, fail, 3, done);
+ });
+
+ it('should return three errors when param is present, but not in body', function(done) {
+ postRoute('/42?testparam=42', null, fail, 3, done);
+ });
+
+ it('should return two errors when one param is present, but does not validate', function(done) {
+ postRoute('/42?testparam=42', { testparam: 'posttest' }, fail, 2, done);
+ });
+
+ it('should return a success when params validate on the body', function(done) {
+ postRoute('/?testparam=blah', { testparam: '42', arrayParam: [1, 2, 3] }, pass, null, done);
+ });
+
+ it('should return two errors when two params are present, but do not validate', function(done) {
+ postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: 123 }, fail, 2, done);
+ });
+
+ it('should return two errors when two params are present, but do not validate', function(done) {
+ postRoute('/?testparam=42', { testparam: 'posttest', arrayParam: {} }, fail, 2, done);
+ });
+
+ it('should return two errors when two params are present, but do not validate', function(done) {
+ postRoute('/', { testparam: 'test', arrayParam: '[]' }, fail, 2, done);
+ });
+
+ it('should return a success when params validate on the body', function(done) {
+ postRoute('/', { testparam: '42', arrayParam: [] }, pass, null, done);
+ });
+ });
+
+}); \ No newline at end of file