Skip to content
Myr

Added test for multiple 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 · parent 1722e2c · Browse files · 1 file+40 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+40 added−0 removed

  • tests:+40 added−0 removed

1 file+40 added−0 removed

modified:test/express.test.js+40 added
319 unmodified lines
320 { body: 'blog post 12' });321 assert.response(blog,322 { url: '/' },323 { body: 'blog index' });324 },325 326 'test route middleware': function(assert){327 var app = express.createServer();328329 function allow(role) {330 return function(req, res, next) {331 // this is totally not real, dont use this :)332 // for tests only333 if (req.headers['x-role'] == role) {334 next();335 } else {336 res.send(401);337 }338 }339 }340 341 function restrictAge(age) {342 return function(req, res, next){343 if (req.headers['x-age'] >= age) {344 next();345 } else {346 res.send(403);347 }348 }349 }350351 app.get('/xxx', allow('member'), restrictAge(18), function(req, res){352 res.send(200);353 });354 355 assert.response(app,356 { url: '/xxx' },357 { body: 'Unauthorized', status: 401 });358 assert.response(app,359 { url: '/xxx', headers: { 'X-Role': 'member' }},360 { body: 'Forbidden', status: 403 });361 assert.response(app,362 { url: '/xxx', headers: { 'X-Role': 'member', 'X-Age': 18 }},363 { body: 'OK', status: 200 });364 }365};