From f70751630c40fce03b4ba76cf84672f6878a43fb Mon Sep 17 00:00:00 2001 From: Julien Gilli Date: Thu, 4 Oct 2018 13:26:25 -0700 Subject: [PATCH] fix case insensitive mode for multiple routes with capital letters (#97) Fixes https://github.com/delvedor/find-my-way/issues/96. --- index.js | 6 ++++- test/case-insesitive.test.js | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ec43401..5aec3d1 100644 --- a/index.js +++ b/index.js @@ -142,7 +142,11 @@ Router.prototype._on = function _on (method, path, opts, handler, store) { // if the path is ended if (i === len) { - return this._insert(method, path.slice(0, i), nodeType, params, handler, store, regex, version) + var completedPath = path.slice(0, i) + if (this.caseSensitive === false) { + completedPath = completedPath.toLowerCase() + } + return this._insert(method, completedPath, nodeType, params, handler, store, regex, version) } // add the parameter and continue with the search this._insert(method, path.slice(0, i), nodeType, params, null, null, regex, version) diff --git a/test/case-insesitive.test.js b/test/case-insesitive.test.js index 0bc813b..d7ac147 100644 --- a/test/case-insesitive.test.js +++ b/test/case-insesitive.test.js @@ -88,3 +88,49 @@ test('parametric case insensitive with capital letter', t => { findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null) }) + +test('case insensitive with capital letter in static path with param', t => { + t.plan(1) + + const findMyWay = FindMyWay({ + caseSensitive: false, + defaultRoute: (req, res) => { + t.fail('Should not be defaultRoute') + } + }) + + findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => { + console.log('baz') + t.equal(params.param, 'baz') + }) + + findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null) +}) + +test('case insensitive with multiple paths containing capital letter in static path with param', t => { + /* + * This is a reproduction of the issue documented at + * https://github.com/delvedor/find-my-way/issues/96. + */ + t.plan(2) + + const findMyWay = FindMyWay({ + caseSensitive: false, + defaultRoute: (req, res) => { + t.fail('Should not be defaultRoute') + } + }) + + findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => { + console.log('baz') + t.equal(params.param, 'baz') + }) + + findMyWay.on('GET', '/Foo/baz/:param', (req, res, params) => { + console.log('bar') + t.equal(params.param, 'bar') + }) + + findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null) + findMyWay.lookup({ method: 'GET', url: '/Foo/baz/bar', headers: {} }, null) +})