Skip to content
Myr

app.param.js

This file

Path

test/app.param.js

70 lines · 1.6 KiB · JavaScript

Last changed by

added another app.param() testTJ Holowaychukbefore adoption286d3a1

History of this file

Take it

added app.param() testsTJ Holowaychukbefore adoption937f01a

lines 1–43why, one step back
1
2var 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){11 if (regexp instanceof RegExp) {12 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(res){33 res.body.should.equal('tj');34 request(app)35 .get('/user/123')36 .end(function(res){37 res.should.have.status(404);38 done();39 });40 });4142 })43 })

added another app.param() testTJ Holowaychukbefore adoption286d3a1

lines 44–69why, one step back
44
45 describe('.param(name, fn)', function(){46 it('should map logic for a single param', function(done){47 var app = express();4849 app.param('id', function(req, res, next, id){50 id = Number(id);51 if (isNaN(id)) return next('route');52 req.params.id = id;53 next();54 });5556 app.get('/user/:id', function(req, res){57 var id = req.params.id;58 id.should.be.a('number');59 res.send('' + id);60 });6162 request(app)63 .get('/user/123')64 .end(function(res){65 res.body.should.equal('123');66 done();67 })68 })69 })

added app.param() testsTJ Holowaychukbefore adoption937f01a

line 70why, one step back
70})