-
-
Save dazld/6507020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var assert = require('assert'), | |
director = require('director'), | |
EventEmitter = require('events').EventEmitter, | |
ns = require('continuation-local-storage').createNamespace('testing'), | |
express = require('express'), | |
app = express(); | |
/* | |
* CONSTANTS | |
*/ | |
var NUM_TESTS = 10; | |
function createRouter() { | |
var emitter = new EventEmitter(), | |
router = new director.http.Router(); | |
var createHandler = function() { | |
return function() { | |
assert(ns.get('req'), "req propagated to Router handler"); | |
emitter.emit('routeMatched', ns.get('req').someId); | |
}; | |
}; | |
var ret = { | |
_createHandler: createHandler, | |
initialize: function() { | |
router.on('get', '/page/:id', ret._createHandler( /* route, routeFunction */ )); | |
}, | |
on: emitter.on.bind(emitter), | |
once: emitter.once.bind(emitter), | |
dispatch: function(req, res) { | |
assert(ns.get('req'), "req propagated to Router dispatch"); | |
router.dispatch(req, res, function(err) { | |
console.log(err); | |
}); | |
} | |
}; | |
return ret; | |
} | |
var router = createRouter(); | |
router.initialize(); | |
// invoke run to give us a context we can use | |
app.use(function(req, res, next) { | |
ns.run(function(){ | |
req.someId = Date.now(); | |
ns.set('req', req); | |
ns.set('res', res); | |
next(); | |
}); | |
}); | |
app.get('*',function(req, res) { | |
// process.nextTick(function(){ // this all works when process.nextTick is uncommented! | |
router.dispatch(req, res, function() { | |
next(); | |
}); | |
// }); | |
}); | |
app.listen(4567); | |
// fake request test | |
function spawnAsync(i) { | |
var req = { | |
url: '/page/' + i, | |
method: 'get', | |
someId: Math.floor(Math.random() * Date.now()), | |
headers: { | |
'content-type': 'text/html' | |
} | |
}; | |
ns.set('req', req); | |
ns.set('res', { | |
send: function(){} // dummy for test | |
}); | |
process.nextTick(router.dispatch.bind(router, req)); | |
} | |
router.on('routeMatched', function(id) { | |
assert(id, "id passed to routeMatched listener"); | |
assert(ns.get('req'), "req propagated to routeMatched listener"); | |
assert.equal(id, ns.get('req').someId, "passed id and req.someId match"); | |
ns.get('res').send('ok'); | |
console.log('ok'); | |
}); | |
// actually do the thing | |
for (var i = 0; i < NUM_TESTS; i++) ns.run(spawnAsync.bind(null, i)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "clsbughunt", | |
"dependencies": { | |
"continuation-local-storage": "~2.1.1", | |
"director": "~1.2.0", | |
"express": "~3.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment