Skip to content

Commit

Permalink
bind handler to domain only when a domain is present
Browse files Browse the repository at this point in the history
  • Loading branch information
sirgallifrey committed Dec 1, 2016
1 parent ad8a9af commit 0cfb811
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internals.prerequisites = function (request, callback) {
}, nextSet);
};

const domain = process.domain; // Save a reference to the current domain
const domain = request.domain; // Save a reference to the current domain

Items.serial(request._route._prerequisites, each, (err) => {

if (err) {
return callback(err);
}

const wrapped = domain.bind(internals.handler);
const wrapped = domain ? domain.bind(internals.handler) : internals.handler;
return wrapped(request, callback);
});
};
Expand Down
39 changes: 39 additions & 0 deletions test/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,45 @@ describe('handler', () => {
expect(res.statusCode).to.equal(500);
});
});

it('works without a domain', (done) => {

const pre = function (req, reply) {

reply('pre');
};

const handler = function (req, reply) {

reply('working').code(200);
};

// this will cause the test to stop on error but is needed to test #3399
const domain = process.domain;
process.domain = undefined;

const server = new Hapi.Server({ useDomains: false });
server.connection();

server.route({
method: 'get',
path: '/',
config: {
pre: [{
method: pre
}],
handler
}
});

server.inject('/', (res) => {

expect(res.statusCode).to.equal(200);
process.domain = domain;
done();
});

});
});

describe('handler()', () => {
Expand Down

0 comments on commit 0cfb811

Please sign in to comment.