Skip to content
Myr

app.param.js

This file

Path

test/app.param.js

366 lines · 8.4 KiB · JavaScript

Last changed by

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

History of this file

Take it

The walk stopped after 200 commits: lines older than dd2b897 point at it.

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 1–31why, one step back
1
2var 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')

tests: use supertest expect for simple assertionsDouglas Christopher Wilsonbefore adoptiondc8acc8

lines 32–33why, one step back
32 .expect(200, 'tj', function (err) {
33 if (err) return done(err)

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 34–59why, one step back
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;

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

line 60why, one step back
60 id.should.be.a.Number()

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 61–65why, one step back
61 res.send('' + id);
62 });6364 app.get('/user/:uid', function(req, res){65 var id = req.params.id;

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

line 66why, one step back
66 id.should.be.a.Number()

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 67–71why, one step back
67 res.send('' + id);
68 });6970 request(app)71 .get('/user/123')

tests: use supertest expect for simple assertionsDouglas Christopher Wilsonbefore adoptiondc8acc8

lines 72–73why, one step back
72 .expect(200, '123', function (err) {
73 if (err) return done(err)

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 74–93why, one step back
74 request(app)
75 .get('/post/123')76 .expect('123', done);77 })78 })79 })8081 describe('.param(name, fn)', function(){82 it('should map logic for a single param', function(done){83 var app = express();8485 app.param('id', function(req, res, next, id){86 id = Number(id);87 if (isNaN(id)) return next('route');88 req.params.id = id;89 next();90 });9192 app.get('/user/:id', function(req, res){93 var id = req.params.id;

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

line 94why, one step back
94 id.should.be.a.Number()

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 95–366why, one step back
95 res.send('' + id);
96 });9798 request(app)99 .get('/user/123')100 .expect('123', done);101 })102103 it('should only call once per request', function(done) {104 var app = express();105 var called = 0;106 var count = 0;107108 app.param('user', function(req, res, next, user) {109 called++;110 req.user = user;111 next();112 });113114 app.get('/foo/:user', function(req, res, next) {115 count++;116 next();117 });118 app.get('/foo/:user', function(req, res, next) {119 count++;120 next();121 });122 app.use(function(req, res) {123 res.end([count, called, req.user].join(' '));124 });125126 request(app)127 .get('/foo/bob')128 .expect('2 1 bob', done);129 })130131 it('should call when values differ', function(done) {132 var app = express();133 var called = 0;134 var count = 0;135136 app.param('user', function(req, res, next, user) {137 called++;138 req.users = (req.users || []).concat(user);139 next();140 });141142 app.get('/:user/bob', function(req, res, next) {143 count++;144 next();145 });146 app.get('/foo/:user', function(req, res, next) {147 count++;148 next();149 });150 app.use(function(req, res) {151 res.end([count, called, req.users.join(',')].join(' '));152 });153154 request(app)155 .get('/foo/bob')156 .expect('2 2 foo,bob', done);157 })158159 it('should support altering req.params across routes', function(done) {160 var app = express();161162 app.param('user', function(req, res, next, user) {163 req.params.user = 'loki';164 next();165 });166167 app.get('/:user', function(req, res, next) {168 next('route');169 });170 app.get('/:user', function(req, res, next) {171 res.send(req.params.user);172 });173174 request(app)175 .get('/bob')176 .expect('loki', done);177 })178179 it('should not invoke without route handler', function(done) {180 var app = express();181182 app.param('thing', function(req, res, next, thing) {183 req.thing = thing;184 next();185 });186187 app.param('user', function(req, res, next, user) {188 next(new Error('invalid invokation'));189 });190191 app.post('/:user', function(req, res, next) {192 res.send(req.params.user);193 });194195 app.get('/:thing', function(req, res, next) {196 res.send(req.thing);197 });198199 request(app)200 .get('/bob')201 .expect(200, 'bob', done);202 })203204 it('should work with encoded values', function(done){205 var app = express();206207 app.param('name', function(req, res, next, name){208 req.params.name = name;209 next();210 });211212 app.get('/user/:name', function(req, res){213 var name = req.params.name;214 res.send('' + name);215 });216217 request(app)218 .get('/user/foo%25bar')219 .expect('foo%bar', done);220 })221222 it('should catch thrown error', function(done){223 var app = express();224225 app.param('id', function(req, res, next, id){226 throw new Error('err!');227 });228229 app.get('/user/:id', function(req, res){230 var id = req.params.id;231 res.send('' + id);232 });233234 request(app)235 .get('/user/123')236 .expect(500, done);237 })238239 it('should catch thrown secondary error', function(done){240 var app = express();241242 app.param('id', function(req, res, next, val){243 process.nextTick(next);244 });245246 app.param('id', function(req, res, next, id){247 throw new Error('err!');248 });249250 app.get('/user/:id', function(req, res){251 var id = req.params.id;252 res.send('' + id);253 });254255 request(app)256 .get('/user/123')257 .expect(500, done);258 })259260 it('should defer to next route', function(done){261 var app = express();262263 app.param('id', function(req, res, next, id){264 next('route');265 });266267 app.get('/user/:id', function(req, res){268 var id = req.params.id;269 res.send('' + id);270 });271272 app.get('/:name/123', function(req, res){273 res.send('name');274 });275276 request(app)277 .get('/user/123')278 .expect('name', done);279 })280281 it('should defer all the param routes', function(done){282 var app = express();283284 app.param('id', function(req, res, next, val){285 if (val === 'new') return next('route');286 return next();287 });288289 app.all('/user/:id', function(req, res){290 res.send('all.id');291 });292293 app.get('/user/:id', function(req, res){294 res.send('get.id');295 });296297 app.get('/user/new', function(req, res){298 res.send('get.new');299 });300301 request(app)302 .get('/user/new')303 .expect('get.new', done);304 })305306 it('should not call when values differ on error', function(done) {307 var app = express();308 var called = 0;309 var count = 0;310311 app.param('user', function(req, res, next, user) {312 called++;313 if (user === 'foo') throw new Error('err!');314 req.user = user;315 next();316 });317318 app.get('/:user/bob', function(req, res, next) {319 count++;320 next();321 });322 app.get('/foo/:user', function(req, res, next) {323 count++;324 next();325 });326327 app.use(function(err, req, res, next) {328 res.status(500);329 res.send([count, called, err.message].join(' '));330 });331332 request(app)333 .get('/foo/bob')334 .expect(500, '0 1 err!', done)335 });336337 it('should call when values differ when using "next"', function(done) {338 var app = express();339 var called = 0;340 var count = 0;341342 app.param('user', function(req, res, next, user) {343 called++;344 if (user === 'foo') return next('route');345 req.user = user;346 next();347 });348349 app.get('/:user/bob', function(req, res, next) {350 count++;351 next();352 });353 app.get('/foo/:user', function(req, res, next) {354 count++;355 next();356 });357 app.use(function(req, res) {358 res.end([count, called, req.user].join(' '));359 });360361 request(app)362 .get('/foo/bob')363 .expect('1 2 bob', done);364 })365 })366})