diff --git a/lib/router.js b/lib/router.js index 4d38bd3c1..013cd0f94 100644 --- a/lib/router.js +++ b/lib/router.js @@ -509,7 +509,7 @@ Router.prototype.find = function find(req, res, callback) { var v = semver.maxSatisfying(k, req.version()); if (v) { - if (!r || semver.gt(v, maxV)) { + if (!r || !maxV || semver.gt(v, maxV)) { r = c.r; params = c.p; maxV = v; diff --git a/test/server.test.js b/test/server.test.js index f9434e550..61ec8c0bd 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -1169,6 +1169,41 @@ test('versioned route matching should prefer \ }); +test('versioned route matching should not throw TypeError' , function (t) { + var p = '/path/' + uuid.v4(); + + SERVER.post({ + path: p, + version: ['1.1.0', '1.2.0'], + contentType: 'application/json' + }, function (req, res, next) { + res.json(200, {route: p}); + next(); + }); + + SERVER.post({ + path: '/path/:id', + version: ['1.1.0', '1.2.0'] + }, function (req, res, next) { + res.json(200, {route: 'id'}); + next(); + }); + + var opts = { + path: p, + headers: { + 'accept-version': '~1' + } + }; + + CLIENT.post(opts, function (err, _, res, obj) { + t.equal(obj.route, p); + t.end(); + }); + +}); + + test('GH-959 matchedVersion() should return on cached routes', function (t) { SERVER.get({