app.router.js
This file
Path
test/app.router.js
663 lines · 14.4 KiB · JavaScript
Last changed by
tests: add more testsDouglas Christopher Wilsonbefore adoption5019f38
History of this fileTake it
The walk stopped after 200 commits: lines older than 7c68822 point at it.
bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
line 3why, one step back3 , request = require('supertest')bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 4–8why, one step back4 , assert = require('assert')5 , methods = require('methods');67describe('app.router', function(){8 describe('methods supported', function(){Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
line 9why, one step back9 methods.concat('del').forEach(function(method){test app.router: ignore connect methodJonathan Ongbefore adoption0796c1d
lines 10–11why, one step back10 if (method === 'connect') return;11bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 12–27why, one step back12 it('should include ' + method.toUpperCase(), function(done){13 var app = express();14 var calls = [];1516 app[method]('/foo', function(req, res){17 if ('head' == method) {18 res.end();19 } else {20 res.end(method);21 }22 });2324 request(app)25 [method]('/foo')26 .expect('head' == method ? '' : method, done);27 })tests: add more testsDouglas Christopher Wilsonbefore adoption5019f38
lines 28–32why, one step back2829 it('should reject numbers for app.' + method, function(){30 var app = express();31 app[method].bind(app, '/', 3).should.throw(/Number/);32 })bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 33–35why, one step back33 });34 })35throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 36–38why, one step back36 describe('decode querystring', function(){37 it('should decode correct params', function(done){38 var app = express();bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
line 39why, one step back39throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 40–42why, one step back40 app.get('/:name', function(req, res, next){41 res.send(req.params.name);42 });bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
line 43why, one step back43throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 44–47why, one step back44 request(app)45 .get('/foo%2Fbar')46 .expect('foo/bar', done);47 })bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
line 48why, one step back48throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 49–50why, one step back49 it('should not accept params in malformed paths', function(done) {50 var app = express();bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
line 51why, one step back51throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 52–54why, one step back52 app.get('/:name', function(req, res, next){53 res.send(req.params.name);54 });bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
line 55why, one step back55throw 400 in case of malformed pathsAlex Kocharinbefore adoption7b0dca0
lines 56–83why, one step back56 request(app)57 .get('/%foobar')58 .expect(400, done);59 })6061 it('should not decode spaces', function(done) {62 var app = express();6364 app.get('/:name', function(req, res, next){65 res.send(req.params.name);66 });6768 request(app)69 .get('/foo+bar')70 .expect('foo+bar', done);71 })7273 it('should work with unicode', function(done) {74 var app = express();7576 app.get('/:name', function(req, res, next){77 res.send(req.params.name);78 });7980 request(app)81 .get('/%ce%b1')82 .expect('\u03b1', done);83 })bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 84–130why, one step back84 })8586 it('should be .use()able', function(done){87 var app = express();8889 var calls = [];9091 app.use(function(req, res, next){92 calls.push('before');93 next();94 });9596 app.get('/', function(req, res, next){97 calls.push('GET /')98 next();99 });100101 app.use(function(req, res, next){102 calls.push('after');103 res.end();104 });105106 request(app)107 .get('/')108 .end(function(res){109 calls.should.eql(['before', 'GET /', 'after'])110 done();111 })112 })113114 describe('when given a regexp', function(){115 it('should match the pathname only', function(done){116 var app = express();117118 app.get(/^\/user\/[0-9]+$/, function(req, res){119 res.end('user');120 });121122 request(app)123 .get('/user/12?foo=bar')124 .expect('user', done);125 })126127 it('should populate req.params with the captures', function(done){128 var app = express();129130 app.get(/^\/user\/([0-9]+)\/(view|edit)?$/, function(req, res){Merge pull request #1835 from visionmedia/change-req-params-to-objectJonathan Ongbefore adoption3f14b4d
lines 131–132why, one step back131 var id = req.params[0]132 , op = req.params[1];bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 133–306why, one step back133 res.end(op + 'ing user ' + id);134 });135136 request(app)137 .get('/user/10/edit')138 .expect('editing user 10', done);139 })140 })141142 describe('case sensitivity', function(){143 it('should be disabled by default', function(done){144 var app = express();145146 app.get('/user', function(req, res){147 res.end('tj');148 });149150 request(app)151 .get('/USER')152 .expect('tj', done);153 })154155 describe('when "case sensitive routing" is enabled', function(){156 it('should match identical casing', function(done){157 var app = express();158159 app.enable('case sensitive routing');160161 app.get('/uSer', function(req, res){162 res.end('tj');163 });164165 request(app)166 .get('/uSer')167 .expect('tj', done);168 })169170 it('should not match otherwise', function(done){171 var app = express();172173 app.enable('case sensitive routing');174175 app.get('/uSer', function(req, res){176 res.end('tj');177 });178179 request(app)180 .get('/user')181 .expect(404, done);182 })183 })184 })185186 describe('trailing slashes', function(){187 it('should be optional by default', function(done){188 var app = express();189190 app.get('/user', function(req, res){191 res.end('tj');192 });193194 request(app)195 .get('/user/')196 .expect('tj', done);197 })198199 describe('when "strict routing" is enabled', function(){200 it('should match trailing slashes', function(done){201 var app = express();202203 app.enable('strict routing');204205 app.get('/user/', function(req, res){206 res.end('tj');207 });208209 request(app)210 .get('/user/')211 .expect('tj', done);212 })213214 it('should match no slashes', function(done){215 var app = express();216217 app.enable('strict routing');218219 app.get('/user', function(req, res){220 res.end('tj');221 });222223 request(app)224 .get('/user')225 .expect('tj', done);226 })227228 it('should fail when omitting the trailing slash', function(done){229 var app = express();230231 app.enable('strict routing');232233 app.get('/user/', function(req, res){234 res.end('tj');235 });236237 request(app)238 .get('/user')239 .expect(404, done);240 })241242 it('should fail when adding the trailing slash', function(done){243 var app = express();244245 app.enable('strict routing');246247 app.get('/user', function(req, res){248 res.end('tj');249 });250251 request(app)252 .get('/user/')253 .expect(404, done);254 })255 })256 })257258 it('should allow escaped regexp', function(done){259 var app = express();260261 app.get('/user/\\d+', function(req, res){262 res.end('woot');263 });264265 request(app)266 .get('/user/10')267 .end(function(err, res){268 res.statusCode.should.equal(200);269 request(app)270 .get('/user/tj')271 .expect(404, done);272 });273 })274275 it('should allow literal "."', function(done){276 var app = express();277278 app.get('/api/users/:from..:to', function(req, res){279 var from = req.params.from280 , to = req.params.to;281282 res.end('users from ' + from + ' to ' + to);283 });284285 request(app)286 .get('/api/users/1..50')287 .expect('users from 1 to 50', done);288 })289290 describe('*', function(){291 it('should denote a greedy capture group', function(done){292 var app = express();293294 app.get('/user/*.json', function(req, res){295 res.end(req.params[0]);296 });297298 request(app)299 .get('/user/tj.json')300 .expect('tj', done);301 })302303 it('should work with several', function(done){304 var app = express();305306 app.get('/api/*.*', function(req, res){Merge pull request #1835 from visionmedia/change-req-params-to-objectJonathan Ongbefore adoption3f14b4d
lines 307–308why, one step back307 var resource = req.params[0]308 , format = req.params[1];bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 309–541why, one step back309 res.end(resource + ' as ' + format);310 });311312 request(app)313 .get('/api/users/foo.bar.json')314 .expect('users/foo.bar as json', done);315 })316317 it('should work cross-segment', function(done){318 var app = express();319320 app.get('/api*', function(req, res){321 res.send(req.params[0]);322 });323324 request(app)325 .get('/api')326 .expect('', function(){327 request(app)328 .get('/api/hey')329 .expect('/hey', done);330 });331 })332333 it('should allow naming', function(done){334 var app = express();335336 app.get('/api/:resource(*)', function(req, res){337 var resource = req.params.resource;338 res.end(resource);339 });340341 request(app)342 .get('/api/users/0.json')343 .expect('users/0.json', done);344 })345346 it('should not be greedy immediately after param', function(done){347 var app = express();348349 app.get('/user/:user*', function(req, res){350 res.end(req.params.user);351 });352353 request(app)354 .get('/user/122')355 .expect('122', done);356 })357358 it('should eat everything after /', function(done){359 var app = express();360361 app.get('/user/:user*', function(req, res){362 res.end(req.params.user);363 });364365 request(app)366 .get('/user/122/aaa')367 .expect('122', done);368 })369370 it('should span multiple segments', function(done){371 var app = express();372373 app.get('/file/*', function(req, res){374 res.end(req.params[0]);375 });376377 request(app)378 .get('/file/javascripts/jquery.js')379 .expect('javascripts/jquery.js', done);380 })381382 it('should be optional', function(done){383 var app = express();384385 app.get('/file/*', function(req, res){386 res.end(req.params[0]);387 });388389 request(app)390 .get('/file/')391 .expect('', done);392 })393394 it('should require a preceeding /', function(done){395 var app = express();396397 app.get('/file/*', function(req, res){398 res.end(req.params[0]);399 });400401 request(app)402 .get('/file')403 .expect(404, done);404 })405 })406407 describe(':name', function(){408 it('should denote a capture group', function(done){409 var app = express();410411 app.get('/user/:user', function(req, res){412 res.end(req.params.user);413 });414415 request(app)416 .get('/user/tj')417 .expect('tj', done);418 })419420 it('should match a single segment only', function(done){421 var app = express();422423 app.get('/user/:user', function(req, res){424 res.end(req.params.user);425 });426427 request(app)428 .get('/user/tj/edit')429 .expect(404, done);430 })431432 it('should allow several capture groups', function(done){433 var app = express();434435 app.get('/user/:user/:op', function(req, res){436 res.end(req.params.op + 'ing ' + req.params.user);437 });438439 request(app)440 .get('/user/tj/edit')441 .expect('editing tj', done);442 })443 })444445 describe(':name?', function(){446 it('should denote an optional capture group', function(done){447 var app = express();448449 app.get('/user/:user/:op?', function(req, res){450 var op = req.params.op || 'view';451 res.end(op + 'ing ' + req.params.user);452 });453454 request(app)455 .get('/user/tj')456 .expect('viewing tj', done);457 })458459 it('should populate the capture group', function(done){460 var app = express();461462 app.get('/user/:user/:op?', function(req, res){463 var op = req.params.op || 'view';464 res.end(op + 'ing ' + req.params.user);465 });466467 request(app)468 .get('/user/tj/edit')469 .expect('editing tj', done);470 })471 })472473 describe('.:name', function(){474 it('should denote a format', function(done){475 var app = express();476477 app.get('/:name.:format', function(req, res){478 res.end(req.params.name + ' as ' + req.params.format);479 });480481 request(app)482 .get('/foo.json')483 .expect('foo as json', function(){484 request(app)485 .get('/foo')486 .expect(404, done);487 });488 })489 })490491 describe('.:name?', function(){492 it('should denote an optional format', function(done){493 var app = express();494495 app.get('/:name.:format?', function(req, res){496 res.end(req.params.name + ' as ' + (req.params.format || 'html'));497 });498499 request(app)500 .get('/foo')501 .expect('foo as html', function(){502 request(app)503 .get('/foo.json')504 .expect('foo as json', done);505 });506 })507 })508509 describe('when next() is called', function(){510 it('should continue lookup', function(done){511 var app = express()512 , calls = [];513514 app.get('/foo/:bar?', function(req, res, next){515 calls.push('/foo/:bar?');516 next();517 });518519 app.get('/bar', function(req, res){520 assert(0);521 });522523 app.get('/foo', function(req, res, next){524 calls.push('/foo');525 next();526 });527528 app.get('/foo', function(req, res, next){529 calls.push('/foo 2');530 res.end('done');531 });532533 request(app)534 .get('/foo')535 .expect('done', function(){536 calls.should.eql(['/foo/:bar?', '/foo', '/foo 2']);537 done();538 })539 })540 })541Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 542–565why, one step back542 describe('when next("route") is called', function(){543 it('should jump to next route', function(done){544 var app = express()545546 function fn(req, res, next){547 res.set('X-Hit', '1')548 next('route')549 }550551 app.get('/foo', fn, function(req, res, next){552 res.end('failure')553 });554555 app.get('/foo', function(req, res){556 res.end('success')557 })558559 request(app)560 .get('/foo')561 .expect('X-Hit', '1')562 .expect(200, 'success', done)563 })564 })565bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 566–619why, one step back566 describe('when next(err) is called', function(){567 it('should break out of app.router', function(done){568 var app = express()569 , calls = [];570571 app.get('/foo/:bar?', function(req, res, next){572 calls.push('/foo/:bar?');573 next();574 });575576 app.get('/bar', function(req, res){577 assert(0);578 });579580 app.get('/foo', function(req, res, next){581 calls.push('/foo');582 next(new Error('fail'));583 });584585 app.get('/foo', function(req, res, next){586 assert(0);587 });588589 app.use(function(err, req, res, next){590 res.end(err.message);591 })592593 request(app)594 .get('/foo')595 .expect('fail', function(){596 calls.should.eql(['/foo/:bar?', '/foo']);597 done();598 })599 })600 })601602 it('should allow rewriting of the url', function(done){603 var app = express();604605 app.get('/account/edit', function(req, res, next){606 req.user = { id: 12 }; // faux authenticated user607 req.url = '/user/' + req.user.id + '/edit';608 next();609 });610611 app.get('/user/:id/edit', function(req, res){612 res.send('editing user ' + req.params.id);613 });614615 request(app)616 .get('/account/edit')617 .expect('editing user 12', done);618 })619tests: add route ordering testDouglas Christopher Wilsonbefore adoptionc3470c9
lines 620–658why, one step back620 it('should run in order added', function(done){621 var app = express();622 var path = [];623624 app.get('*', function(req, res, next){625 path.push(0);626 next();627 });628629 app.get('/user/:id', function(req, res, next){630 path.push(1);631 next();632 });633634 app.use(function(req, res, next){635 path.push(2);636 next();637 });638639 app.all('/user/:id', function(req, res, next){640 path.push(3);641 next();642 });643644 app.get('*', function(req, res, next){645 path.push(4);646 next();647 });648649 app.use(function(req, res, next){650 path.push(5);651 res.end(path.join(','))652 });653654 request(app)655 .get('/user/1')656 .expect(200, '0,1,2,3,4,5', done);657 })658bump connect, mocha, and shouldJonathan Ongbefore adoption7c68822
lines 659–663why, one step back659 it('should be chainable', function(){660 var app = express();661 app.get('/', function(){}).should.equal(app);662 })663})