UNPKG

koa-validator

Version:
407 lines (393 loc) 13.9 kB
var koa = require('koa') , request = require('supertest') , bodyParser = require('koa-bodyparser') , router = require('koa-router') , validator = require('..') , createApp = function(){ var app = koa(); app.use(bodyParser()); app.use(validator()); app.use(router(app)); return app; } ; describe('validator', function(){ it("these validates should be to ok" , function(done){ var app = createApp() , errors ; app.post('/validate',function*(){ this.checkBody('name').notEmpty().len(3,20); this.checkBody('empty').empty(); this.checkBody('match').matches(/^abc$/i); this.checkBody('integer').isInt(); this.checkBody('float_').isFloat(); this.checkBody('in').in([1,2]); this.checkBody('eq').eq("eq"); this.checkBody('eqeq').eqeq('1'); this.checkBody('neq').neq("eq"); this.checkBody('neqeq').neqeq(1); this.checkBody('number4').gt(3); this.checkBody('number4').lt(5); this.checkBody('number4').ge(4); this.checkBody('number4').le(4); this.checkBody('number4').ge(3); this.checkBody('number4').le(5); this.checkBody('contains').contains("tain"); this.checkBody('notContains').notContains(" "); this.checkBody('email').isEmail(); this.checkBody('url').isUrl(); this.checkBody('ip').isIp(); this.checkBody('alpha').isAlpha(); this.checkBody('numeric').isNumeric(); this.checkBody('an').isAlphanumeric(); this.checkBody('base64').isBase64(); this.checkBody('hex').isHexadecimal(); this.checkBody('color1').isHexColor(); this.checkBody('color2').isHexColor(); this.checkBody('color3').isHexColor(); this.checkBody('color4').isHexColor(); this.checkBody('low').isLowercase(); this.checkBody('up').isUppercase(); this.checkBody('div').isDivisibleBy(3); this.checkBody('n').isNull(); this.checkBody('len').isLength(1,4); this.checkBody('byteLength').byteLength(4,6); this.checkBody('uuid').isUUID(); this.checkBody('date').isDate(); this.checkBody('time').isTime(); this.checkBody('after').isAfter(new Date("2014-08-06")); this.checkBody('before').isBefore(new Date("2014-08-08")); this.checkBody('in').isIn([1, 2]); this.checkBody('credit').isCreditCard(); this.checkBody('isbn').isISBN(); this.checkBody('json').isJSON(); this.checkBody('mb').isMultibyte(); this.checkBody('ascii').isAscii(); this.checkBody('fw').isFullWidth(); this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); if(this.haveValidationError()){ this.body = this.validationErrors(); return; } this.body= 'ok'; }); var req = request(app.listen()); req.post('/validate') .send({ name:"jim", empty:"", email:"jim@gmail.com", len:"len", match:"abc", integer:12, float_:1.23, in:1, eq:"eq", eqeq: '1', neq:'neq', neqeq: '1', number4:'4', contains:"contains", notContains:"notContains", url:"http://www.google.com", ip:'192.168.1.1', alpha:"abxyABXZ", numeric:"3243134", an:"a1b2c3", base64:"aGVsbG8=", hex:"0a1b2c3ef", color1:"#ffffff", color2:"ffffff", color3:"#fff", color4:"fff", low:"hello", up:"HELLO", div:"21", n:"", byteLength:"你好", uuid:"c8162b90-fdda-4803-843b-ed5851480c86", time:"13:12:00", date:"2014-08-07", after:"2014-08-07", before:"2014-08-07", credit:"4063651340421805", isbn:"9787513300711", json:'{"a":1}', mb:"多字节", ascii:"fff", fw:"宽字节", hw:"a字节", vw:"v多字节", sp:'ABC千𥧄1-2-3' }) .expect(200) .expect('ok' ,done); }); it("these validates fail tests should be to ok" , function(done){ var app = createApp() , errors ; app.post('/validate',function*(){ this.checkBody('name').notEmpty().len(3,20); this.checkBody('notEmpty').notEmpty(); this.checkBody('notEmpty').len(2,3); this.checkBody('match').matches(/^abc$/i); this.checkBody('integer').isInt(); this.checkBody('float_').isFloat(); this.checkBody('in').in([1,2]); this.checkBody('eq').eq("eq"); this.checkBody('eqeq').eqeq(1); this.checkBody('neq').neq("eq"); this.checkBody('number4').gt(5); this.checkBody('number4').lt(3); this.checkBody('number4').ge(5); this.checkBody('number4').le(3); this.checkBody('contains').contains("tain"); this.checkBody('notContains').notContains(" "); this.checkBody('email').isEmail(); this.checkBody('url').isUrl(); this.checkBody('ip').isIp(); this.checkBody('alpha').isAlpha(); this.checkBody('numeric').isNumeric(); this.checkBody('an').isAlphanumeric(); this.checkBody('base64').isBase64(); this.checkBody('hex').isHexadecimal(); this.checkBody('color1').isHexColor(); this.checkBody('color2').isHexColor(); this.checkBody('color3').isHexColor(); this.checkBody('color4').isHexColor(); this.checkBody('low').isLowercase(); this.checkBody('up').isUppercase(); this.checkBody('div').isDivisibleBy(3); this.checkBody('n').isNull(); this.checkBody('len').isLength(3,4); this.checkBody('byteLength').byteLength(4,6); this.checkBody('uuid').isUUID(); this.checkBody('time').isTime(); this.checkBody('date').isDate(); this.checkBody('after').isAfter(new Date("2014-08-06")); this.checkBody('before').isBefore(new Date("2014-08-02")); this.checkBody('in').isIn([1,2]); this.checkBody('credit').isCreditCard(); this.checkBody('isbn').isISBN(); this.checkBody('json').isJSON(); this.checkBody('mb').isMultibyte(); this.checkBody('ascii').isAscii(); this.checkBody('fw').isFullWidth(); this.checkBody('hw').isHalfWidth(); this.checkBody('vw').isVariableWidth(); this.checkBody('sp').isSurrogatePair(); if(this.haveValidationError()){ errors = this.validationErrors(); if(errors.length === 49){ this.body = 'ok'; return; }else{ this.body = 'only ' + errors.length + ' errors'; return; } }else{ this.body = 'there is no error'; return; } }); var req = request(app.listen()); req.post('/validate') .send({ name:"j", empty:"fd", email:"jim@@gmail.com", len:"l", match:"xyz", integer:"12a", float_:'a1.23', in:'fd', eq:"neq", eqeq: 1, neq:'eq', number4:'4', contains:"hello" , notContains:"h f", url:"google", ip:'192.168.', alpha:"321", numeric:"fada", an:"__a", base64:"fdsaf", hex:"hgsr", color1:"#fffff", color2:"fffff", color3:"#ff", color4:"ff", low:"Hre", up:"re", div:"22", n:"f", byteLength:"你", uuid:"c8162b90-fdda-4803-843bed5851480c86", date:"2014-0807", time:"24:00:00", after:"2014-08-05", before:"2014-08-02", credit:"4063651340421805332", isbn:"978751330071154", json:'{"a:1}', mb:"fd", ascii:"你好", fw:"43", hw:"你好", vw:"aa", sp:'fdfd' }) .expect(200) .expect('ok' ,done); }); it('there validate query should be to okay' , function(done){ var app = createApp() , errors ; app.get('/query',function*(){ this.checkQuery('name').notEmpty(); this.checkQuery('password').len(3,20); if(this.haveValidationError()){ this.body = errors = this.validationErrors();; return; } this.body = 'ok'; }); request(app.listen()) .get('/query') .query({ name:'jim', password:'yeap' }).expect(200) .expect('ok' , done); }); it('there validate params should be to okay' , function(done){ var app = createApp() , errors ; app.get('/:id',function*(){ this.checkParams('id').isInt(); if(this.haveValidationError()){ this.body = this.validationErrors(); return; } this.body = 'ok'; }); request(app.listen()) .get('/123') .expect(200) .expect('ok' , done); }); it('there sanitizers should be to okay' , function(done){ var app = createApp(); var url ="http://www.google.com/" app.post('/sanitizers',function*(){ this.sanitizeBody('default').default('default'); this.sanitizeBody('int_').toInt(); this.sanitizeBody('float_').toFloat(); this.sanitizeBody('bool').toBoolean(); this.sanitizeBody('date').toDate(); this.sanitizeBody('trim').trim(); this.sanitizeBody('ltrim').ltrim(); this.sanitizeBody('rtrim').rtrim(); this.sanitizeBody('up').toUp(); this.sanitizeBody('low').toLow(); this.sanitizeBody('escape').escape(); this.sanitizeBody('stripLow').stripLow(); this.sanitizeBody('whitelist').whitelist('ll'); this.sanitizeBody('blacklist').blacklist('ll'); this.sanitizeBody('encodeURI').decodeURI(); this.sanitizeBody('decodeURI').encodeURI(); this.sanitizeBody('encodeURIComponent').decodeURIComponent(); this.sanitizeBody('decodeURIComponent').encodeURIComponent(); this.sanitizeBody('rep').replace(',' ,''); var body = this.request.body; if('default' != body.default){ this.throw(500); } if(20 !== body.int_ ){ this.throw(500); } if(1.2 !== body.float_ ){ this.throw(500); } if(true!== body.bool ){ this.throw(500); } if(new Date('2014-01-01').getTime() !== body.date.getTime() ){ this.throw(500); } if('jim'!=body.trim){ this.throw(500); } if('jim '!=body.ltrim){ this.throw(500); } if(' jim'!=body.rtrim){ this.throw(500); } if('JIM'!=body.up){ this.throw(500); } if('jim'!=body.low){ this.throw(500); } if('&lt;div&gt;'!=body.escape){ this.throw(500); } if('abc'!=body.stripLow){ this.throw(500); } if('ll'!=body.whitelist){ this.throw(500); } if('heo'!=body.blacklist){ this.throw(500); } if(encodeURI(url)!=body.decodeURI){ this.throw(500); } if(decodeURI(url)!=body.encodeURI){ this.throw(500); } if(encodeURIComponent(url)!=body.decodeURIComponent){ this.throw(500); } if(decodeURIComponent(url)!=body.encodeURIComponent){ this.throw(500); } if('ab'!=body.rep){ this.throw(500); } this.body = 'ok'; }); request(app.listen()) .post('/sanitizers') .send({ int_:'20', float_:'1.2', bool:'1', date:'2014-01-01', trim:' jim ', ltrim:' jim ', rtrim:' jim ', up:'jim', low:'Jim', escape:'<div>', stripLow:'abc\r', whitelist:'hello', blacklist:'hello', encodeURI:encodeURI(url), decodeURI:url, encodeURIComponent:encodeURIComponent(url), decodeURIComponent:url, rep:'a,b' }).expect(200) .expect('ok' , done); }); });