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

Ensure _matchedRoute is the most specific route. #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,23 +352,18 @@ Router.prototype.routes = Router.prototype.middleware = function () {

if (!matched.route) return next();

const matchedLayers = matched.pathAndMethod
const mostSpecificLayer = matchedLayers[matchedLayers.length - 1]
const mostSpecificLayer = ctx.matched.find((layer) => layer.methods.includes(ctx.method));
ctx._matchedRoute = mostSpecificLayer.path;
if (mostSpecificLayer.name) {
ctx._matchedRouteName = mostSpecificLayer.name;
}

layerChain = matchedLayers.reduce(function(memo, layer) {
layerChain = matched.pathAndMethod.reduce(function(memo, layer) {
memo.push(function(ctx, next) {
ctx.captures = layer.captures(path, ctx.captures);
ctx.params = ctx.request.params = layer.params(path, ctx.captures, ctx.params);
ctx.routerPath = layer.path;
ctx.routerName = layer.name;
ctx._matchedRoute = layer.path;
if (layer.name) {
ctx._matchedRouteName = layer.name;
}
return next();
});
return memo.concat(layer.stack);
Expand Down
89 changes: 89 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,95 @@ describe('Router', function () {
done();
});
});

it('places the most specific `_matchedRoute` and `_matchedRouteName` value on the context when using wildcard routes', function (done) {
const app = new Koa();
const router = new Router();
let matchedRoute = null;
let _matchedRouteName = null;
router.use(function (ctx, next) {
expect(ctx._matchedRoute).to.be(matchedRoute);
expect(ctx._matchedRouteName).to.be(_matchedRouteName);
return next();
});

router.get('foo', '/foo', function (ctx,) {
expect(ctx._matchedRoute).to.be('/foo');
expect(ctx._matchedRouteName).to.be('foo');
ctx.status = 200;
});

router.get('wildcard', '(.*)', function (ctx) {
expect(ctx._matchedRoute).to.be('(.*)');
expect(ctx._matchedRouteName).to.be('wildcard');
ctx.status = 200;
});

app.use(router.routes());

const server = http.createServer(app.callback());
matchedRoute = '/foo';
_matchedRouteName = 'foo';
request(server)
.get('/foo')
.expect(200)
.end(function (err) {
if (err) done(err);
matchedRoute = '(.*)';
_matchedRouteName = 'wildcard';
request(server)
.get('/bar')
.expect(200)
.end(done);
});
});

it('places the most specific `_matchedRoute` and `_matchedRouteName` value on the context when using multiple routers', function (done) {
const app = new Koa();
const router = new Router();
const router1 = new Router();
const router2 = new Router();
let matchedRoute = null;
let _matchedRouteName = null;
router.use(function (ctx, next) {
expect(ctx._matchedRoute).to.be(matchedRoute);
expect(ctx._matchedRouteName).to.be(_matchedRouteName);
return next();
});

router1.get('foo', '/foo', function (ctx) {
expect(ctx._matchedRoute).to.be('/foo');
expect(ctx._matchedRouteName).to.be('foo');
ctx.status = 200;
});

router2.get('bar', '/bar', function (ctx) {
expect(ctx._matchedRoute).to.be('/bar');
expect(ctx._matchedRouteName).to.be('bar');
ctx.status = 200;
});

router.use(router1.routes());
router.use(router2.routes());

app.use(router.routes());

const server = http.createServer(app.callback());
matchedRoute = '/foo';
_matchedRouteName = 'foo';
request(server)
.get('/foo')
.expect(200)
.end(function (err) {
if (err) done(err);
matchedRoute = '/bar';
_matchedRouteName = 'bar';
request(server)
.get('/bar')
.expect(200)
.end(done);
});
});
});

describe('If no HEAD method, default to GET', function () {
Expand Down