Skip to content

Commit

Permalink
Router.use do not receive ctx.param(s) when route has prefix with par…
Browse files Browse the repository at this point in the history
…ams (#69)

* add failing test for prefix param(s) not being passed to .use

* update router to not ignore captures if route has prefix

* check if router prefix has params before considering disabling captures

* simplify test case

* make the default matcher ([^\/]*) instead of (.*) for prefix

* add more tests

* move new tests to router#prefix

* fix require of path-to-regexp

* pathToRegExp -> pathToRegexp

* update pathToRegexp usage to match refactored path-to-regexp
  • Loading branch information
cymen committed Jul 5, 2020
1 parent cc756b6 commit 13c658d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const compose = require('koa-compose');
const HttpError = require('http-errors');
const methods = require('methods');
const Layer = require('./layer');
const { pathToRegexp } = require('path-to-regexp');

/**
* @module koa-router
Expand Down Expand Up @@ -289,7 +290,10 @@ Router.prototype.use = function () {
setRouterParams(Object.keys(router.params));
}
} else {
router.register(path || '(.*)', [], m, { end: false, ignoreCaptures: !hasPath });
const keys = [];
pathToRegexp(router.opts.prefix || '', keys);
const routerPrefixHasParam = router.opts.prefix && keys.length;
router.register(path || '([^\/]*)', [], m, { end: false, ignoreCaptures: !hasPath && !routerPrefixHasParam });
}
}

Expand Down
82 changes: 82 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1839,6 +1839,88 @@ describe('Router', function () {
expect(route.paramNames[1]).to.have.property('name', 'id');
});


it('populates ctx.params correctly for router prefix (including use)', function(done) {
var app = new Koa();
var router = new Router({ prefix: '/:category' });
app.use(router.routes());
router
.use((ctx, next) => {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
return next();
})
.get('/suffixHere', function(ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/cats/suffixHere')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
});
});

it('populates ctx.params correctly for more complex router prefix (including use)', function(done) {
var app = new Koa();
var router = new Router({ prefix: '/:category/:color' });
app.use(router.routes());
router
.use((ctx, next) => {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
ctx.params.should.have.property('color', 'gray');
return next();
})
.get('/:active/suffixHere', function(ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'cats');
ctx.params.should.have.property('color', 'gray');
ctx.params.should.have.property('active', 'true');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/cats/gray/true/suffixHere')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
});
});

it('populates ctx.params correctly for static prefix', function(done) {
var app = new Koa();
var router = new Router({ prefix: '/all' });
app.use(router.routes());
router
.use((ctx, next) => {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.be.empty();
return next();
})
.get('/:active/suffixHere', function(ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('active', 'true');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/all/true/suffixHere')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
});
});

describe('when used with .use(fn) - gh-247', function () {
it('does not set params.0 to the matched path', function (done) {
const app = new Koa();
Expand Down

0 comments on commit 13c658d

Please sign in to comment.