app.param.js
This file
Path
test/app.param.js
307 lines · 7.0 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than ad3ca25 point at it.
deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 1–307why, one step back12var express = require('../')3 , request = require('supertest');45describe('app', function(){6 describe('.param(fn)', function(){7 it('should map app.param(name, ...) logic', function(done){8 var app = express();910 app.param(function(name, regexp){11 if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #155712 return function(req, res, next, val){13 var captures;14 if (captures = regexp.exec(String(val))) {15 req.params[name] = captures[1];16 next();17 } else {18 next('route');19 }20 }21 }22 })2324 app.param(':name', /^([a-zA-Z]+)$/);2526 app.get('/user/:name', function(req, res){27 res.send(req.params.name);28 });2930 request(app)31 .get('/user/tj')32 .end(function(err, res){33 res.text.should.equal('tj');34 request(app)35 .get('/user/123')36 .expect(404, done);37 });3839 })4041 it('should fail if not given fn', function(){42 var app = express();43 app.param.bind(app, ':name', 'bob').should.throw();44 })45 })4647 describe('.param(names, fn)', function(){48 it('should map the array', function(done){49 var app = express();5051 app.param(['id', 'uid'], function(req, res, next, id){52 id = Number(id);53 if (isNaN(id)) return next('route');54 req.params.id = id;55 next();56 });5758 app.get('/post/:id', function(req, res){59 var id = req.params.id;60 id.should.be.a.Number;61 res.send('' + id);62 });6364 app.get('/user/:uid', function(req, res){65 var id = req.params.id;66 id.should.be.a.Number;67 res.send('' + id);68 });6970 request(app)71 .get('/user/123')72 .end(function(err, res){73 res.text.should.equal('123');7475 request(app)76 .get('/post/123')77 .expect('123', done);78 })79 })80 })8182 describe('.param(name, fn)', function(){83 it('should map logic for a single param', function(done){84 var app = express();8586 app.param('id', function(req, res, next, id){87 id = Number(id);88 if (isNaN(id)) return next('route');89 req.params.id = id;90 next();91 });9293 app.get('/user/:id', function(req, res){94 var id = req.params.id;95 id.should.be.a.Number;96 res.send('' + id);97 });9899 request(app)100 .get('/user/123')101 .expect('123', done);102 })103104 it('should only call once per request', function(done) {105 var app = express();106 var called = 0;107 var count = 0;108109 app.param('user', function(req, res, next, user) {110 called++;111 req.user = user;112 next();113 });114115 app.get('/foo/:user', function(req, res, next) {116 count++;117 next();118 });119 app.get('/foo/:user', function(req, res, next) {120 count++;121 next();122 });123 app.use(function(req, res) {124 res.end([count, called, req.user].join(' '));125 });126127 request(app)128 .get('/foo/bob')129 .expect('2 1 bob', done);130 })131132 it('should call when values differ', function(done) {133 var app = express();134 var called = 0;135 var count = 0;136137 app.param('user', function(req, res, next, user) {138 called++;139 req.users = (req.users || []).concat(user);140 next();141 });142143 app.get('/:user/bob', function(req, res, next) {144 count++;145 next();146 });147 app.get('/foo/:user', function(req, res, next) {148 count++;149 next();150 });151 app.use(function(req, res) {152 res.end([count, called, req.users.join(',')].join(' '));153 });154155 request(app)156 .get('/foo/bob')157 .expect('2 2 foo,bob', done);158 })159160 it('should support altering req.params across routes', function(done) {161 var app = express();162163 app.param('user', function(req, res, next, user) {164 req.params.user = 'loki';165 next();166 });167168 app.get('/:user', function(req, res, next) {169 next('route');170 });171 app.get('/:user', function(req, res, next) {172 res.send(req.params.user);173 });174175 request(app)176 .get('/bob')177 .expect('loki', done);178 })179180 it('should not invoke without route handler', function(done) {181 var app = express();182183 app.param('thing', function(req, res, next, thing) {184 req.thing = thing;185 next();186 });187188 app.param('user', function(req, res, next, user) {189 next(new Error('invalid invokation'));190 });191192 app.post('/:user', function(req, res, next) {193 res.send(req.params.user);194 });195196 app.get('/:thing', function(req, res, next) {197 res.send(req.thing);198 });199200 request(app)201 .get('/bob')202 .expect(200, 'bob', done);203 })204205 it('should work with encoded values', function(done){206 var app = express();207208 app.param('name', function(req, res, next, name){209 req.params.name = name;210 next();211 });212213 app.get('/user/:name', function(req, res){214 var name = req.params.name;215 res.send('' + name);216 });217218 request(app)219 .get('/user/foo%25bar')220 .expect('foo%bar', done);221 })222223 it('should catch thrown error', function(done){224 var app = express();225226 app.param('id', function(req, res, next, id){227 throw new Error('err!');228 });229230 app.get('/user/:id', function(req, res){231 var id = req.params.id;232 res.send('' + id);233 });234235 request(app)236 .get('/user/123')237 .expect(500, done);238 })239240 it('should catch thrown secondary error', function(done){241 var app = express();242243 app.param('id', function(req, res, next, val){244 process.nextTick(next);245 });246247 app.param('id', function(req, res, next, id){248 throw new Error('err!');249 });250251 app.get('/user/:id', function(req, res){252 var id = req.params.id;253 res.send('' + id);254 });255256 request(app)257 .get('/user/123')258 .expect(500, done);259 })260261 it('should defer to next route', function(done){262 var app = express();263264 app.param('id', function(req, res, next, id){265 next('route');266 });267268 app.get('/user/:id', function(req, res){269 var id = req.params.id;270 res.send('' + id);271 });272273 app.get('/:name/123', function(req, res){274 res.send('name');275 });276277 request(app)278 .get('/user/123')279 .expect('name', done);280 })281282 it('should defer all the param routes', function(done){283 var app = express();284285 app.param('id', function(req, res, next, val){286 if (val === 'new') return next('route');287 return next();288 });289290 app.all('/user/:id', function(req, res){291 res.send('all.id');292 });293294 app.get('/user/:id', function(req, res){295 res.send('get.id');296 });297298 app.get('/user/new', function(req, res){299 res.send('get.new');300 });301302 request(app)303 .get('/user/new')304 .expect('get.new', done);305 })306 })307})