Skip to content

Commit 275f03f

Browse files
authored
No match path param with ignoreTrailingSlash (#146)
* add failing test * fix more strict test * fix param path with trailing slash * better check * more test check
1 parent 6a3bfaf commit 275f03f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

index.js

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

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

test/issue-145.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const FindMyWay = require('../')
5+
6+
t.test('issue-145', (t) => {
7+
t.plan(8)
8+
9+
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
10+
11+
const fixedPath = function staticPath () {}
12+
const varPath = function parameterPath () {}
13+
findMyWay.on('GET', '/a/b', fixedPath)
14+
findMyWay.on('GET', '/a/:pam/c', varPath)
15+
16+
t.equals(findMyWay.find('GET', '/a/b').handler, fixedPath)
17+
t.equals(findMyWay.find('GET', '/a/b/').handler, fixedPath)
18+
t.equals(findMyWay.find('GET', '/a/b/c').handler, varPath)
19+
t.equals(findMyWay.find('GET', '/a/b/c/').handler, varPath)
20+
t.equals(findMyWay.find('GET', '/a/foo/c').handler, varPath)
21+
t.equals(findMyWay.find('GET', '/a/foo/c/').handler, varPath)
22+
t.notOk(findMyWay.find('GET', '/a/c'))
23+
t.notOk(findMyWay.find('GET', '/a/c/'))
24+
})

0 commit comments

Comments
 (0)