app.param.js
This file
Path
test/app.param.js
160 lines · 3.6 KiB · JavaScript
Last changed by
tests: add param test with encoded valueDouglas Christopher Wilsonbefore adoptiond4a2843
History of this fileTake it
The walk stopped after 200 commits: lines older than a10f695 point at it.
pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 3why, one step back3 , request = require('supertest');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
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 })tests: add more various testsDouglas Christopher Wilsonbefore adoption83b8b7a
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 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 45–59why, 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;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 60why, one step back60 id.should.be.a.Number;pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 61–65why, one step back61 res.send('' + id);62 });6364 app.get('/user/:uid', function(req, res){65 var id = req.params.id;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 66why, one step back66 id.should.be.a.Number;pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 67–94why, one step back67 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;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 95why, one step back95 id.should.be.a.Number;pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 96–101why, one step back96 res.send('' + id);97 });9899 request(app)100 .get('/user/123')101 .expect('123', done);tests: add more various testsDouglas Christopher Wilsonbefore adoption83b8b7a
lines 102–103why, one step back102 })103tests: add param test with encoded valueDouglas Christopher Wilsonbefore adoptiond4a2843
lines 104–121why, one step back104 it('should work with encoded values', function(done){105 var app = express();106107 app.param('name', function(req, res, next, name){108 req.params.name = name;109 next();110 });111112 app.get('/user/:name', function(req, res){113 var name = req.params.name;114 res.send('' + name);115 });116117 request(app)118 .get('/user/foo%25bar')119 .expect('foo%bar', done);120 })121tests: add more various testsDouglas Christopher Wilsonbefore adoption83b8b7a
lines 122–157why, one step back122 it('should catch thrown error', function(done){123 var app = express();124125 app.param('id', function(req, res, next, id){126 throw new Error('err!');127 });128129 app.get('/user/:id', function(req, res){130 var id = req.params.id;131 res.send('' + id);132 });133134 request(app)135 .get('/user/123')136 .expect(500, done);137 })138139 it('should defer to next route', function(done){140 var app = express();141142 app.param('id', function(req, res, next, id){143 next('route');144 });145146 app.get('/user/:id', function(req, res){147 var id = req.params.id;148 res.send('' + id);149 });150151 app.get('/:name/123', function(req, res){152 res.send('name');153 });154155 request(app)156 .get('/user/123')157 .expect('name', done);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 158–160why, one step back158 })159 })160})