From 275f03f109236ecce40736dc6f2478e0d7fe0aa8 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sat, 7 Mar 2020 18:03:44 +0100 Subject: [PATCH] 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 --- index.js | 3 ++- test/issue-145.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/issue-145.test.js diff --git a/index.js b/index.js index 484cf7f..e4387cc 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/issue-145.test.js b/test/issue-145.test.js new file mode 100644 index 0000000..8641fee --- /dev/null +++ b/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/')) +})