Updated params example with acceptance test.

Esse commit está contido em:
Robert Sköld
2011-12-18 23:36:45 +01:00
commit de TJ Holowaychuk
commit a153082120
2 arquivos alterados com 42 adições e 5 exclusões
+14 -5
Ver Arquivo
@@ -3,8 +3,8 @@
* Module dependencies.
*/
var express = require('../../lib/express')
, app = express.createServer();
var express = require('../../')
, app = module.exports = express();
// Faux database
@@ -18,7 +18,14 @@ var users = [
// Convert :to and :from to integers
app.param(['to', 'from'], function(n){ return parseInt(n, 10); });
app.param(['to', 'from'], function(req, res, next, num, name){
req.params[name] = num = parseInt(num, 10);
if( isNaN(num) ){
next(new Error('failed to parseInt '+num));
} else {
next();
}
});
// Load user by id
@@ -57,5 +64,7 @@ app.get('/users/:from-:to', function(req, res, next){
res.send('users ' + names.slice(from, to).join(', '));
});
app.listen(3000);
console.log('Express application listening on port 3000');
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}
+28
Ver Arquivo
@@ -0,0 +1,28 @@
var app = require('../../examples/params/app')
, request = require('../support/http');
describe('params', function(){
describe('GET /', function(){
it('should respond with instructions', function(done){
request(app)
.get('/')
.expect(/Visit/,done)
})
})
describe('GET /user/0', function(){
it('should respond with a user', function(done){
request(app)
.get('/user/0')
.expect(/user tj/,done)
})
})
describe('GET /users/0-2', function(){
it('should respond with three users', function(done){
request(app)
.get('/users/0-2')
.expect(/users tj, tobi/,done)
})
})
})