Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: 652 - Incorrect error on route with no versions (#1465)
  • Loading branch information
karthikiyengar authored and DonutEspresso committed Sep 13, 2017
1 parent 925b2cf commit ee15490
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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

0 comments on commit ee15490

Please sign in to comment.