2 var after = require('after');
3 var assert = require('assert')
4 var express = require('..');
5 var request = require('supertest');
7 describe('app', function(){
8 it('should emit "mount" when mounted', function(done){
12 blog.on('mount', function(arg){
13 arg.should.equal(app);
20 describe('.use(app)', function(){
21 it('should mount the app', function(done){
25 blog.get('/blog', function(req, res){
33 .expect('blog', done);
36 it('should support mount-points', function(done){
41 blog.get('/', function(req, res){
45 forum.get('/', function(req, res){
49 app.use('/blog', blog);
50 app.use('/forum', forum);
54 .expect('blog', function(){
57 .expect('forum', done);
61 it('should set the child\'s .parent', function(){
65 app.use('/blog', blog);
66 blog.parent.should.equal(app);
69 it('should support dynamic routes', function(done){
73 blog.get('/', function(req, res){
77 app.use('/post/:article', blog);
80 .get('/post/once-upon-a-time')
81 .expect('success', done);
84 it('should support mounted app anywhere', function(done){
85 var cb = after(3, done);
90 function fn1(req, res, next) {
91 res.setHeader('x-fn-1', 'hit');
95 function fn2(req, res, next) {
96 res.setHeader('x-fn-2', 'hit');
100 blog.get('/', function(req, res){
104 blog.once('mount', function (parent) {
105 parent.should.equal(app);
108 other.once('mount', function (parent) {
109 parent.should.equal(app);
113 app.use('/post/:article', fn1, other, fn2, blog);
116 .get('/post/once-upon-a-time')
117 .expect('x-fn-1', 'hit')
118 .expect('x-fn-2', 'hit')
119 .expect('success', cb);
123 describe('.use(middleware)', function(){
124 it('should accept multiple arguments', function (done) {
127 function fn1(req, res, next) {
128 res.setHeader('x-fn-1', 'hit');
132 function fn2(req, res, next) {
133 res.setHeader('x-fn-2', 'hit');
137 app.use(fn1, fn2, function fn3(req, res) {
138 res.setHeader('x-fn-3', 'hit');
144 .expect('x-fn-1', 'hit')
145 .expect('x-fn-2', 'hit')
146 .expect('x-fn-3', 'hit')
150 it('should invoke middleware for all requests', function (done) {
152 var cb = after(3, done);
154 app.use(function (req, res) {
155 res.send('saw ' + req.method + ' ' + req.url);
160 .expect(200, 'saw GET /', cb);
164 .expect(200, 'saw OPTIONS /', cb);
168 .expect(200, 'saw POST /foo', cb);
171 it('should accept array of middleware', function (done) {
174 function fn1(req, res, next) {
175 res.setHeader('x-fn-1', 'hit');
179 function fn2(req, res, next) {
180 res.setHeader('x-fn-2', 'hit');
184 function fn3(req, res, next) {
185 res.setHeader('x-fn-3', 'hit');
189 app.use([fn1, fn2, fn3]);
193 .expect('x-fn-1', 'hit')
194 .expect('x-fn-2', 'hit')
195 .expect('x-fn-3', 'hit')
199 it('should accept multiple arrays of middleware', function (done) {
202 function fn1(req, res, next) {
203 res.setHeader('x-fn-1', 'hit');
207 function fn2(req, res, next) {
208 res.setHeader('x-fn-2', 'hit');
212 function fn3(req, res, next) {
213 res.setHeader('x-fn-3', 'hit');
217 app.use([fn1, fn2], [fn3]);
221 .expect('x-fn-1', 'hit')
222 .expect('x-fn-2', 'hit')
223 .expect('x-fn-3', 'hit')
227 it('should accept nested arrays of middleware', function (done) {
230 function fn1(req, res, next) {
231 res.setHeader('x-fn-1', 'hit');
235 function fn2(req, res, next) {
236 res.setHeader('x-fn-2', 'hit');
240 function fn3(req, res, next) {
241 res.setHeader('x-fn-3', 'hit');
245 app.use([[fn1], fn2], [fn3]);
249 .expect('x-fn-1', 'hit')
250 .expect('x-fn-2', 'hit')
251 .expect('x-fn-3', 'hit')
256 describe('.use(path, middleware)', function(){
257 it('should require middleware', function () {
259 assert.throws(function () { app.use('/') }, /requires a middleware function/)
262 it('should reject string as middleware', function () {
264 assert.throws(function () { app.use('/', 'foo') }, /requires a middleware function but got a string/)
267 it('should reject number as middleware', function () {
269 assert.throws(function () { app.use('/', 42) }, /requires a middleware function but got a number/)
272 it('should reject null as middleware', function () {
274 assert.throws(function () { app.use('/', null) }, /requires a middleware function but got a Null/)
277 it('should reject Date as middleware', function () {
279 assert.throws(function () { app.use('/', new Date()) }, /requires a middleware function but got a Date/)
282 it('should strip path from req.url', function (done) {
285 app.use('/foo', function (req, res) {
286 res.send('saw ' + req.method + ' ' + req.url);
291 .expect(200, 'saw GET /bar', done);
294 it('should accept multiple arguments', function (done) {
297 function fn1(req, res, next) {
298 res.setHeader('x-fn-1', 'hit');
302 function fn2(req, res, next) {
303 res.setHeader('x-fn-2', 'hit');
307 app.use('/foo', fn1, fn2, function fn3(req, res) {
308 res.setHeader('x-fn-3', 'hit');
314 .expect('x-fn-1', 'hit')
315 .expect('x-fn-2', 'hit')
316 .expect('x-fn-3', 'hit')
320 it('should invoke middleware for all requests starting with path', function (done) {
322 var cb = after(3, done);
324 app.use('/foo', function (req, res) {
325 res.send('saw ' + req.method + ' ' + req.url);
334 .expect(200, 'saw POST /', cb);
338 .expect(200, 'saw POST /bar', cb);
341 it('should work if path has trailing slash', function (done) {
343 var cb = after(3, done);
345 app.use('/foo/', function (req, res) {
346 res.send('saw ' + req.method + ' ' + req.url);
355 .expect(200, 'saw POST /', cb);
359 .expect(200, 'saw POST /bar', cb);
362 it('should accept array of middleware', function (done) {
365 function fn1(req, res, next) {
366 res.setHeader('x-fn-1', 'hit');
370 function fn2(req, res, next) {
371 res.setHeader('x-fn-2', 'hit');
375 function fn3(req, res, next) {
376 res.setHeader('x-fn-3', 'hit');
380 app.use('/foo', [fn1, fn2, fn3]);
384 .expect('x-fn-1', 'hit')
385 .expect('x-fn-2', 'hit')
386 .expect('x-fn-3', 'hit')
390 it('should accept multiple arrays of middleware', function (done) {
393 function fn1(req, res, next) {
394 res.setHeader('x-fn-1', 'hit');
398 function fn2(req, res, next) {
399 res.setHeader('x-fn-2', 'hit');
403 function fn3(req, res, next) {
404 res.setHeader('x-fn-3', 'hit');
408 app.use('/foo', [fn1, fn2], [fn3]);
412 .expect('x-fn-1', 'hit')
413 .expect('x-fn-2', 'hit')
414 .expect('x-fn-3', 'hit')
418 it('should accept nested arrays of middleware', function (done) {
421 function fn1(req, res, next) {
422 res.setHeader('x-fn-1', 'hit');
426 function fn2(req, res, next) {
427 res.setHeader('x-fn-2', 'hit');
431 function fn3(req, res, next) {
432 res.setHeader('x-fn-3', 'hit');
436 app.use('/foo', [fn1, [fn2]], [fn3]);
440 .expect('x-fn-1', 'hit')
441 .expect('x-fn-2', 'hit')
442 .expect('x-fn-3', 'hit')
446 it('should support array of paths', function (done) {
448 var cb = after(3, done);
450 app.use(['/foo/', '/bar'], function (req, res) {
451 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
460 .expect(200, 'saw GET / through /foo', cb);
464 .expect(200, 'saw GET / through /bar', cb);
467 it('should support array of paths with middleware array', function (done) {
469 var cb = after(2, done);
471 function fn1(req, res, next) {
472 res.setHeader('x-fn-1', 'hit');
476 function fn2(req, res, next) {
477 res.setHeader('x-fn-2', 'hit');
481 function fn3(req, res, next) {
482 res.setHeader('x-fn-3', 'hit');
483 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
486 app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);
490 .expect('x-fn-1', 'hit')
491 .expect('x-fn-2', 'hit')
492 .expect('x-fn-3', 'hit')
493 .expect(200, 'saw GET / through /foo', cb);
497 .expect('x-fn-1', 'hit')
498 .expect('x-fn-2', 'hit')
499 .expect('x-fn-3', 'hit')
500 .expect(200, 'saw GET / through /bar', cb);
503 it('should support regexp path', function (done) {
505 var cb = after(4, done);
507 app.use(/^\/[a-z]oo/, function (req, res) {
508 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
517 .expect(200, 'saw GET / through /foo', cb);
521 .expect(200, 'saw GET /bear through /zoo/bear', cb);
528 it('should support empty string path', function (done) {
531 app.use('', function (req, res) {
532 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
537 .expect(200, 'saw GET / through /', done);