app.param.js
This file
Path
test/app.param.js
261 lines · 5.9 KiB · JavaScript
Last changed by
Fix persistence of req.params from app.paramsDouglas Christopher Wilsonbefore adoption6775658
History of this fileTake it
The walk stopped after 200 commits: lines older than 2bc703c point at it.
Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
line 3why, one step back3 , request = require('supertest');Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c
lines 4–39why, one step back45describe('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 })Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 40–44why, one step back4041 it('should fail if not given fn', function(){42 var app = express();43 app.param.bind(app, ':name', 'bob').should.throw();44 })Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c
lines 45–102why, one step back45 })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 })tests: add more app.param testsDouglas Christopher Wilsonbefore adoptione7e2592
lines 103–157why, one step back103104 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);Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 158–159why, one step back158 })159Fix persistence of req.params from app.paramsDouglas Christopher Wilsonbefore adoption6775658
lines 160–179why, one step back160 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 })179Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 180–197why, one step back180 it('should work with encoded values', function(done){181 var app = express();182183 app.param('name', function(req, res, next, name){184 req.params.name = name;185 next();186 });187188 app.get('/user/:name', function(req, res){189 var name = req.params.name;190 res.send('' + name);191 });192193 request(app)194 .get('/user/foo%25bar')195 .expect('foo%bar', done);196 })197Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 198–233why, one step back198 it('should catch thrown error', function(done){199 var app = express();200201 app.param('id', function(req, res, next, id){202 throw new Error('err!');203 });204205 app.get('/user/:id', function(req, res){206 var id = req.params.id;207 res.send('' + id);208 });209210 request(app)211 .get('/user/123')212 .expect(500, done);213 })214215 it('should defer to next route', function(done){216 var app = express();217218 app.param('id', function(req, res, next, id){219 next('route');220 });221222 app.get('/user/:id', function(req, res){223 var id = req.params.id;224 res.send('' + id);225 });226227 app.get('/:name/123', function(req, res){228 res.send('name');229 });230231 request(app)232 .get('/user/123')233 .expect('name', done);tests: add more app.param testsDouglas Christopher Wilsonbefore adoptione7e2592
line 234why, one step back234 })fix handling of errors from param callbacksDouglas Christopher Wilsonbefore adoptionfb2d918
lines 235–259why, one step back235236 it('should defer all the param routes', function(done){237 var app = express();238239 app.param('id', function(req, res, next, val){240 if (val === 'new') return next('route');241 return next();242 });243244 app.all('/user/:id', function(req, res){245 res.send('all.id');246 });247248 app.get('/user/:id', function(req, res){249 res.send('get.id');250 });251252 app.get('/user/new', function(req, res){253 res.send('get.new');254 });255256 request(app)257 .get('/user/new')258 .expect('get.new', done);259 })Merge pull request #1802 from kapouer/patch-1Jonathan Ongbefore adoption2bc703c
lines 260–261why, one step back260 })261})