Merge branch 'refactor/route-middleware'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents eac6665, 5688ea6 · Browse files · 4 files·+21 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+21 added−24 removed
- tests:+17 added−3 removed
- other:+4 added−21 removed
Provenance
- Commit
06fda62- Tree
79a8286- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
eac6665- Parent 2
5688ea6
4 files changed+21 added−24 removed
modified:lib/http.js+3 added-20 removed
448 unmodified lines449 450 // Generate routing methods451 452 function generateMethod(method){453- Server.prototype[method] = function(path, fn){453+ Server.prototype[method] = function(path){454 var self = this;455 456 // Lookup457 if (1 == arguments.length) {2 unmodified lines460 : method);461 }462 463 // Ensure router is mounted464- if (!this.__usedRouter) {465- this.use(this.router);466- }464+ if (!this.__usedRouter) this.use(this.router);465 468- // Route specific middleware support469- if (arguments.length > 2) {470- var args = Array.prototype.slice.call(arguments, 1);471- fn = args.pop();472- (function stack(middleware){473- middleware.forEach(function(fn){474- if (Array.isArray(fn)) {475- stack(fn);476- } else {477- self[method](path, fn);478- }479- });480- })(args);481- }482-466 // Generate the route484- this.routes[method](path, fn);467+ this.routes[method].apply(this, arguments);468 return this;469 };470 return arguments.callee;471 }6 unmodified linesmodified:support/connectnot shown
Its text is not shown, modified.
modified:test/express.test.js+17 added-3 removed
433 unmodified lines434 { body: 'restored' }435 );436 },437 438- 'test route middleware': function(){439- var app = express.createServer();438+ 'test route middleware': function(beforeExit){439+ var app = express.createServer()440+ , calls = 0;441 442 function allow(role) {443 return function(req, res, next) {444 // this is totally not real, dont use this :)15 unmodified lines460 }461 }462 }463 464+ app.param('user', function(req, res, next, user){465+ ++calls;466+ next();467+ });468+469 app.get('/xxx', allow('member'), restrictAge(18), function(req, res){470 res.send(200);471 });472 3 unmodified lines476 477 app.get('/tobi', [allow('member')], [[restrictAge(18)]], function(req, res){478 res.send(200);479 });480+ 481+ app.get('/user/:user', [allow('member')], [[restrictAge(18)]], function(req, res){482+ res.send(200);483+ });484 475- ['xxx', 'booze', 'tobi'].forEach(function(thing){485+ ['xxx', 'booze', 'tobi', 'user/tj'].forEach(function(thing){486 assert.response(app,487 { url: '/' + thing },488 { body: 'Unauthorized', status: 401 });489 assert.response(app,2 unmodified lines492 assert.response(app,493 { url: '/' + thing, headers: { 'X-Role': 'member', 'X-Age': 18 }},494 { body: 'OK', status: 200 });495 });496+497+ beforeExit(function(){498+ calls.should.equal(3);499+ });500 },501 502 'test named capture groups': function(){503 var app = express.createServer();118 unmodified linesmodified:package.json+1 added-1 removed
8 unmodified lines91011121313141516175 unmodified lines8 unmodified lines { "name": "Ciaran Jessup", "email": "ciaranj@gmail.com" }, { "name": "Guillermo Rauch", "email": "rauchg@gmail.com" } ], "dependencies": { "connect": ">= 1.3.0 < 2.0.0", "connect": ">= 1.3.1 < 2.0.0", "mime": ">= 0.0.1", "qs": ">= 0.0.6" }, "keywords": ["framework", "sinatra", "web", "rest", "restful"],5 unmodified lines