2 var express = require('../')
3 , request = require('supertest');
5 describe('app', function(){
6 describe('.param(fn)', function(){
7 it('should map app.param(name, ...) logic', function(done){
10 app.param(function(name, regexp){
11 if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557
12 return function(req, res, next, val){
14 if (captures = regexp.exec(String(val))) {
15 req.params[name] = captures[1];
24 app.param(':name', /^([a-zA-Z]+)$/);
26 app.get('/user/:name', function(req, res){
27 res.send(req.params.name);
32 .expect(200, 'tj', function (err) {
33 if (err) return done(err)
41 it('should fail if not given fn', function(){
43 app.param.bind(app, ':name', 'bob').should.throw();
47 describe('.param(names, fn)', function(){
48 it('should map the array', function(done){
51 app.param(['id', 'uid'], function(req, res, next, id){
53 if (isNaN(id)) return next('route');
58 app.get('/post/:id', function(req, res){
59 var id = req.params.id;
60 id.should.be.a.Number()
64 app.get('/user/:uid', function(req, res){
65 var id = req.params.id;
66 id.should.be.a.Number()
72 .expect(200, '123', function (err) {
73 if (err) return done(err)
81 describe('.param(name, fn)', function(){
82 it('should map logic for a single param', function(done){
85 app.param('id', function(req, res, next, id){
87 if (isNaN(id)) return next('route');
92 app.get('/user/:id', function(req, res){
93 var id = req.params.id;
94 id.should.be.a.Number()
100 .expect('123', done);
103 it('should only call once per request', function(done) {
108 app.param('user', function(req, res, next, user) {
114 app.get('/foo/:user', function(req, res, next) {
118 app.get('/foo/:user', function(req, res, next) {
122 app.use(function(req, res) {
123 res.end([count, called, req.user].join(' '));
128 .expect('2 1 bob', done);
131 it('should call when values differ', function(done) {
136 app.param('user', function(req, res, next, user) {
138 req.users = (req.users || []).concat(user);
142 app.get('/:user/bob', function(req, res, next) {
146 app.get('/foo/:user', function(req, res, next) {
150 app.use(function(req, res) {
151 res.end([count, called, req.users.join(',')].join(' '));
156 .expect('2 2 foo,bob', done);
159 it('should support altering req.params across routes', function(done) {
162 app.param('user', function(req, res, next, user) {
163 req.params.user = 'loki';
167 app.get('/:user', function(req, res, next) {
170 app.get('/:user', function(req, res, next) {
171 res.send(req.params.user);
176 .expect('loki', done);
179 it('should not invoke without route handler', function(done) {
182 app.param('thing', function(req, res, next, thing) {
187 app.param('user', function(req, res, next, user) {
188 next(new Error('invalid invokation'));
191 app.post('/:user', function(req, res, next) {
192 res.send(req.params.user);
195 app.get('/:thing', function(req, res, next) {
201 .expect(200, 'bob', done);
204 it('should work with encoded values', function(done){
207 app.param('name', function(req, res, next, name){
208 req.params.name = name;
212 app.get('/user/:name', function(req, res){
213 var name = req.params.name;
218 .get('/user/foo%25bar')
219 .expect('foo%bar', done);
222 it('should catch thrown error', function(done){
225 app.param('id', function(req, res, next, id){
226 throw new Error('err!');
229 app.get('/user/:id', function(req, res){
230 var id = req.params.id;
239 it('should catch thrown secondary error', function(done){
242 app.param('id', function(req, res, next, val){
243 process.nextTick(next);
246 app.param('id', function(req, res, next, id){
247 throw new Error('err!');
250 app.get('/user/:id', function(req, res){
251 var id = req.params.id;
260 it('should defer to next route', function(done){
263 app.param('id', function(req, res, next, id){
267 app.get('/user/:id', function(req, res){
268 var id = req.params.id;
272 app.get('/:name/123', function(req, res){
278 .expect('name', done);
281 it('should defer all the param routes', function(done){
284 app.param('id', function(req, res, next, val){
285 if (val === 'new') return next('route');
289 app.all('/user/:id', function(req, res){
293 app.get('/user/:id', function(req, res){
297 app.get('/user/new', function(req, res){
303 .expect('get.new', done);
306 it('should not call when values differ on error', function(done) {
311 app.param('user', function(req, res, next, user) {
313 if (user === 'foo') throw new Error('err!');
318 app.get('/:user/bob', function(req, res, next) {
322 app.get('/foo/:user', function(req, res, next) {
327 app.use(function(err, req, res, next) {
329 res.send([count, called, err.message].join(' '));
334 .expect(500, '0 1 err!', done)
337 it('should call when values differ when using "next"', function(done) {
342 app.param('user', function(req, res, next, user) {
344 if (user === 'foo') return next('route');
349 app.get('/:user/bob', function(req, res, next) {
353 app.get('/foo/:user', function(req, res, next) {
357 app.use(function(req, res) {
358 res.end([count, called, req.user].join(' '));
363 .expect('1 2 bob', done);