Skip to content

Commit

Permalink
Merge 1e501eb into 57b6577
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jul 2, 2018
2 parents 57b6577 + 1e501eb commit 14d0cc3
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -54,7 +54,7 @@ const router = require('find-my-way')({
ignoreTrailingSlash: true
})
function handler (req, res, params) {
res.send('foo')
res.end('foo')
}
// maps "/foo/" and "/foo" to `handler`
router.on('GET', '/foo/', handler)
Expand All @@ -75,6 +75,19 @@ const router = require('find-my-way')({
allowUnsafeRegex: true
})
```

According to [RFC3986](https://tools.ietf.org/html/rfc3986#section-6.2.2.1), find-my-way is case sensitive by default.
You can disable this by setting the `caseSensitive` option to `false`:
in that case all paths will be lowercased before routing, including
parametric and regexp-matched values. You can turn off case sensitivity
with:

```js
const router = require('find-my-way')({
caseSensitive: false
})
```

<a name="on"></a>
#### on(method, path, [opts], handler, [store])
Register a new route.
Expand Down
17 changes: 16 additions & 1 deletion index.js
Expand Up @@ -32,6 +32,7 @@ function Router (opts) {
this.defaultRoute = null
}

this.caseSensitive = opts.caseSensitive === undefined ? true : opts.caseSensitive
this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
this.maxParamLength = opts.maxParamLength || 100
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
Expand Down Expand Up @@ -96,8 +97,14 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
if (path.charCodeAt(i) === 58) {
var nodeType = NODE_TYPES.PARAM
j = i + 1
var staticPart = path.slice(0, i)

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

// add the static part of the route to the tree
this._insert(method, path.slice(0, i), 0, null, null, null, null, version)
this._insert(method, staticPart, 0, null, null, null, null, version)

// isolate the parameter name
var isRegex = false
Expand Down Expand Up @@ -149,6 +156,11 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
return this._insert(method, path.slice(0, len), 2, params, handler, store, null, version)
}
}

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

// static route
this._insert(method, path, 0, params, handler, store, null, version)
}
Expand Down Expand Up @@ -308,6 +320,9 @@ Router.prototype.lookup = function lookup (req, res) {
}

Router.prototype.find = function find (method, path, version) {
if (this.caseSensitive === false) {
path = path.toLowerCase()
}
var maxParamLength = this.maxParamLength
var currentNode = this.tree
var wildcardNode = null
Expand Down
90 changes: 90 additions & 0 deletions test/case-insesitive.test.js
@@ -0,0 +1,90 @@
'use strict'

const t = require('tap')
const test = t.test
const FindMyWay = require('../')

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

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

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

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

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

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

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

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

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

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

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

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

test('parametric case insensitive with a static part', t => {
t.plan(1)

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

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

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

test('parametric case insensitive with capital letter', t => {
t.plan(1)

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

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

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

0 comments on commit 14d0cc3

Please sign in to comment.