Skip to content

Commit

Permalink
router.url behaves unexpectedly for a named route that has no path …
Browse files Browse the repository at this point in the history
…parameters. (#37)

* demonstrate issue

* fix
  • Loading branch information
panva authored Jul 5, 2020
1 parent ae0745c commit 1b06bd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Layer.prototype.url = function (params, options) {
}
} else if (tokens.some(token => token.name)) {
replace = params;
} else {
} else if (!options) {
options = params;
}

Expand Down
28 changes: 26 additions & 2 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,32 @@ describe('Router', function () {
})

it('generates URL for given route name without params and query params', function(done) {
const app = new Koa();
const router = new Router();
var router = new Router();
router.get('books', '/books', function (ctx) {
ctx.status = 204;
});
var url = router.url('books');
url.should.equal('/books');
var url = router.url('books');
url.should.equal('/books', {});
var url = router.url('books');
url.should.equal('/books', {}, {});
var url = router.url('books',
{},
{ query: { page: 3, limit: 10 } }
);
url.should.equal('/books?page=3&limit=10');
var url = router.url('books',
{},
{ query: 'page=3&limit=10' }
);
url.should.equal('/books?page=3&limit=10');
done();
})


it('generates URL for given route name without params and query params', function(done) {
var router = new Router();
router.get('category', '/category', function (ctx) {
ctx.status = 204;
});
Expand Down

0 comments on commit 1b06bd1

Please sign in to comment.