Skip to content

Commit 1bcedc7

Browse files
authored
Fix path params match issue (#159) (#160)
1 parent 4e9d226 commit 1bcedc7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ Router.prototype.find = function find (method, path, version) {
421421
return this._getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard)
422422
}
423423

424-
var goBack = this.ignoreTrailingSlash ? previousPath : '/' + previousPath
424+
var goBack = previousPath.charCodeAt(0) === 47 ? previousPath : '/' + previousPath
425425
if (originalPath.indexOf(goBack) === -1) {
426426
// we need to know the outstanding path so far from the originalPath since the last encountered "/" and assign it to previousPath.
427427
// e.g originalPath: /aa/bbb/cc, path: bb/cc

test/path-params-match.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const FindMyWay = require('../')
5+
6+
t.test('path params match', (t) => {
7+
t.plan(12)
8+
9+
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
10+
11+
const b1Path = function b1StaticPath () {}
12+
const b2Path = function b2StaticPath () {}
13+
const cPath = function cStaticPath () {}
14+
const paramPath = function parameterPath () {}
15+
16+
findMyWay.on('GET', '/ab1', b1Path)
17+
findMyWay.on('GET', '/ab2', b2Path)
18+
findMyWay.on('GET', '/ac', cPath)
19+
findMyWay.on('GET', '/:pam', paramPath)
20+
21+
t.equals(findMyWay.find('GET', '/ab1').handler, b1Path)
22+
t.equals(findMyWay.find('GET', '/ab1/').handler, b1Path)
23+
t.equals(findMyWay.find('GET', '/ab2').handler, b2Path)
24+
t.equals(findMyWay.find('GET', '/ab2/').handler, b2Path)
25+
t.equals(findMyWay.find('GET', '/ac').handler, cPath)
26+
t.equals(findMyWay.find('GET', '/ac/').handler, cPath)
27+
t.equals(findMyWay.find('GET', '/foo').handler, paramPath)
28+
t.equals(findMyWay.find('GET', '/foo/').handler, paramPath)
29+
30+
const noTrailingSlashRet = findMyWay.find('GET', '/abcdef')
31+
t.equals(noTrailingSlashRet.handler, paramPath)
32+
t.deepEqual(noTrailingSlashRet.params, { pam: 'abcdef' })
33+
34+
const trailingSlashRet = findMyWay.find('GET', '/abcdef/')
35+
t.equals(trailingSlashRet.handler, paramPath)
36+
t.deepEqual(trailingSlashRet.params, { pam: 'abcdef' })
37+
})

0 commit comments

Comments
 (0)