Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server _routeErrorResponse avoiding uncaughtException by only sending response when not sent #1568

Merged
merged 5 commits into from Nov 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/server.test.js
Expand Up @@ -224,6 +224,33 @@ test('rm route and clear cached route', function(t) {
});
});

test('_routeErrorResponse does not cause uncaughtException when called when header has already been sent', function(
t
) {
SERVER.on('MethodNotAllowed', function(req, res, error, next) {
res.json(405, { status: 'MethodNotAllowed' });
var nextHasErrored;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this:

try {
    next();
} catch (err) {
    t.fail('next() should not throw error when header has already been sent');
}
t.end();

try {
next();
// eslint-disable-next-line no-empty
} catch (err) {
nextHasErrored = new Error('next Error');
}

t.ifError(nextHasErrored);
t.end();
});

SERVER.post('/routePostOnly', function tester(req, res, next) {
next();
});

CLIENT.get('/routePostOnly', function(err, _, res) {
t.ok(err);
t.equal(res.statusCode, 405);
});
});

test('GH-1171: rm one version of the routes, other versions should still work', function(
t
) {
Expand Down