3 var express
= require('../')
4 , request
= require('supertest');
6 describe('app', function(){
7 describe('.param(names, fn)', function(){
8 it('should map the array', function(done
){
11 app
.param(['id', 'uid'], function(req
, res
, next
, id
){
13 if (isNaN(id
)) return next('route');
18 app
.get('/post/:id', function(req
, res
){
19 var id
= req
.params
.id
;
20 res
.send((typeof id
) + ':' + id
)
23 app
.get('/user/:uid', function(req
, res
){
24 var id
= req
.params
.id
;
25 res
.send((typeof id
) + ':' + id
)
30 .expect(200, 'number:123', function (err
) {
31 if (err
) return done(err
)
34 .expect('number:123', done
)
39 describe('.param(name, fn)', function(){
40 it('should map logic for a single param', function(done
){
43 app
.param('id', function(req
, res
, next
, id
){
45 if (isNaN(id
)) return next('route');
50 app
.get('/user/:id', function(req
, res
){
51 var id
= req
.params
.id
;
52 res
.send((typeof id
) + ':' + id
)
57 .expect(200, 'number:123', done
)
60 it('should only call once per request', function(done
) {
65 app
.param('user', function(req
, res
, next
, user
) {
71 app
.get('/foo/:user', function(req
, res
, next
) {
75 app
.get('/foo/:user', function(req
, res
, next
) {
79 app
.use(function(req
, res
) {
80 res
.end([count
, called
, req
.user
].join(' '));
85 .expect('2 1 bob', done
);
88 it('should call when values differ', function(done
) {
93 app
.param('user', function(req
, res
, next
, user
) {
95 req
.users
= (req
.users
|| []).concat(user
);
99 app
.get('/:user/bob', function(req
, res
, next
) {
103 app
.get('/foo/:user', function(req
, res
, next
) {
107 app
.use(function(req
, res
) {
108 res
.end([count
, called
, req
.users
.join(',')].join(' '));
113 .expect('2 2 foo,bob', done
);
116 it('should support altering req.params across routes', function(done
) {
119 app
.param('user', function(req
, res
, next
, user
) {
120 req
.params
.user
= 'loki';
124 app
.get('/:user', function(req
, res
, next
) {
127 app
.get('/:user', function (req
, res
) {
128 res
.send(req
.params
.user
);
133 .expect('loki', done
);
136 it('should not invoke without route handler', function(done
) {
139 app
.param('thing', function(req
, res
, next
, thing
) {
144 app
.param('user', function(req
, res
, next
, user
) {
145 next(new Error('invalid invocation'))
148 app
.post('/:user', function (req
, res
) {
149 res
.send(req
.params
.user
);
152 app
.get('/:thing', function (req
, res
) {
158 .expect(200, 'bob', done
);
161 it('should work with encoded values', function(done
){
164 app
.param('name', function(req
, res
, next
, name
){
165 req
.params
.name
= name
;
169 app
.get('/user/:name', function(req
, res
){
170 var name
= req
.params
.name
;
175 .get('/user/foo%25bar')
176 .expect('foo%bar', done
);
179 it('should catch thrown error', function(done
){
182 app
.param('id', function(req
, res
, next
, id
){
183 throw new Error('err!');
186 app
.get('/user/:id', function(req
, res
){
187 var id
= req
.params
.id
;
196 it('should catch thrown secondary error', function(done
){
199 app
.param('id', function(req
, res
, next
, val
){
200 process
.nextTick(next
);
203 app
.param('id', function(req
, res
, next
, id
){
204 throw new Error('err!');
207 app
.get('/user/:id', function(req
, res
){
208 var id
= req
.params
.id
;
217 it('should defer to next route', function(done
){
220 app
.param('id', function(req
, res
, next
, id
){
224 app
.get('/user/:id', function(req
, res
){
225 var id
= req
.params
.id
;
229 app
.get('/:name/123', function(req
, res
){
235 .expect('name', done
);
238 it('should defer all the param routes', function(done
){
241 app
.param('id', function(req
, res
, next
, val
){
242 if (val
=== 'new') return next('route');
246 app
.all('/user/:id', function(req
, res
){
250 app
.get('/user/:id', function(req
, res
){
254 app
.get('/user/new', function(req
, res
){
260 .expect('get.new', done
);
263 it('should not call when values differ on error', function(done
) {
268 app
.param('user', function(req
, res
, next
, user
) {
270 if (user
=== 'foo') throw new Error('err!');
275 app
.get('/:user/bob', function(req
, res
, next
) {
279 app
.get('/foo/:user', function(req
, res
, next
) {
284 app
.use(function(err
, req
, res
, next
) {
286 res
.send([count
, called
, err
.message
].join(' '));
291 .expect(500, '0 1 err!', done
)
294 it('should call when values differ when using "next"', function(done
) {
299 app
.param('user', function(req
, res
, next
, user
) {
301 if (user
=== 'foo') return next('route');
306 app
.get('/:user/bob', function(req
, res
, next
) {
310 app
.get('/foo/:user', function(req
, res
, next
) {
314 app
.use(function(req
, res
) {
315 res
.end([count
, called
, req
.user
].join(' '));
320 .expect('1 2 bob', done
);