Skip to content

Commit

Permalink
preserve param values case in case insensitive mode (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
misterdjules authored and delvedor committed Feb 17, 2019
1 parent 822b0bf commit 7a875fd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
30 changes: 19 additions & 11 deletions index.js
Expand Up @@ -340,32 +340,33 @@ Router.prototype.lookup = function lookup (req, res, ctx) {
}

Router.prototype.find = function find (method, path, version) {
if (this.caseSensitive === false) {
path = path.toLowerCase()
}

if (path.charCodeAt(0) !== 47) { // 47 is '/'
path = path.replace(FULL_PATH_REGEXP, '/')
}

var originalPath = path
var originalPathLength = path.length

if (this.caseSensitive === false) {
path = path.toLowerCase()
}

var maxParamLength = this.maxParamLength
var currentNode = this.tree
var wildcardNode = null
var pathLenWildcard = 0
var originalPath = path
var originalPathLength = path.length
var decoded = null
var pindex = 0
var params = []
var i = 0
var idxInOriginalPath = 0

while (true) {
var pathLen = path.length
var prefix = currentNode.prefix
var prefixLen = prefix.length
var len = 0
var previousPath = path

// found the route
if (pathLen === 0 || path === prefix) {
var handle = version === undefined
Expand Down Expand Up @@ -396,6 +397,7 @@ Router.prototype.find = function find (method, path, version) {
if (len === prefixLen) {
path = path.slice(len)
pathLen = path.length
idxInOriginalPath += len
}

var node = version === undefined
Expand All @@ -415,10 +417,13 @@ Router.prototype.find = function find (method, path, version) {
var pathDiff = originalPath.slice(0, originalPathLength - pathLen)
previousPath = pathDiff.slice(pathDiff.lastIndexOf('/') + 1, pathDiff.length) + path
}
idxInOriginalPath = idxInOriginalPath -
(previousPath.length - path.length)
path = previousPath
pathLen = previousPath.length
len = prefixLen
}

var kind = node.kind

// static route
Expand Down Expand Up @@ -448,16 +453,17 @@ Router.prototype.find = function find (method, path, version) {
i = path.indexOf('/')
if (i === -1) i = pathLen
if (i > maxParamLength) return null
decoded = fastDecode(path.slice(0, i))
decoded = fastDecode(originalPath.slice(idxInOriginalPath, idxInOriginalPath + i))
if (decoded === null) return null
params[pindex++] = decoded
path = path.slice(i)
idxInOriginalPath += i
continue
}

// wildcard route
if (kind === NODE_TYPES.MATCH_ALL) {
decoded = fastDecode(path)
decoded = fastDecode(originalPath.slice(idxInOriginalPath))
if (decoded === null) return null
params[pindex] = decoded
currentNode = node
Expand All @@ -471,11 +477,12 @@ Router.prototype.find = function find (method, path, version) {
i = path.indexOf('/')
if (i === -1) i = pathLen
if (i > maxParamLength) return null
decoded = fastDecode(path.slice(0, i))
decoded = fastDecode(originalPath.slice(idxInOriginalPath, idxInOriginalPath + i))
if (decoded === null) return null
if (!node.regex.test(decoded)) return null
params[pindex++] = decoded
path = path.slice(i)
idxInOriginalPath += i
continue
}

Expand All @@ -491,10 +498,11 @@ Router.prototype.find = function find (method, path, version) {
while (i < pathLen && path.charCodeAt(i) !== 47 && path.charCodeAt(i) !== 45) i++
if (i > maxParamLength) return null
}
decoded = fastDecode(path.slice(0, i))
decoded = fastDecode(originalPath.slice(idxInOriginalPath, idxInOriginalPath + i))
if (decoded === null) return null
params[pindex++] = decoded
path = path.slice(i)
idxInOriginalPath += i
continue
}

Expand Down
88 changes: 79 additions & 9 deletions test/case-insesitive.test.js → test/case-insensitive.test.js
Expand Up @@ -21,6 +21,23 @@ test('case insensitive static routes of level 1', t => {
findMyWay.lookup({ method: 'GET', url: '/WOO', headers: {} }, null)
})

test('case insensitive static routes of level 2', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/woo', (req, res, params) => {
t.pass('we should be here')
})

findMyWay.lookup({ method: 'GET', url: '/FoO/WOO', headers: {} }, null)
})

test('case insensitive static routes of level 3', t => {
t.plan(1)

Expand Down Expand Up @@ -49,7 +66,7 @@ test('parametric case insensitive', t => {
})

findMyWay.on('GET', '/foo/:param', (req, res, params) => {
t.equal(params.param, 'bar')
t.equal(params.param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
Expand All @@ -66,7 +83,7 @@ test('parametric case insensitive with a static part', t => {
})

findMyWay.on('GET', '/foo/my-:param', (req, res, params) => {
t.equal(params.param, 'bar')
t.equal(params.param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/MY-bAR', headers: {} }, null)
Expand All @@ -83,7 +100,7 @@ test('parametric case insensitive with capital letter', t => {
})

findMyWay.on('GET', '/foo/:Param', (req, res, params) => {
t.equal(params.Param, 'bar')
t.equal(params.Param, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
Expand All @@ -100,10 +117,10 @@ test('case insensitive with capital letter in static path with param', t => {
})

findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baz')
t.equal(params.param, 'baZ')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null)
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 => {
Expand All @@ -121,13 +138,66 @@ test('case insensitive with multiple paths containing capital letter in static p
})

findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baz')
t.equal(params.param, 'baZ')
})

findMyWay.on('GET', '/Foo/baz/:param', (req, res, params) => {
t.equal(params.param, '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)
})

test('case insensitive with multiple mixed-case params within same slash couple', t => {
t.plan(2)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param1-:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/FOO/My-bAR', headers: {} }, null)
})

test('case insensitive with multiple mixed-case params', t => {
t.plan(2)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/:param1/:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
})

findMyWay.lookup({ method: 'GET', url: '/FOO/My/bAR', headers: {} }, null)
})

test('case insensitive with wildcard', t => {
t.plan(1)

const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/foo/*', (req, res, params) => {
t.equal(params['*'], 'baR')
})

findMyWay.lookup({ method: 'GET', url: '/Foo/bar/baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/Foo/baz/bar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/FOO/baR', headers: {} }, null)
})

0 comments on commit 7a875fd

Please sign in to comment.