diff --git a/index.js b/index.js index 32fd008..551c2cc 100644 --- a/index.js +++ b/index.js @@ -267,6 +267,10 @@ Router.prototype._on = function _on (method, path, opts, handler, store) { params.push('*') currentNode = currentNode.createWildcardChild() parentNodePathIndex = i + 1 + + if (i !== path.length - 1) { + throw new Error('Wildcard must be the last character in the route') + } } } diff --git a/test/issue-28.test.js b/test/issue-28.test.js index fb901e6..c25d3ef 100644 --- a/test/issue-28.test.js +++ b/test/issue-28.test.js @@ -601,3 +601,19 @@ test('Wildcard node with constraints', t => { null ) }) + +test('Wildcard must be the last character in the route', (t) => { + t.plan(6) + + const expectedError = new Error('Wildcard must be the last character in the route') + + const findMyWay = FindMyWay() + + t.throws(() => findMyWay.on('GET', '*1', () => {}), expectedError) + t.throws(() => findMyWay.on('GET', '*/', () => {}), expectedError) + t.throws(() => findMyWay.on('GET', '*?', () => {}), expectedError) + + t.throws(() => findMyWay.on('GET', '/foo*123', () => {}), expectedError) + t.throws(() => findMyWay.on('GET', '/foo*?', () => {}), expectedError) + t.throws(() => findMyWay.on('GET', '/foo*/', () => {}), expectedError) +})