app.use.js
This file
Path
test/app.use.js
527 lines · 11.9 KiB · JavaScript
Last changed by
Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–168why, one step back12var after = require('after');3var express = require('..');4var request = require('supertest');56describe('app', function(){7 it('should emit "mount" when mounted', function(done){8 var blog = express()9 , app = express();1011 blog.on('mount', function(arg){12 arg.should.equal(app);13 done();14 });1516 app.use(blog);17 })1819 describe('.use(app)', function(){20 it('should mount the app', function(done){21 var blog = express()22 , app = express();2324 blog.get('/blog', function(req, res){25 res.end('blog');26 });2728 app.use(blog);2930 request(app)31 .get('/blog')32 .expect('blog', done);33 })3435 it('should support mount-points', function(done){36 var blog = express()37 , forum = express()38 , app = express();3940 blog.get('/', function(req, res){41 res.end('blog');42 });4344 forum.get('/', function(req, res){45 res.end('forum');46 });4748 app.use('/blog', blog);49 app.use('/forum', forum);5051 request(app)52 .get('/blog')53 .expect('blog', function(){54 request(app)55 .get('/forum')56 .expect('forum', done);57 });58 })5960 it('should set the child\'s .parent', function(){61 var blog = express()62 , app = express();6364 app.use('/blog', blog);65 blog.parent.should.equal(app);66 })6768 it('should support dynamic routes', function(done){69 var blog = express()70 , app = express();7172 blog.get('/', function(req, res){73 res.end('success');74 });7576 app.use('/post/:article', blog);7778 request(app)79 .get('/post/once-upon-a-time')80 .expect('success', done);81 })8283 it('should support mounted app anywhere', function(done){84 var cb = after(3, done);85 var blog = express()86 , other = express()87 , app = express();8889 function fn1(req, res, next) {90 res.setHeader('x-fn-1', 'hit');91 next();92 }9394 function fn2(req, res, next) {95 res.setHeader('x-fn-2', 'hit');96 next();97 }9899 blog.get('/', function(req, res){100 res.end('success');101 });102103 blog.once('mount', function (parent) {104 parent.should.equal(app);105 cb();106 });107 other.once('mount', function (parent) {108 parent.should.equal(app);109 cb();110 });111112 app.use('/post/:article', fn1, other, fn2, blog);113114 request(app)115 .get('/post/once-upon-a-time')116 .expect('x-fn-1', 'hit')117 .expect('x-fn-2', 'hit')118 .expect('success', cb);119 })120 })121122 describe('.use(middleware)', function(){123 it('should accept multiple arguments', function (done) {124 var app = express();125126 function fn1(req, res, next) {127 res.setHeader('x-fn-1', 'hit');128 next();129 }130131 function fn2(req, res, next) {132 res.setHeader('x-fn-2', 'hit');133 next();134 }135136 app.use(fn1, fn2, function fn3(req, res) {137 res.setHeader('x-fn-3', 'hit');138 res.end();139 });140141 request(app)142 .get('/')143 .expect('x-fn-1', 'hit')144 .expect('x-fn-2', 'hit')145 .expect('x-fn-3', 'hit')146 .expect(200, done);147 })148149 it('should invoke middleware for all requests', function (done) {150 var app = express();151 var cb = after(3, done);152153 app.use(function (req, res) {154 res.send('saw ' + req.method + ' ' + req.url);155 });156157 request(app)158 .get('/')159 .expect(200, 'saw GET /', cb);160161 request(app)162 .options('/')163 .expect(200, 'saw OPTIONS /', cb);164165 request(app)166 .post('/foo')167 .expect(200, 'saw POST /foo', cb);168 })Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 169–252why, one step back169170 it('should accept array of middleware', function (done) {171 var app = express();172173 function fn1(req, res, next) {174 res.setHeader('x-fn-1', 'hit');175 next();176 }177178 function fn2(req, res, next) {179 res.setHeader('x-fn-2', 'hit');180 next();181 }182183 function fn3(req, res, next) {184 res.setHeader('x-fn-3', 'hit');185 res.end();186 }187188 app.use([fn1, fn2, fn3]);189190 request(app)191 .get('/')192 .expect('x-fn-1', 'hit')193 .expect('x-fn-2', 'hit')194 .expect('x-fn-3', 'hit')195 .expect(200, done);196 })197198 it('should accept multiple arrays of middleware', function (done) {199 var app = express();200201 function fn1(req, res, next) {202 res.setHeader('x-fn-1', 'hit');203 next();204 }205206 function fn2(req, res, next) {207 res.setHeader('x-fn-2', 'hit');208 next();209 }210211 function fn3(req, res, next) {212 res.setHeader('x-fn-3', 'hit');213 res.end();214 }215216 app.use([fn1, fn2], [fn3]);217218 request(app)219 .get('/')220 .expect('x-fn-1', 'hit')221 .expect('x-fn-2', 'hit')222 .expect('x-fn-3', 'hit')223 .expect(200, done);224 })225226 it('should accept nested arrays of middleware', function (done) {227 var app = express();228229 function fn1(req, res, next) {230 res.setHeader('x-fn-1', 'hit');231 next();232 }233234 function fn2(req, res, next) {235 res.setHeader('x-fn-2', 'hit');236 next();237 }238239 function fn3(req, res, next) {240 res.setHeader('x-fn-3', 'hit');241 res.end();242 }243244 app.use([[fn1], fn2], [fn3]);245246 request(app)247 .get('/')248 .expect('x-fn-1', 'hit')249 .expect('x-fn-2', 'hit')250 .expect('x-fn-3', 'hit')251 .expect(200, done);252 })docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 253–255why, one step back253 })254255 describe('.use(path, middleware)', function(){Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
lines 256–268why, one step back256 it('should reject missing functions', function () {257 var app = express();258 app.use.bind(app, '/').should.throw(/requires middleware function/);259 })260261 it('should reject non-functions as middleware', function () {262 var app = express();263 app.use.bind(app, '/', 'hi').should.throw(/requires middleware function.*string/);264 app.use.bind(app, '/', 5).should.throw(/requires middleware function.*number/);265 app.use.bind(app, '/', null).should.throw(/requires middleware function.*Null/);266 app.use.bind(app, '/', new Date()).should.throw(/requires middleware function.*Date/);267 })268docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 269–348why, one step back269 it('should strip path from req.url', function (done) {270 var app = express();271272 app.use('/foo', function (req, res) {273 res.send('saw ' + req.method + ' ' + req.url);274 });275276 request(app)277 .get('/foo/bar')278 .expect(200, 'saw GET /bar', done);279 })280281 it('should accept multiple arguments', function (done) {282 var app = express();283284 function fn1(req, res, next) {285 res.setHeader('x-fn-1', 'hit');286 next();287 }288289 function fn2(req, res, next) {290 res.setHeader('x-fn-2', 'hit');291 next();292 }293294 app.use('/foo', fn1, fn2, function fn3(req, res) {295 res.setHeader('x-fn-3', 'hit');296 res.end();297 });298299 request(app)300 .get('/foo')301 .expect('x-fn-1', 'hit')302 .expect('x-fn-2', 'hit')303 .expect('x-fn-3', 'hit')304 .expect(200, done);305 })306307 it('should invoke middleware for all requests starting with path', function (done) {308 var app = express();309 var cb = after(3, done);310311 app.use('/foo', function (req, res) {312 res.send('saw ' + req.method + ' ' + req.url);313 });314315 request(app)316 .get('/')317 .expect(404, cb);318319 request(app)320 .post('/foo')321 .expect(200, 'saw POST /', cb);322323 request(app)324 .post('/foo/bar')325 .expect(200, 'saw POST /bar', cb);326 })327328 it('should work if path has trailing slash', function (done) {329 var app = express();330 var cb = after(3, done);331332 app.use('/foo/', function (req, res) {333 res.send('saw ' + req.method + ' ' + req.url);334 });335336 request(app)337 .get('/')338 .expect(404, cb);339340 request(app)341 .post('/foo')342 .expect(200, 'saw POST /', cb);343344 request(app)345 .post('/foo/bar')346 .expect(200, 'saw POST /bar', cb);347 })348Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 349–432why, one step back349 it('should accept array of middleware', function (done) {350 var app = express();351352 function fn1(req, res, next) {353 res.setHeader('x-fn-1', 'hit');354 next();355 }356357 function fn2(req, res, next) {358 res.setHeader('x-fn-2', 'hit');359 next();360 }361362 function fn3(req, res, next) {363 res.setHeader('x-fn-3', 'hit');364 res.end();365 }366367 app.use('/foo', [fn1, fn2, fn3]);368369 request(app)370 .get('/foo')371 .expect('x-fn-1', 'hit')372 .expect('x-fn-2', 'hit')373 .expect('x-fn-3', 'hit')374 .expect(200, done);375 })376377 it('should accept multiple arrays of middleware', function (done) {378 var app = express();379380 function fn1(req, res, next) {381 res.setHeader('x-fn-1', 'hit');382 next();383 }384385 function fn2(req, res, next) {386 res.setHeader('x-fn-2', 'hit');387 next();388 }389390 function fn3(req, res, next) {391 res.setHeader('x-fn-3', 'hit');392 res.end();393 }394395 app.use('/foo', [fn1, fn2], [fn3]);396397 request(app)398 .get('/foo')399 .expect('x-fn-1', 'hit')400 .expect('x-fn-2', 'hit')401 .expect('x-fn-3', 'hit')402 .expect(200, done);403 })404405 it('should accept nested arrays of middleware', function (done) {406 var app = express();407408 function fn1(req, res, next) {409 res.setHeader('x-fn-1', 'hit');410 next();411 }412413 function fn2(req, res, next) {414 res.setHeader('x-fn-2', 'hit');415 next();416 }417418 function fn3(req, res, next) {419 res.setHeader('x-fn-3', 'hit');420 res.end();421 }422423 app.use('/foo', [fn1, [fn2]], [fn3]);424425 request(app)426 .get('/foo')427 .expect('x-fn-1', 'hit')428 .expect('x-fn-2', 'hit')429 .expect('x-fn-3', 'hit')430 .expect(200, done);431 })432docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 433–450why, one step back433 it('should support array of paths', function (done) {434 var app = express();435 var cb = after(3, done);436437 app.use(['/foo/', '/bar'], function (req, res) {438 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);439 });440441 request(app)442 .get('/')443 .expect(404, cb);444445 request(app)446 .get('/foo')447 .expect(200, 'saw GET / through /foo', cb);448449 request(app)450 .get('/bar')Fix app.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoptioncf41a8f
lines 451–486why, one step back451 .expect(200, 'saw GET / through /bar', cb);452 })453454 it('should support array of paths with middleware array', function (done) {455 var app = express();456 var cb = after(2, done);457458 function fn1(req, res, next) {459 res.setHeader('x-fn-1', 'hit');460 next();461 }462463 function fn2(req, res, next) {464 res.setHeader('x-fn-2', 'hit');465 next();466 }467468 function fn3(req, res, next) {469 res.setHeader('x-fn-3', 'hit');470 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);471 }472473 app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);474475 request(app)476 .get('/foo')477 .expect('x-fn-1', 'hit')478 .expect('x-fn-2', 'hit')479 .expect('x-fn-3', 'hit')480 .expect(200, 'saw GET / through /foo', cb);481482 request(app)483 .get('/bar')484 .expect('x-fn-1', 'hit')485 .expect('x-fn-2', 'hit')486 .expect('x-fn-3', 'hit')docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 487–513why, one step back487 .expect(200, 'saw GET / through /bar', cb);488 })489490 it('should support regexp path', function (done) {491 var app = express();492 var cb = after(4, done);493494 app.use(/^\/[a-z]oo/, function (req, res) {495 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);496 });497498 request(app)499 .get('/')500 .expect(404, cb);501502 request(app)503 .get('/foo')504 .expect(200, 'saw GET / through /foo', cb);505506 request(app)507 .get('/zoo/bear')508 .expect(200, 'saw GET /bear through /zoo/bear', cb);509510 request(app)511 .get('/get/zoo')512 .expect(404, cb);513 })Fix regression for empty string path in app.useDouglas Christopher Wilsonbefore adoption3c1a964
lines 514–525why, one step back514515 it('should support empty string path', function (done) {516 var app = express();517518 app.use('', function (req, res) {519 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);520 });521522 request(app)523 .get('/')524 .expect(200, 'saw GET / through /', done);525 })docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 526–527why, one step back526 })527})