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

Fix: 652 - Incorrect error on route with no versions #1465

Merged
merged 4 commits into from Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions lib/router.js
Expand Up @@ -468,9 +468,16 @@ Router.prototype.find = function find(req, res, callback) {
// not the first one. However, if neither the client nor
// server specified any version, we're done, because neither
// cared
if (routes[i].versions.length === 0 && req.version() === '*') {
r = routes[i];
break;
if (routes[i].versions.length === 0) {
if (req.version() === '*') {
r = routes[i];
break;
}
callback(new InvalidVersionError('%s is not supported by %s %s',
req.version() || '?',
req.method,
req.path()));
return;
}

if (routes[i].versions.length > 0) {
Expand Down
41 changes: 41 additions & 0 deletions test/server.test.js
Expand Up @@ -1204,6 +1204,47 @@ test('versioned route matching should not throw TypeError' , function (t) {
});


test('GH-652 throw InvalidVersion on version mismatch', function (t) {
function response (req, res, next) {
return res.send(req.route.version);
}
SERVER.get({ path: '/ping', version: '1.0.1' }, response);
SERVER.listen(0, '127.0.0.1', function () {
var opts = {
path: '/ping',
headers: {
'accept-version': '1.0.2'
}
};
CLIENT.get(opts, function (err, req, res, data) {
t.equal(res.statusCode, 400);
t.equal(data.code, 'InvalidVersion');
t.done();
});
});
});

test('GH-652 throw InvalidVersion on non-versioned route', function (t) {
function response (req, res, next) {
return res.send(req.route.version);
}
SERVER.get({ path: '/ping' }, response);
SERVER.listen(0, '127.0.0.1', function () {
var opts = {
path: '/ping',
headers: {
'accept-version': '1.0.1'
}
};
CLIENT.get(opts, function (err, req, res, data) {
t.equal(res.statusCode, 400);
t.equal(data.code, 'InvalidVersion');
t.done();
});
});
});


test('GH-959 matchedVersion() should return on cached routes', function (t) {

SERVER.get({
Expand Down