UNPKG

3.26 kBJavaScriptView Raw
1
2/**
3 * Module dependencies.
4 */
5
6var express = require('express')
7 , assert = require('assert')
8 , namespace = require('../');
9
10module.exports = {
11 'test app.namespace(str, fn)': function(){
12 var app = express.createServer()
13 , id;
14
15 app.get('/one', function(req, res){
16 res.send('GET one');
17 });
18
19 assert.equal(app.namespace('/user', function(){}), app);
20
21 app.namespace('/user', function(){
22 app.all('/:id', function(req, res, next){
23 id = req.params.id;
24 next();
25 });
26
27 app.get('/:id', function(req, res){
28 res.send('GET user ' + id);
29 });
30
31 app.del('/:id', function(req, res){
32 res.send('DELETE user ' + id);
33 });
34 });
35
36 app.get('/two', function(req, res){
37 res.send('GET two');
38 });
39
40 assert.response(app,
41 { url: '/user/12' },
42 { body: 'GET user 12' });
43
44 assert.response(app,
45 { url: '/user/12', method: 'DELETE' },
46 { body: 'DELETE user 12' });
47
48 assert.response(app,
49 { url: '/one' },
50 { body: 'GET one' });
51
52 assert.response(app,
53 { url: '/two' },
54 { body: 'GET two' });
55 },
56
57 'test app.namespace(str, fn) nesting': function(done){
58 var pending = 6
59 , calls = 0
60 , app = express.createServer();
61
62 function finished() {
63 --pending || function(){
64 assert.equal(2, calls);
65 done();
66 }();
67 }
68
69 function middleware(req, res, next) {
70 ++calls;
71 next();
72 }
73
74 app.get('/one', function(req, res){
75 res.send('GET one');
76 });
77
78 app.namespace('/forum/:id', function(){
79 app.get('/', function(req, res){
80 res.send('GET forum ' + req.params.id);
81 });
82
83 app.get('/edit', function(req, res){
84 res.send('GET forum ' + req.params.id + ' edit page');
85 });
86
87 app.namespace('/thread', function(){
88 app.get('/:tid', middleware, middleware, function(req, res){
89 res.send('GET forum ' + req.params.id + ' thread ' + req.params.tid);
90 });
91 });
92
93 app.del('/', function(req, res){
94 res.send('DELETE forum ' + req.params.id);
95 });
96 });
97
98 app.get('/two', function(req, res){
99 res.send('GET two');
100 });
101
102 assert.response(app,
103 { url: '/forum/1' },
104 { body: 'GET forum 1' }
105 , finished);
106
107 assert.response(app,
108 { url: '/forum/1/edit' },
109 { body: 'GET forum 1 edit page' }
110 , finished);
111
112 assert.response(app,
113 { url: '/forum/1/thread/50' },
114 { body: 'GET forum 1 thread 50' }
115 , finished);
116
117 assert.response(app,
118 { url: '/forum/2', method: 'DELETE' },
119 { body: 'DELETE forum 2' }
120 , finished);
121
122 assert.response(app,
123 { url: '/one' },
124 { body: 'GET one' }
125 , finished);
126
127 assert.response(app,
128 { url: '/two' },
129 { body: 'GET two' }
130 , finished);
131 },
132
133 'test fn.route': function(){
134 var app = express.createServer();
135
136 app.namespace('/user/:id', function(){
137 app.get('/', function handler(req, res){
138 assert.equal('/user/:id', handler.namespace);
139 res.send(200);
140 });
141 });
142
143 assert.response(app,
144 { url: '/user/12' },
145 { body: 'OK' });
146 }
147};
\No newline at end of file