Skip to content

Commit f707516

Browse files
misterdjulesdelvedor
authored andcommitted
fix case insensitive mode for multiple routes with capital letters (#97)
Fixes #96.
1 parent 14ec21a commit f707516

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
142142

143143
// if the path is ended
144144
if (i === len) {
145-
return this._insert(method, path.slice(0, i), nodeType, params, handler, store, regex, version)
145+
var completedPath = path.slice(0, i)
146+
if (this.caseSensitive === false) {
147+
completedPath = completedPath.toLowerCase()
148+
}
149+
return this._insert(method, completedPath, nodeType, params, handler, store, regex, version)
146150
}
147151
// add the parameter and continue with the search
148152
this._insert(method, path.slice(0, i), nodeType, params, null, null, regex, version)

test/case-insesitive.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,49 @@ test('parametric case insensitive with capital letter', t => {
8888

8989
findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
9090
})
91+
92+
test('case insensitive with capital letter in static path with param', t => {
93+
t.plan(1)
94+
95+
const findMyWay = FindMyWay({
96+
caseSensitive: false,
97+
defaultRoute: (req, res) => {
98+
t.fail('Should not be defaultRoute')
99+
}
100+
})
101+
102+
findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
103+
console.log('baz')
104+
t.equal(params.param, 'baz')
105+
})
106+
107+
findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null)
108+
})
109+
110+
test('case insensitive with multiple paths containing capital letter in static path with param', t => {
111+
/*
112+
* This is a reproduction of the issue documented at
113+
* https://github.com/delvedor/find-my-way/issues/96.
114+
*/
115+
t.plan(2)
116+
117+
const findMyWay = FindMyWay({
118+
caseSensitive: false,
119+
defaultRoute: (req, res) => {
120+
t.fail('Should not be defaultRoute')
121+
}
122+
})
123+
124+
findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
125+
console.log('baz')
126+
t.equal(params.param, 'baz')
127+
})
128+
129+
findMyWay.on('GET', '/Foo/baz/:param', (req, res, params) => {
130+
console.log('bar')
131+
t.equal(params.param, 'bar')
132+
})
133+
134+
findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null)
135+
findMyWay.lookup({ method: 'GET', url: '/Foo/baz/bar', headers: {} }, null)
136+
})

0 commit comments

Comments
 (0)