app.param.js
This file
Path
test/app.param.js
99 lines · 2.3 KiB · JavaScript
Last changed by
fix tests for should.jsJonathan Ongbefore adoption50cb62c
History of this fileTake it
The walk stopped after 200 commits: lines older than 66d9a4a point at it.
Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption66d9a4a
lines 1–10why, one step back12var express = require('../')3 , request = require('./support/http');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){Merge pull request #1566 from daguej/v8-context-fixTJ Holowaychukbefore adoption2db135d
line 11why, one step back11 if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption66d9a4a
lines 12–54why, one step back12 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 })40 })4142 describe('.param(names, fn)', function(){43 it('should map the array', function(done){44 var app = express();4546 app.param(['id', 'uid'], function(req, res, next, id){47 id = Number(id);48 if (isNaN(id)) return next('route');49 req.params.id = id;50 next();51 });5253 app.get('/post/:id', function(req, res){54 var id = req.params.id;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 55why, one step back55 id.should.be.a.Number;Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption66d9a4a
lines 56–60why, one step back56 res.send('' + id);57 });5859 app.get('/user/:uid', function(req, res){60 var id = req.params.id;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 61why, one step back61 id.should.be.a.Number;Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption66d9a4a
lines 62–89why, one step back62 res.send('' + id);63 });6465 request(app)66 .get('/user/123')67 .end(function(err, res){68 res.text.should.equal('123');6970 request(app)71 .get('/post/123')72 .expect('123', done);73 })74 })75 })7677 describe('.param(name, fn)', function(){78 it('should map logic for a single param', function(done){79 var app = express();8081 app.param('id', function(req, res, next, id){82 id = Number(id);83 if (isNaN(id)) return next('route');84 req.params.id = id;85 next();86 });8788 app.get('/user/:id', function(req, res){89 var id = req.params.id;fix tests for should.jsJonathan Ongbefore adoption50cb62c
line 90why, one step back90 id.should.be.a.Number;Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption66d9a4a
lines 91–99why, one step back91 res.send('' + id);92 });9394 request(app)95 .get('/user/123')96 .expect('123', done);97 })98 })99})