Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path params match issue #159

Merged
merged 1 commit into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ Router.prototype.find = function find (method, path, version) {
return this._getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard)
}

var goBack = this.ignoreTrailingSlash ? previousPath : '/' + previousPath
var goBack = previousPath.charCodeAt(0) === 47 ? 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
Expand Down
37 changes: 37 additions & 0 deletions test/path-params-match.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

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

t.test('path params match', (t) => {
t.plan(12)

const findMyWay = FindMyWay({ ignoreTrailingSlash: true })

const b1Path = function b1StaticPath () {}
const b2Path = function b2StaticPath () {}
const cPath = function cStaticPath () {}
const paramPath = function parameterPath () {}

findMyWay.on('GET', '/ab1', b1Path)
findMyWay.on('GET', '/ab2', b2Path)
findMyWay.on('GET', '/ac', cPath)
findMyWay.on('GET', '/:pam', paramPath)

t.equals(findMyWay.find('GET', '/ab1').handler, b1Path)
t.equals(findMyWay.find('GET', '/ab1/').handler, b1Path)
t.equals(findMyWay.find('GET', '/ab2').handler, b2Path)
t.equals(findMyWay.find('GET', '/ab2/').handler, b2Path)
t.equals(findMyWay.find('GET', '/ac').handler, cPath)
t.equals(findMyWay.find('GET', '/ac/').handler, cPath)
t.equals(findMyWay.find('GET', '/foo').handler, paramPath)
t.equals(findMyWay.find('GET', '/foo/').handler, paramPath)

const noTrailingSlashRet = findMyWay.find('GET', '/abcdef')
t.equals(noTrailingSlashRet.handler, paramPath)
t.deepEqual(noTrailingSlashRet.params, { pam: 'abcdef' })

const trailingSlashRet = findMyWay.find('GET', '/abcdef/')
t.equals(trailingSlashRet.handler, paramPath)
t.deepEqual(trailingSlashRet.params, { pam: 'abcdef' })
})