express.test.js
This file
Path
test/express.test.js
525 lines · 12.8 KiB · JavaScript
Last changed by
Merge branch 'features/https'Tj Holowaychukbefore adoption8261437
History of this fileTake it
The walk stopped after 200 commits: lines older than ce2a18f point at it.
master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 1why, one step back1using should / refactored testsTj Holowaychukbefore adoption643879f
lines 2–4why, one step back2/**3 * Module dependencies.4 */master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 5why, one step back5using should / refactored testsTj Holowaychukbefore adoption643879f
lines 6–10why, one step back6var express = require('express')7 , connect = require('connect')8 , assert = require('assert')9 , should = require('should');10master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 11why, one step back11module.exports = {using should / refactored testsTj Holowaychukbefore adoption643879f
lines 12–13why, one step back12 'test inheritance': function(){13 var server = express.createServer();Merge branch 'features/https'Tj Holowaychukbefore adoption8261437
line 14why, one step back14 server.should.be.an.instanceof(connect.HTTPServer);using should / refactored testsTj Holowaychukbefore adoption643879f
lines 15–20why, one step back15 },16 17 'test connect middleware autoloaders': function(){18 express.errorHandler.should.equal(connect.errorHandler);19 },20 Added failing middleware precedence testsTj Holowaychukbefore adoption898fb0f
lines 21–30why, one step back21 'test createServer() precedence': function(){22 var app = express.createServer(function(req, res){23 res.send(req.query.bar);24 });25 26 assert.response(app,27 { url: '/foo?bar=baz' },28 { body: 'baz' });29 },30 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 31–51why, one step back31 'test basic server': function(){32 var server = express.createServer();3334 server.get('/', function(req, res){35 server.set('env').should.equal('test');36 res.writeHead(200, {});37 res.end('wahoo');38 });3940 server.put('/user/:id', function(req, res){41 res.writeHead(200, {});42 res.end('updated user ' + req.params.id)43 });4445 server.del('/something', function(req, res){46 res.send('Destroyed');47 });4849 server.delete('/something/else', function(req, res){50 res.send('Destroyed');51 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 52why, one step back52 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 53–60why, one step back53 server.all('/staff/:id', function(req, res, next){54 req.staff = { id: req.params.id };55 next();56 });5758 server.get('/staff/:id', function(req, res){59 res.send('GET Staff ' + req.staff.id);60 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 61why, one step back61 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 62–68why, one step back62 server.post('/staff/:id', function(req, res){63 res.send('POST Staff ' + req.staff.id);64 });65 66 server.all('*', function(req, res){67 res.send('requested ' + req.url);68 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 69why, one step back69using should / refactored testsTj Holowaychukbefore adoption643879f
lines 70–76why, one step back70 assert.response(server,71 { url: '/' },72 { body: 'wahoo' });73 74 assert.response(server,75 { url: '/user/12', method: 'PUT' },76 { body: 'updated user 12' });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 77why, one step back77using should / refactored testsTj Holowaychukbefore adoption643879f
lines 78–80why, one step back78 assert.response(server,79 { url: '/something', method: 'DELETE' },80 { body: 'Destroyed' });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 81why, one step back81using should / refactored testsTj Holowaychukbefore adoption643879f
lines 82–84why, one step back82 assert.response(server,83 { url: '/something/else', method: 'DELETE' },84 { body: 'Destroyed' });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 85why, one step back85using should / refactored testsTj Holowaychukbefore adoption643879f
lines 86–100why, one step back86 assert.response(server,87 { url: '/staff/12' },88 { body: 'GET Staff 12' });89 90 assert.response(server,91 { url: '/staff/12', method: 'POST' },92 { body: 'POST Staff 12' });93 94 assert.response(server,95 { url: '/foo/bar/baz', method: 'DELETE' },96 { body: 'requested /foo/bar/baz' });97 },98 99 'test constructor middleware': function(beforeExit){100 var calls = [];master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 101why, one step back101using should / refactored testsTj Holowaychukbefore adoption643879f
lines 102–105why, one step back102 function one(req, res, next){103 calls.push('one');104 next();105 }master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 106why, one step back106using should / refactored testsTj Holowaychukbefore adoption643879f
lines 107–110why, one step back107 function two(req, res, next){108 calls.push('two');109 next();110 }master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 111why, one step back111using should / refactored testsTj Holowaychukbefore adoption643879f
lines 112–125why, one step back112 var app = express.createServer(one, two);113 app.get('/', function(req, res){114 res.writeHead(200, {});115 res.end('foo bar');116 });117 118 assert.response(app,119 { url: '/' },120 { body: 'foo bar' });121 122 beforeExit(function(){123 calls.should.eql(['one', 'two']);124 });125 },master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 126why, one step back126 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 127–129why, one step back127 'test #error()': function(){128 // Passing down middleware stack129 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 130why, one step back130 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 131–133why, one step back131 app.get('/', function(req, res, next){132 next(new Error('broken'));133 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 134why, one step back134 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 135–139why, one step back135 app.use('/', connect.errorHandler());136 137 assert.response(app,138 { url: '/' },139 { body: 'Internal Server Error' });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 140why, one step back140using should / refactored testsTj Holowaychukbefore adoption643879f
lines 141–146why, one step back141 // Custom handler142 var app = express.createServer();143 144 app.error(function(err, req, res){145 res.send('Shit: ' + err.message, 500);146 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 147why, one step back147using should / refactored testsTj Holowaychukbefore adoption643879f
lines 148–169why, one step back148 app.get('/', function(req, res, next){149 next(new Error('broken'));150 });151 152 assert.response(app,153 { url: '/' },154 { body: 'Shit: broken', status: 500 });155 156 // Multiple error()s157 var app = express.createServer();158 159 app.error(function(err, req, res, next){160 if (err.message === 'broken') {161 next(err);162 } else {163 res.send(500);164 }165 });166 167 app.error(function(err, req, res, next){168 res.send(err.message, 500);169 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 170why, one step back170using should / refactored testsTj Holowaychukbefore adoption643879f
lines 171–176why, one step back171 app.get('/', function(req, res, next){172 throw new Error('broken');173 });174 app.get('/foo', function(req, res, next){175 throw new Error('oh noes');176 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 177why, one step back177 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 178–185why, one step back178 assert.response(app,179 { url: '/' },180 { body: 'broken', status: 500 });181 assert.response(app,182 { url: '/foo' },183 { body: 'Internal Server Error' });184 },185 Updated connect submoduleTj Holowaychukbefore adoptioncd996f9
lines 186–191why, one step back186 'test error() with route-specific middleware': function(){187 var app = express.createServer();188189 190 },191 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 192–193why, one step back192 'test next()': function(){193 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 194why, one step back194 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 195–221why, one step back195 app.get('/user.:format?', function(req, res, next){196 switch (req.params.format) {197 case 'json':198 res.writeHead(200, {});199 res.end('some json');200 break;201 default:202 next();203 }204 });205 206 app.get('/user', function(req, res){207 res.writeHead(200, {});208 res.end('no json :)');209 });210 211 assert.response(app,212 { url: '/user.json' },213 { body: 'some json' });214 215 assert.response(app,216 { url: '/user' },217 { body: 'no json :)' });218 },219 220 'test #use()': function(){221 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 222why, one step back222using should / refactored testsTj Holowaychukbefore adoption643879f
lines 223–236why, one step back223 app.get('/users', function(req, res, next){224 next(new Error('fail!!'));225 });226 app.use('/', connect.errorHandler({ showMessage: true }));227 228 assert.response(app,229 { url: '/users' },230 { body: 'Error: fail!!' });231 },232 233 'test #configure()': function(beforeExit){234 var calls = [];235 process.env.NODE_ENV = 'development';236 var server = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 237why, one step back237 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 238–246why, one step back238 // Config blocks239 var ret = server.configure(function(){240 assert.equal(this, server, 'Test context of configure() is the server');241 calls.push('any');242 }).configure('development', function(){243 calls.push('dev');244 }).configure('production', function(){245 calls.push('production');246 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 247why, one step back247using should / refactored testsTj Holowaychukbefore adoption643879f
line 248why, one step back248 should.equal(ret, server, 'Test #configure() returns server for chaining');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 249why, one step back249using should / refactored testsTj Holowaychukbefore adoption643879f
lines 250–252why, one step back250 assert.response(server,251 { url: '/' },252 { body: 'Cannot GET /' });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 253why, one step back253using should / refactored testsTj Holowaychukbefore adoption643879f
lines 254–262why, one step back254 beforeExit(function(){255 calls.should.eql(['any', 'dev']);256 });257 },258 259 'test #configure() immediate call': function(){260 var app = express.createServer();261262 app.configure(function(){Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae
line 263why, one step back263 app.use(connect.bodyParser());using should / refactored testsTj Holowaychukbefore adoption643879f
line 264why, one step back264 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 265why, one step back265 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 266–268why, one step back266 app.post('/', function(req, res){267 res.send(req.param('name') || 'nope');268 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 269why, one step back269using should / refactored testsTj Holowaychukbefore adoption643879f
lines 270–273why, one step back270 assert.response(app,271 { url: '/', method: 'POST', data: 'name=tj', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},272 { body: 'tj' });273 },master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 274why, one step back274using should / refactored testsTj Holowaychukbefore adoption643879f
lines 275–276why, one step back275 'test #configure() precedence': function(){276 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 277why, one step back277using should / refactored testsTj Holowaychukbefore adoption643879f
lines 278–288why, one step back278 app.configure(function(){279 app.use(function(req, res, next){280 res.writeHead(200, {});281 res.write('first');282 next();283 });284 app.use(app.router);285 app.use(function(req, res, next){286 res.end('last');287 });288 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 289why, one step back289 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 290–311why, one step back290 app.get('/', function(req, res, next){291 res.write(' route ');292 next();293 });294295 assert.response(app,296 { url: '/' },297 { body: 'first route last' });298 },299 300 'test #set()': function(){301 var app = express.createServer();302 var ret = app.set('title', 'My App').set('something', 'else');303 ret.should.equal(app);304 app.set('title').should.equal('My App');305 app.set('something').should.equal('else');306 },307 308 'test #enable()': function(){309 var app = express.createServer();310 var ret = app.enable('some feature');311 ret.should.equal(app);Added app.disabled() and app.enabled()Tj Holowaychukbefore adoption749136b
lines 312–314why, one step back312 app.set('some feature').should.be.true;313 app.enabled('some feature').should.be.true;314 app.enabled('something else').should.be.false;using should / refactored testsTj Holowaychukbefore adoption643879f
lines 315–320why, one step back315 },316 317 'test #disable()': function(){318 var app = express.createServer();319 var ret = app.disable('some feature');320 ret.should.equal(app);Added app.disabled() and app.enabled()Tj Holowaychukbefore adoption749136b
lines 321–323why, one step back321 app.set('some feature').should.be.false;322 app.disabled('some feature').should.be.true;323 app.disabled('something else').should.be.true;using should / refactored testsTj Holowaychukbefore adoption643879f
lines 324–327why, one step back324 },325 326 'test middleware precedence': function(){327 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 328why, one step back328 Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae
line 329why, one step back329 app.use(connect.bodyParser());using should / refactored testsTj Holowaychukbefore adoption643879f
lines 330–331why, one step back330331 assert.equal(2, app.stack.length);master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 332why, one step back332 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 333–352why, one step back333 app.post('/', function(req, res){334 res.send(JSON.stringify(req.body || ''));335 });336 app.get('/', function(){337338 });339 assert.equal(3, app.stack.length);340341 assert.response(app,342 { url: '/', method: 'POST', data: 'name=tj', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},343 { body: '{"name":"tj"}' });344 },345 346 'test mounting': function(){347 var called348 , app = express.createServer()349 , blog = express.createServer()350 , map = express.createServer();351352 map.set('home', '/map');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 353why, one step back353 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 354–358why, one step back354 map.mounted(function(parent){355 called = true;356 assert.equal(this, map, 'mounted() is not in context of the child app');357 assert.equal(app, parent, 'mounted() was not called with parent app');358 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 359why, one step back359 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 360–364why, one step back360 app.use('/blog', blog);361 app.use('/contact', map);362 blog.route.should.equal('/blog');363 map.route.should.equal('/contact');364 should.equal(true, called);master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 365why, one step back365using should / refactored testsTj Holowaychukbefore adoption643879f
lines 366–367why, one step back366 app.set("test", "parent setting");367 blog.set('test').should.equal('parent setting');master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 368why, one step back368 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 369–374why, one step back369 app.get('/', function(req, res){370 app.set('home').should.equal('/');371 blog.set('home').should.equal('/blog');372 map.set('home').should.equal('/contact/map');373 res.send('main app');374 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 375why, one step back375using should / refactored testsTj Holowaychukbefore adoption643879f
lines 376–378why, one step back376 blog.get('/', function(req, res){377 res.send('blog index');378 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 379why, one step back379 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 380–399why, one step back380 blog.get('/post/:id', function(req, res){381 res.send('blog post ' + req.params.id);382 });383 384 assert.response(app,385 { url: '/' },386 { body: 'main app' });387 assert.response(app,388 { url: '/blog' },389 { body: 'blog index' });390 assert.response(app,391 { url: '/blog/post/12' },392 { body: 'blog post 12' });393 assert.response(blog,394 { url: '/' },395 { body: 'blog index' });396 },397 398 'test route middleware': function(){399 var app = express.createServer();master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 400why, one step back400using should / refactored testsTj Holowaychukbefore adoption643879f
lines 401–408why, one step back401 function allow(role) {402 return function(req, res, next) {403 // this is totally not real, dont use this :)404 // for tests only405 if (req.headers['x-role'] == role) {406 next();407 } else {408 res.send(401);master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 409why, one step back409 }using should / refactored testsTj Holowaychukbefore adoption643879f
lines 410–418why, one step back410 }411 }412 413 function restrictAge(age) {414 return function(req, res, next){415 if (req.headers['x-age'] >= age) {416 next();417 } else {418 res.send(403);master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 419why, one step back419 }using should / refactored testsTj Holowaychukbefore adoption643879f
lines 420–421why, one step back420 }421 }master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 422why, one step back422using should / refactored testsTj Holowaychukbefore adoption643879f
lines 423–425why, one step back423 app.get('/xxx', allow('member'), restrictAge(18), function(req, res){424 res.send(200);425 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 426why, one step back426using should / refactored testsTj Holowaychukbefore adoption643879f
lines 427–429why, one step back427 app.get('/booze', [allow('member')], restrictAge(18), function(req, res){428 res.send(200);429 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 430why, one step back430using should / refactored testsTj Holowaychukbefore adoption643879f
lines 431–433why, one step back431 app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){432 res.send(200);433 });master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 434why, one step back434using should / refactored testsTj Holowaychukbefore adoption643879f
lines 435–445why, one step back435 ['xxx', 'booze', 'tobi'].forEach(function(thing){436 assert.response(app,437 { url: '/' + thing },438 { body: 'Unauthorized', status: 401 });439 assert.response(app,440 { url: '/' + thing, headers: { 'X-Role': 'member' }},441 { body: 'Forbidden', status: 403 });442 assert.response(app,443 { url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},444 { body: 'OK', status: 200 });445 });Added Server#param()Tj Holowaychukbefore adoption3e06586
lines 446–447why, one step back446 },447 DocsTj Holowaychukbefore adoptionbba049b
lines 448–450why, one step back448 'test named capture groups': function(){449 var app = express.createServer();450Test for named capture groupsTj Holowaychukbefore adoptionabd0020
line 451why, one step back451 app.get('/user/:id([0-9]{2,10})', function(req, res){DocsTj Holowaychukbefore adoptionbba049b
lines 452–457why, one step back452 res.send('user ' + req.params.id);453 });454455 assert.response(app,456 { url: '/user/12' },457 { body: 'user 12' });Test for named capture groupsTj Holowaychukbefore adoptionabd0020
lines 458–461why, one step back458 459 assert.response(app,460 { url: '/user/ab' },461 { body: 'Cannot GET /user/ab' });DocsTj Holowaychukbefore adoptionbba049b
lines 462–463why, one step back462 },463 Added Server#param()Tj Holowaychukbefore adoption3e06586
lines 464–475why, one step back464 'test .param()': function(){465 var app = express.createServer();466467 var users = [468 { name: 'tj' }469 , { name: 'tobi' }470 , { name: 'loki' }471 , { name: 'jane' }472 , { name: 'bandit' }473 ];474475 function integer(n){ return parseInt(n, 10); };Added Array support to Server#param()Tj Holowaychukbefore adoptionf843093
line 476why, one step back476 app.param(['to', 'from'], integer);Added Server#param()Tj Holowaychukbefore adoption3e06586
lines 477–507why, one step back477478 app.param('user', function(req, res, next, id){479 if (req.user = users[id]) {480 next();481 } else {482 next(new Error('failed to find user'));483 }484 });485486 app.get('/user/:user', function(req, res, next){487 res.send('user ' + req.user.name);488 });489490 app.get('/users/:from-:to', function(req, res, next){491 var names = users.slice(req.params.from, req.params.to).map(function(user){492 return user.name;493 });494 res.send('users ' + names.join(', '));495 });496 497 assert.response(app,498 { url: '/user/0' },499 { body: 'user tj' });500 501 assert.response(app,502 { url: '/user/1' },503 { body: 'user tobi' });504 505 assert.response(app,506 { url: '/users/0-3' },507 { body: 'users tj, tobi, loki' });Merge branch 'OPTIONS'Tj Holowaychukbefore adoption365b7fe
lines 508–523why, one step back508 },509 510 'test OPTIONS': function(){511 var app = express.createServer();512 513 app.get('/', function(){});514 app.get('/user/:id', function(){});515 app.put('/user/:id', function(){});516 517 assert.response(app,518 { url: '/', method: 'OPTIONS' },519 { headers: { Allow: 'GET' }});520 521 assert.response(app,522 { url: '/user/12', method: 'OPTIONS' },523 { headers: { Allow: 'GET,PUT' }});using should / refactored testsTj Holowaychukbefore adoption643879f
line 524why, one step back524 }master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f
line 525why, one step back525};