This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.params.js
84 lines (63 loc) · 1.84 KB
/
test.params.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Module dependencies.
*/
var express = require('express')
, should = require('should')
, assert = require('assert')
, params = require('../');
module.exports = {
'.version': function(){
params.version.should.match(/^\d+\.\d+\.\d+$/);
},
'arity < 3': function(){
var app = express.createServer();
params.extend(app);
app.param('id', Number);
app.param('error', function(){ throw new Error('fail') });
app.param('lib', function(val){ return false; });
app.get('/project/:lib', function(req, res, next){
res.send('project ' + req.params.lib);
});
app.get('/user/:id', function(req, res, next){
var id = req.params.id;
res.send('typeof ' + typeof id + ' ' + id);
});
app.get('/error/:error', function(req, res, next){
});
assert.response(app,
{ url: '/project/express' },
{ status: 404 });
assert.response(app,
{ url: '/user/2' },
{ body: 'typeof number 2' });
assert.response(app,
{ url: '/user/tj' },
{ status: 404 });
assert.response(app,
{ url: '/error/hey' },
{ status: 500 });
},
'regexp': function(){
var app = express.createServer();
params.extend(app);
app.param('range', /^(\w+)\.\.(\w+)?$/);
app.get('/range/:range', function(req, res, next){
var range = req.params.range;
res.send('from ' + range[1] + ' to ' + range[2]);
});
app.param('username', /^[a-z_]+$/);
app.get('/user/:username', function(req, res, next){
var username = req.params.username;
res.send('user ' + username);
});
assert.response(app,
{ url: '/user/tj' },
{ body: 'user tj' });
assert.response(app,
{ url: '/user/23' },
{ status: 404 });
assert.response(app,
{ url: '/range/1..5' },
{ body: 'from 1 to 5' });
}
}