1
+
1
2
/**
2
3
* Module dependencies.
3
4
*/
4
5
5
6
var express = require ( 'express' )
6
7
, assert = require ( 'assert' )
7
- , namespace = require ( '../' ) ;
8
+ , request = require ( 'supertest' )
9
+ , namespace = require ( '..' )
10
+ , pending = require ( './support/pending' ) ;
8
11
9
- module . exports = {
10
- 'test app.namespace(str, fn)' : function ( ) {
11
- var app = express ( )
12
- , id ;
12
+ describe ( 'app.namespace(path, fn)' , function ( ) {
13
+ it ( 'should not prefix root-level paths' , function ( done ) {
14
+ var app = express ( ) ;
15
+ done = pending ( 2 , done ) ;
13
16
14
17
app . get ( '/one' , function ( req , res ) {
15
18
res . send ( 'GET one' ) ;
16
19
} ) ;
17
20
18
- assert . equal ( app . namespace ( '/user' , function ( ) { } ) , app ) ;
19
-
20
- app . namespace ( '/user' , function ( ) {
21
- app . all ( '/:id' , function ( req , res , next ) {
22
- id = req . params . id ;
23
- next ( ) ;
24
- } ) ;
25
-
26
- app . get ( '/:id' , function ( req , res ) {
27
- res . send ( 'GET user ' + id ) ;
28
- } ) ;
29
-
30
- app . del ( '/:id' , function ( req , res ) {
31
- res . send ( 'DELETE user ' + id ) ;
32
- } ) ;
33
- } ) ;
34
-
35
- app . get ( '/two' , function ( req , res ) {
21
+ app . get ( '/some/two' , function ( req , res ) {
36
22
res . send ( 'GET two' ) ;
37
23
} ) ;
38
24
39
- assert . response ( app ,
40
- { url : '/user/12' } ,
41
- { body : 'GET user 12' } ) ;
42
-
43
- assert . response ( app ,
44
- { url : '/user/12' , method : 'DELETE' } ,
45
- { body : 'DELETE user 12' } ) ;
46
-
47
- assert . response ( app ,
48
- { url : '/one' } ,
49
- { body : 'GET one' } ) ;
50
-
51
- assert . response ( app ,
52
- { url : '/two' } ,
53
- { body : 'GET two' } ) ;
54
- } ,
55
-
56
- 'test app.namespace(str, fn) nesting' : function ( done ) {
57
- var pending = 6
58
- , calls = 0
59
- , app = express ( ) ;
60
-
61
- function finished ( ) {
62
- -- pending || function ( ) {
63
- assert . equal ( 2 , calls ) ;
64
- done ( ) ;
65
- } ( ) ;
66
- }
25
+ request ( app )
26
+ . get ( '/one' )
27
+ . expect ( 'GET one' , done ) ;
67
28
68
- function middleware ( req , res , next ) {
69
- ++ calls ;
70
- next ( ) ;
71
- }
29
+ request ( app )
30
+ . get ( '/some/two' )
31
+ . expect ( 'GET two' , done ) ;
32
+ } )
33
+
34
+ it ( 'should prefix within .namespace()' , function ( done ) {
35
+ var app = express ( ) ;
36
+ done = pending ( 4 , done ) ;
72
37
73
38
app . get ( '/one' , function ( req , res ) {
74
39
res . send ( 'GET one' ) ;
75
40
} ) ;
76
41
77
- app . namespace ( '/forum/:id ' , function ( ) {
42
+ app . namespace ( '/foo ' , function ( ) {
78
43
app . get ( '/' , function ( req , res ) {
79
- res . send ( 'GET forum ' + req . params . id ) ;
80
- } ) ;
81
-
82
- app . get ( '/edit' , function ( req , res ) {
83
- res . send ( 'GET forum ' + req . params . id + ' edit page' ) ;
44
+ res . send ( 'foo' ) ;
84
45
} ) ;
85
46
86
- app . namespace ( '/thread ' , function ( ) {
87
- app . get ( '/:tid' , middleware , middleware , function ( req , res ) {
88
- res . send ( 'GET forum ' + req . params . id + ' thread ' + req . params . tid ) ;
47
+ app . namespace ( '/baz ' , function ( ) {
48
+ app . get ( '/' , function ( req , res ) {
49
+ res . send ( 'GET baz' ) ;
89
50
} ) ;
90
- } ) ;
91
51
92
- app . del ( '/' , function ( req , res ) {
93
- res . send ( 'DELETE forum ' + req . params . id ) ;
52
+ app . del ( '/all' , function ( req , res ) {
53
+ res . send ( 'DELETE all baz' ) ;
54
+ } ) ;
55
+ } )
56
+
57
+ app . get ( '/bar' , function ( req , res ) {
58
+ res . send ( 'bar' ) ;
94
59
} ) ;
95
- } ) ;
96
-
97
- app . get ( '/two' , function ( req , res ) {
60
+ } )
61
+
62
+ app . get ( '/some/ two' , function ( req , res ) {
98
63
res . send ( 'GET two' ) ;
99
64
} ) ;
100
65
101
- assert . response ( app ,
102
- { url : '/forum/1' } ,
103
- { body : 'GET forum 1' }
104
- , finished ) ;
105
-
106
- assert . response ( app ,
107
- { url : '/forum/1/edit' } ,
108
- { body : 'GET forum 1 edit page' }
109
- , finished ) ;
110
-
111
- assert . response ( app ,
112
- { url : '/forum/1/thread/50' } ,
113
- { body : 'GET forum 1 thread 50' }
114
- , finished ) ;
115
-
116
- assert . response ( app ,
117
- { url : '/forum/2' , method : 'DELETE' } ,
118
- { body : 'DELETE forum 2' }
119
- , finished ) ;
120
-
121
- assert . response ( app ,
122
- { url : '/one' } ,
123
- { body : 'GET one' }
124
- , finished ) ;
125
-
126
- assert . response ( app ,
127
- { url : '/two' } ,
128
- { body : 'GET two' }
129
- , finished ) ;
130
- } ,
131
-
132
- 'test fn.route' : function ( ) {
133
- var app = express ( ) ;
66
+ request ( app )
67
+ . get ( '/foo/baz' )
68
+ . expect ( 'GET baz' , done ) ;
134
69
135
- app . namespace ( '/user/:id' , function ( ) {
136
- app . get ( '/' , function handler ( req , res ) {
137
- assert . equal ( '/user/:id' , handler . namespace ) ;
138
- res . send ( 200 ) ;
139
- } ) ;
140
- } ) ;
70
+ request ( app )
71
+ . del ( '/foo/baz/all' )
72
+ . expect ( 'DELETE all baz' , done ) ;
141
73
142
- assert . response ( app ,
143
- { url : '/user/12' } ,
144
- { body : 'OK' } ) ;
145
- } ,
146
- 'test app.namespace(str, middleware, fn)' : function ( done ) {
147
- var app = express ( ) ,
148
- calledA = 0 ,
149
- calledB = 0 ;
150
-
151
- function middlewareA ( req , res , next ) {
152
- calledA ++ ;
153
- next ( ) ;
154
- }
74
+ request ( app )
75
+ . get ( '/one' )
76
+ . expect ( 'GET one' , done ) ;
155
77
156
- function middlewareB ( req , res , next ) {
157
- calledB ++ ;
158
- next ( ) ;
159
- }
78
+ request ( app )
79
+ . get ( '/some/two' )
80
+ . expect ( 'GET two' , done ) ;
160
81
161
- app . namespace ( '/user/:id' , middlewareA , function ( ) {
162
- app . get ( '/' , function ( req , res ) {
163
- res . send ( 'got Home' ) ;
164
- } ) ;
82
+ request ( app )
83
+ . get ( '/foo' )
84
+ . expect ( 'foo' , done ) ;
165
85
166
- app . get ( '/other' , function ( req , res ) {
167
- res . send ( 'got Other' ) ;
168
- } ) ;
86
+ request ( app )
87
+ . get ( '/foo/bar' )
88
+ . expect ( 'bar' , done ) ;
89
+ } )
169
90
170
- app . namespace ( '/nest' , middlewareB , function ( req , res ) {
171
- app . get ( '/' , function ( req , res ) {
172
- res . send ( 'got Nest' ) ;
173
- } ) ;
174
- } ) ;
175
- } ) ;
91
+ it ( 'should support middleware' , function ( done ) {
92
+ var app = express ( ) ;
176
93
177
- var pending = 3 ;
178
- function finished ( ) {
179
- -- pending || function ( ) {
180
- assert . equal ( 3 , calledA ) ;
181
- assert . equal ( 1 , calledB ) ;
182
- done ( ) ;
183
- } ( ) ;
94
+ function load ( req , res , next ) {
95
+ req . forum = { id : req . params . id } ;
96
+ next ( ) ;
184
97
}
185
98
186
- assert . response ( app ,
187
- { url : '/user/12' } ,
188
- { body : 'got Home' } ,
189
- finished ) ;
190
- assert . response ( app ,
191
- { url : '/user/12/other' } ,
192
- { body : 'got Other' } ,
193
- finished ) ;
194
- assert . response ( app ,
195
- { url : '/user/12/nest' } ,
196
- { body : 'got Nest' } ,
197
- finished ) ;
198
- }
199
- } ;
99
+ app . namespace ( '/forum/:id' , load , function ( ) {
100
+ app . get ( '/' , function ( req , res ) {
101
+ res . send ( '' + req . forum . id ) ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ request ( app )
106
+ . get ( '/forum/23' )
107
+ . expect ( '23' , done ) ;
108
+ } )
109
+ } )
0 commit comments