2012-02-26 11:46:44 -08:00
|
|
|
|
|
|
|
|
var express = require('../')
|
2014-03-05 22:06:14 -08:00
|
|
|
, request = require('supertest');
|
2012-02-26 11:46:44 -08:00
|
|
|
|
|
|
|
|
describe('app', function(){
|
|
|
|
|
describe('.param(fn)', function(){
|
|
|
|
|
it('should map app.param(name, ...) logic', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param(function(name, regexp){
|
2013-04-01 14:03:32 -04:00
|
|
|
if (Object.prototype.toString.call(regexp) == '[object RegExp]') { // See #1557
|
2012-02-26 11:46:44 -08:00
|
|
|
return function(req, res, next, val){
|
|
|
|
|
var captures;
|
|
|
|
|
if (captures = regexp.exec(String(val))) {
|
|
|
|
|
req.params[name] = captures[1];
|
|
|
|
|
next();
|
|
|
|
|
} else {
|
|
|
|
|
next('route');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.param(':name', /^([a-zA-Z]+)$/);
|
|
|
|
|
|
|
|
|
|
app.get('/user/:name', function(req, res){
|
|
|
|
|
res.send(req.params.name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/tj')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
|
|
|
|
res.text.should.equal('tj');
|
2012-02-26 11:46:44 -08:00
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect(404, done);
|
2012-02-26 11:46:44 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
2014-05-20 23:25:51 -04:00
|
|
|
|
|
|
|
|
it('should fail if not given fn', function(){
|
|
|
|
|
var app = express();
|
|
|
|
|
app.param.bind(app, ':name', 'bob').should.throw();
|
|
|
|
|
})
|
2012-02-26 11:46:44 -08:00
|
|
|
})
|
2012-02-26 11:50:32 -08:00
|
|
|
|
2012-02-26 11:52:23 -08:00
|
|
|
describe('.param(names, fn)', function(){
|
|
|
|
|
it('should map the array', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param(['id', 'uid'], function(req, res, next, id){
|
|
|
|
|
id = Number(id);
|
|
|
|
|
if (isNaN(id)) return next('route');
|
|
|
|
|
req.params.id = id;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/post/:id', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
2013-10-14 18:35:46 -07:00
|
|
|
id.should.be.a.Number;
|
2012-02-26 11:52:23 -08:00
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:uid', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
2013-10-14 18:35:46 -07:00
|
|
|
id.should.be.a.Number;
|
2012-02-26 11:52:23 -08:00
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
|
|
|
|
res.text.should.equal('123');
|
2012-02-26 11:52:23 -08:00
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/post/123')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('123', done);
|
2012-02-26 11:52:23 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2012-02-26 11:50:32 -08:00
|
|
|
describe('.param(name, fn)', function(){
|
|
|
|
|
it('should map logic for a single param', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, id){
|
|
|
|
|
id = Number(id);
|
|
|
|
|
if (isNaN(id)) return next('route');
|
|
|
|
|
req.params.id = id;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:id', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
2013-10-14 18:35:46 -07:00
|
|
|
id.should.be.a.Number;
|
2012-02-26 11:50:32 -08:00
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('123', done);
|
2012-02-26 11:50:32 -08:00
|
|
|
})
|
2014-05-20 10:58:39 -04:00
|
|
|
|
|
|
|
|
it('should only call once per request', function(done) {
|
|
|
|
|
var app = express();
|
|
|
|
|
var called = 0;
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
app.param('user', function(req, res, next, user) {
|
|
|
|
|
called++;
|
|
|
|
|
req.user = user;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/foo/:user', function(req, res, next) {
|
|
|
|
|
count++;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
app.get('/foo/:user', function(req, res, next) {
|
|
|
|
|
count++;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
app.use(function(req, res) {
|
|
|
|
|
res.end([count, called, req.user].join(' '));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/foo/bob')
|
|
|
|
|
.expect('2 1 bob', done);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should call when values differ', function(done) {
|
|
|
|
|
var app = express();
|
|
|
|
|
var called = 0;
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
|
|
app.param('user', function(req, res, next, user) {
|
|
|
|
|
called++;
|
|
|
|
|
req.users = (req.users || []).concat(user);
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/:user/bob', function(req, res, next) {
|
|
|
|
|
count++;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
app.get('/foo/:user', function(req, res, next) {
|
|
|
|
|
count++;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
app.use(function(req, res) {
|
|
|
|
|
res.end([count, called, req.users.join(',')].join(' '));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/foo/bob')
|
|
|
|
|
.expect('2 2 foo,bob', done);
|
|
|
|
|
})
|
2014-05-21 02:08:04 -04:00
|
|
|
|
2014-06-11 18:15:09 -04:00
|
|
|
it('should support altering req.params across routes', function(done) {
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('user', function(req, res, next, user) {
|
|
|
|
|
req.params.user = 'loki';
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/:user', function(req, res, next) {
|
|
|
|
|
next('route');
|
|
|
|
|
});
|
|
|
|
|
app.get('/:user', function(req, res, next) {
|
|
|
|
|
res.send(req.params.user);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/bob')
|
|
|
|
|
.expect('loki', done);
|
|
|
|
|
})
|
|
|
|
|
|
2014-07-03 16:41:23 -04:00
|
|
|
it('should not invoke without route handler', function(done) {
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('thing', function(req, res, next, thing) {
|
|
|
|
|
req.thing = thing;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.param('user', function(req, res, next, user) {
|
|
|
|
|
next(new Error('invalid invokation'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.post('/:user', function(req, res, next) {
|
|
|
|
|
res.send(req.params.user);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/:thing', function(req, res, next) {
|
|
|
|
|
res.send(req.thing);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/bob')
|
|
|
|
|
.expect(200, 'bob', done);
|
|
|
|
|
})
|
|
|
|
|
|
2014-05-28 01:08:47 -04:00
|
|
|
it('should work with encoded values', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('name', function(req, res, next, name){
|
|
|
|
|
req.params.name = name;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:name', function(req, res){
|
|
|
|
|
var name = req.params.name;
|
|
|
|
|
res.send('' + name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/foo%25bar')
|
|
|
|
|
.expect('foo%bar', done);
|
|
|
|
|
})
|
|
|
|
|
|
2014-05-20 23:25:51 -04:00
|
|
|
it('should catch thrown error', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, id){
|
|
|
|
|
throw new Error('err!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:id', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
|
|
|
|
.expect(500, done);
|
|
|
|
|
})
|
2014-07-08 01:15:31 -04:00
|
|
|
|
|
|
|
|
it('should catch thrown secondary error', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, val){
|
|
|
|
|
process.nextTick(next);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, id){
|
|
|
|
|
throw new Error('err!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:id', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
|
|
|
|
.expect(500, done);
|
|
|
|
|
})
|
2014-05-20 23:25:51 -04:00
|
|
|
|
|
|
|
|
it('should defer to next route', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, id){
|
|
|
|
|
next('route');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:id', function(req, res){
|
|
|
|
|
var id = req.params.id;
|
|
|
|
|
res.send('' + id);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/:name/123', function(req, res){
|
|
|
|
|
res.send('name');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/123')
|
|
|
|
|
.expect('name', done);
|
|
|
|
|
})
|
2014-05-28 22:26:05 -04:00
|
|
|
|
|
|
|
|
it('should defer all the param routes', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.param('id', function(req, res, next, val){
|
|
|
|
|
if (val === 'new') return next('route');
|
|
|
|
|
return next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.all('/user/:id', function(req, res){
|
|
|
|
|
res.send('all.id');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/:id', function(req, res){
|
|
|
|
|
res.send('get.id');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/user/new', function(req, res){
|
|
|
|
|
res.send('get.new');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/user/new')
|
|
|
|
|
.expect('get.new', done);
|
|
|
|
|
})
|
2012-02-26 11:50:32 -08:00
|
|
|
})
|
2012-02-26 11:46:44 -08:00
|
|
|
})
|