Skip to content
Myr

app.param.js

This file

Path

test/app.param.js

108 lines · 2.4 KiB · JavaScript

Last changed by

added app.param() array testTJ Holowaychukbefore adoptionada2d0c

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

line 44why, one step back
44

added app.param() array testTJ Holowaychukbefore adoptionada2d0c

lines 45–82why, one step back
45 describe('.param(names, fn)', function(){
46 it('should map the array', function(done){47 var app = express();4849 app.param(['id', 'uid'], 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('/post/:id', function(req, res){57 var id = req.params.id;58 id.should.be.a('number');59 res.send('' + id);60 });6162 app.get('/user/:uid', function(req, res){63 var id = req.params.id;64 id.should.be.a('number');65 res.send('' + id);66 });6768 request(app)69 .get('/user/123')70 .end(function(res){71 res.body.should.equal('123');7273 request(app)74 .get('/post/123')75 .end(function(res){76 res.body.should.equal('123');77 done();78 })79 })80 })81 })82

added another app.param() testTJ Holowaychukbefore adoption286d3a1

lines 83–107why, one step back
83 describe('.param(name, fn)', function(){
84 it('should map logic for a single param', function(done){85 var app = express();8687 app.param('id', function(req, res, next, id){88 id = Number(id);89 if (isNaN(id)) return next('route');90 req.params.id = id;91 next();92 });9394 app.get('/user/:id', function(req, res){95 var id = req.params.id;96 id.should.be.a('number');97 res.send('' + id);98 });99100 request(app)101 .get('/user/123')102 .end(function(res){103 res.body.should.equal('123');104 done();105 })106 })107 })

added app.param() testsTJ Holowaychukbefore adoption937f01a

line 108why, one step back
108})