3 var after = require('after');
4 var assert = require('assert')
5 var express = require('..');
6 var request = require('supertest');
8 describe('app', function(){
9 it('should emit "mount" when mounted', function(done){
13 blog.on('mount', function(arg){
14 assert.strictEqual(arg, app)
21 describe('.use(app)', function(){
22 it('should mount the app', function(done){
26 blog.get('/blog', function(req, res){
34 .expect('blog', done);
37 it('should support mount-points', function(done){
41 var cb = after(2, done)
43 blog.get('/', function(req, res){
47 forum.get('/', function(req, res){
51 app.use('/blog', blog);
52 app.use('/forum', forum);
56 .expect(200, 'blog', cb)
60 .expect(200, 'forum', cb)
63 it('should set the child\'s .parent', function(){
67 app.use('/blog', blog);
68 assert.strictEqual(blog.parent, app)
71 it('should support dynamic routes', function(done){
75 blog.get('/', function(req, res){
79 app.use('/post/:article', blog);
82 .get('/post/once-upon-a-time')
83 .expect('success', done);
86 it('should support mounted app anywhere', function(done){
87 var cb = after(3, done);
92 function fn1(req, res, next) {
93 res.setHeader('x-fn-1', 'hit');
97 function fn2(req, res, next) {
98 res.setHeader('x-fn-2', 'hit');
102 blog.get('/', function(req, res){
106 blog.once('mount', function (parent) {
107 assert.strictEqual(parent, app)
110 other.once('mount', function (parent) {
111 assert.strictEqual(parent, app)
115 app.use('/post/:article', fn1, other, fn2, blog);
118 .get('/post/once-upon-a-time')
119 .expect('x-fn-1', 'hit')
120 .expect('x-fn-2', 'hit')
121 .expect('success', cb);
125 describe('.use(middleware)', function(){
126 it('should accept multiple arguments', function (done) {
129 function fn1(req, res, next) {
130 res.setHeader('x-fn-1', 'hit');
134 function fn2(req, res, next) {
135 res.setHeader('x-fn-2', 'hit');
139 app.use(fn1, fn2, function fn3(req, res) {
140 res.setHeader('x-fn-3', 'hit');
146 .expect('x-fn-1', 'hit')
147 .expect('x-fn-2', 'hit')
148 .expect('x-fn-3', 'hit')
152 it('should invoke middleware for all requests', function (done) {
154 var cb = after(3, done);
156 app.use(function (req, res) {
157 res.send('saw ' + req.method + ' ' + req.url);
162 .expect(200, 'saw GET /', cb);
166 .expect(200, 'saw OPTIONS /', cb);
170 .expect(200, 'saw POST /foo', cb);
173 it('should accept array of middleware', function (done) {
176 function fn1(req, res, next) {
177 res.setHeader('x-fn-1', 'hit');
181 function fn2(req, res, next) {
182 res.setHeader('x-fn-2', 'hit');
186 function fn3(req, res, next) {
187 res.setHeader('x-fn-3', 'hit');
191 app.use([fn1, fn2, fn3]);
195 .expect('x-fn-1', 'hit')
196 .expect('x-fn-2', 'hit')
197 .expect('x-fn-3', 'hit')
201 it('should accept multiple arrays of middleware', function (done) {
204 function fn1(req, res, next) {
205 res.setHeader('x-fn-1', 'hit');
209 function fn2(req, res, next) {
210 res.setHeader('x-fn-2', 'hit');
214 function fn3(req, res, next) {
215 res.setHeader('x-fn-3', 'hit');
219 app.use([fn1, fn2], [fn3]);
223 .expect('x-fn-1', 'hit')
224 .expect('x-fn-2', 'hit')
225 .expect('x-fn-3', 'hit')
229 it('should accept nested arrays of middleware', function (done) {
232 function fn1(req, res, next) {
233 res.setHeader('x-fn-1', 'hit');
237 function fn2(req, res, next) {
238 res.setHeader('x-fn-2', 'hit');
242 function fn3(req, res, next) {
243 res.setHeader('x-fn-3', 'hit');
247 app.use([[fn1], fn2], [fn3]);
251 .expect('x-fn-1', 'hit')
252 .expect('x-fn-2', 'hit')
253 .expect('x-fn-3', 'hit')
258 describe('.use(path, middleware)', function(){
259 it('should require middleware', function () {
261 assert.throws(function () { app.use('/') }, 'TypeError: app.use() requires a middleware function')
264 it('should reject string as middleware', function () {
266 assert.throws(function () { app.use('/', 'foo') }, /argument handler must be a function/)
269 it('should reject number as middleware', function () {
271 assert.throws(function () { app.use('/', 42) }, /argument handler must be a function/)
274 it('should reject null as middleware', function () {
276 assert.throws(function () { app.use('/', null) }, /argument handler must be a function/)
279 it('should reject Date as middleware', function () {
281 assert.throws(function () { app.use('/', new Date()) }, /argument handler must be a function/)
284 it('should strip path from req.url', function (done) {
287 app.use('/foo', function (req, res) {
288 res.send('saw ' + req.method + ' ' + req.url);
293 .expect(200, 'saw GET /bar', done);
296 it('should accept multiple arguments', function (done) {
299 function fn1(req, res, next) {
300 res.setHeader('x-fn-1', 'hit');
304 function fn2(req, res, next) {
305 res.setHeader('x-fn-2', 'hit');
309 app.use('/foo', fn1, fn2, function fn3(req, res) {
310 res.setHeader('x-fn-3', 'hit');
316 .expect('x-fn-1', 'hit')
317 .expect('x-fn-2', 'hit')
318 .expect('x-fn-3', 'hit')
322 it('should invoke middleware for all requests starting with path', function (done) {
324 var cb = after(3, done);
326 app.use('/foo', function (req, res) {
327 res.send('saw ' + req.method + ' ' + req.url);
336 .expect(200, 'saw POST /', cb);
340 .expect(200, 'saw POST /bar', cb);
343 it('should work if path has trailing slash', function (done) {
345 var cb = after(3, done);
347 app.use('/foo/', function (req, res) {
348 res.send('saw ' + req.method + ' ' + req.url);
357 .expect(200, 'saw POST /', cb);
361 .expect(200, 'saw POST /bar', cb);
364 it('should accept array of middleware', function (done) {
367 function fn1(req, res, next) {
368 res.setHeader('x-fn-1', 'hit');
372 function fn2(req, res, next) {
373 res.setHeader('x-fn-2', 'hit');
377 function fn3(req, res, next) {
378 res.setHeader('x-fn-3', 'hit');
382 app.use('/foo', [fn1, fn2, fn3]);
386 .expect('x-fn-1', 'hit')
387 .expect('x-fn-2', 'hit')
388 .expect('x-fn-3', 'hit')
392 it('should accept multiple arrays of middleware', function (done) {
395 function fn1(req, res, next) {
396 res.setHeader('x-fn-1', 'hit');
400 function fn2(req, res, next) {
401 res.setHeader('x-fn-2', 'hit');
405 function fn3(req, res, next) {
406 res.setHeader('x-fn-3', 'hit');
410 app.use('/foo', [fn1, fn2], [fn3]);
414 .expect('x-fn-1', 'hit')
415 .expect('x-fn-2', 'hit')
416 .expect('x-fn-3', 'hit')
420 it('should accept nested arrays of middleware', function (done) {
423 function fn1(req, res, next) {
424 res.setHeader('x-fn-1', 'hit');
428 function fn2(req, res, next) {
429 res.setHeader('x-fn-2', 'hit');
433 function fn3(req, res, next) {
434 res.setHeader('x-fn-3', 'hit');
438 app.use('/foo', [fn1, [fn2]], [fn3]);
442 .expect('x-fn-1', 'hit')
443 .expect('x-fn-2', 'hit')
444 .expect('x-fn-3', 'hit')
448 it('should support array of paths', function (done) {
450 var cb = after(3, done);
452 app.use(['/foo/', '/bar'], function (req, res) {
453 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
462 .expect(200, 'saw GET / through /foo', cb);
466 .expect(200, 'saw GET / through /bar', cb);
469 it('should support array of paths with middleware array', function (done) {
471 var cb = after(2, done);
473 function fn1(req, res, next) {
474 res.setHeader('x-fn-1', 'hit');
478 function fn2(req, res, next) {
479 res.setHeader('x-fn-2', 'hit');
483 function fn3(req, res, next) {
484 res.setHeader('x-fn-3', 'hit');
485 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
488 app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);
492 .expect('x-fn-1', 'hit')
493 .expect('x-fn-2', 'hit')
494 .expect('x-fn-3', 'hit')
495 .expect(200, 'saw GET / through /foo', cb);
499 .expect('x-fn-1', 'hit')
500 .expect('x-fn-2', 'hit')
501 .expect('x-fn-3', 'hit')
502 .expect(200, 'saw GET / through /bar', cb);
505 it('should support regexp path', function (done) {
507 var cb = after(4, done);
509 app.use(/^\/[a-z]oo/, function (req, res) {
510 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
519 .expect(200, 'saw GET / through /foo', cb);
523 .expect(200, 'saw GET /bear through /zoo/bear', cb);
530 it('should support empty string path', function (done) {
533 app.use('', function (req, res) {
534 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
539 .expect(200, 'saw GET / through /', done);