Skip to content

Commit

Permalink
No match path param with ignoreTrailingSlash (#146)
Browse files Browse the repository at this point in the history
* add failing test

* fix more strict test

* fix param path with trailing slash

* better check

* more test check
  • Loading branch information
Eomm committed Mar 7, 2020
1 parent 6a3bfaf commit 275f03f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -421,7 +421,8 @@ Router.prototype.find = function find (method, path, version) {
return this._getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard)
}

if (originalPath.indexOf('/' + previousPath) === -1) {
var goBack = this.ignoreTrailingSlash ? previousPath : '/' + previousPath
if (originalPath.indexOf(goBack) === -1) {
// we need to know the outstanding path so far from the originalPath since the last encountered "/" and assign it to previousPath.
// e.g originalPath: /aa/bbb/cc, path: bb/cc
// outstanding path: /bbb/cc
Expand Down
24 changes: 24 additions & 0 deletions test/issue-145.test.js
@@ -0,0 +1,24 @@
'use strict'

const t = require('tap')
const FindMyWay = require('../')

t.test('issue-145', (t) => {
t.plan(8)

const findMyWay = FindMyWay({ ignoreTrailingSlash: true })

const fixedPath = function staticPath () {}
const varPath = function parameterPath () {}
findMyWay.on('GET', '/a/b', fixedPath)
findMyWay.on('GET', '/a/:pam/c', varPath)

t.equals(findMyWay.find('GET', '/a/b').handler, fixedPath)
t.equals(findMyWay.find('GET', '/a/b/').handler, fixedPath)
t.equals(findMyWay.find('GET', '/a/b/c').handler, varPath)
t.equals(findMyWay.find('GET', '/a/b/c/').handler, varPath)
t.equals(findMyWay.find('GET', '/a/foo/c').handler, varPath)
t.equals(findMyWay.find('GET', '/a/foo/c/').handler, varPath)
t.notOk(findMyWay.find('GET', '/a/c'))
t.notOk(findMyWay.find('GET', '/a/c/'))
})

0 comments on commit 275f03f

Please sign in to comment.