diff --git a/lib/client.js b/lib/client.js index ba1f854..57c7b42 100644 --- a/lib/client.js +++ b/lib/client.js @@ -392,7 +392,11 @@ extend(Raven.prototype, { requestHandler: function () { var self = this; return function (req, res, next) { - self.context({}, next, next); + self.context({}, function () { + domain.active.add(req); + domain.active.add(res); + next(); + }, next); }; }, diff --git a/test/raven.client.js b/test/raven.client.js index 6b0ac1c..ea1e890 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -1018,4 +1018,32 @@ describe('raven.middleware', function () { client2.should.not.equal(client1); client2.should.be.an.instanceof(raven.constructor); }); + + it('should explicitly add req and res to the domain', function (done) { + var client = new raven.Client(dsn).install(); + var message = 'test breadcrumb'; + + var EventEmitter = require('events'); + if (process.version <= 'v0.11') EventEmitter = EventEmitter.EventEmitter; // node 0.10 compat + var e = new EventEmitter(); + e.on('done', function () { + // Context won't propagate here without the explicit binding of req/res done in the middleware + setTimeout(function () { + client.getContext().breadcrumbs.length.should.equal(1); + client.getContext().breadcrumbs[0].message.should.equal(message); + done(); + }, 0); + }); + + // Pass e as the req/res, so e will be added to the domain + client.requestHandler()(e, e, function () { + client.captureBreadcrumb({ + message: message, + category: 'log' + }); + setTimeout(function () { + e.emit('done'); + }, 0); + }); + }); });