Skip to content

Commit

Permalink
fix case insensitive mode for multiple routes with capital letters (#97)
Browse files Browse the repository at this point in the history
Fixes #96.
  • Loading branch information
misterdjules authored and delvedor committed Oct 4, 2018
1 parent 14ec21a commit f707516
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions test/case-insesitive.test.js
Expand Up @@ -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)
})

0 comments on commit f707516

Please sign in to comment.