find-my-way
Advanced tools
Comparing version 1.6.1 to 1.6.2
21
index.js
@@ -19,3 +19,2 @@ 'use strict' | ||
'?': 63 | ||
*/ | ||
@@ -35,3 +34,3 @@ | ||
if (opts.defaultRoute) { | ||
assert.equal(typeof opts.defaultRoute, 'function', 'The default route must be a function') | ||
assert(typeof opts.defaultRoute === 'function', 'The default route must be a function') | ||
this.defaultRoute = opts.defaultRoute | ||
@@ -51,6 +50,11 @@ } | ||
assert.equal(typeof method, 'string', 'Method should be a string') | ||
assert.equal(typeof path, 'string', 'Path should be a string') | ||
assert.equal(typeof handler, 'function', 'Handler should be a function') | ||
assert.notEqual(httpMethods.indexOf(method), -1, `Method '${method}' is not an http method.`) | ||
// method validation | ||
assert(typeof method === 'string', 'Method should be a string') | ||
assert(httpMethods.indexOf(method) !== -1, `Method '${method}' is not an http method.`) | ||
// path validation | ||
assert(typeof path === 'string', 'Path should be a string') | ||
assert(path.length > 0, 'The path could not be empty') | ||
assert(path[0] === '/' || path[0] === '*', 'The first character of a path should be `/` or `*`') | ||
// handler validation | ||
assert(typeof handler === 'function', 'Handler should be a function') | ||
@@ -84,3 +88,3 @@ const params = [] | ||
nodeType = 3 | ||
} else if (i < len) { | ||
} else if (i < len && path.charCodeAt(i) !== 47) { | ||
nodeType = 4 | ||
@@ -307,5 +311,2 @@ } | ||
} | ||
// route not found | ||
if (len !== prefixLen) return null | ||
} | ||
@@ -312,0 +313,0 @@ } |
{ | ||
"name": "find-my-way", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "Crazy fast http radix based router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -43,2 +43,26 @@ 'use strict' | ||
test('The path could not be empty', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay() | ||
try { | ||
findMyWay.on('GET', '', () => {}) | ||
t.fail('The path could not be empty') | ||
} catch (e) { | ||
t.is(e.message, 'The path could not be empty') | ||
} | ||
}) | ||
test('The first character of a path should be `/` or `*`', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay() | ||
try { | ||
findMyWay.on('GET', 'a', () => {}) | ||
t.fail('The first character of a path should be `/` or `*`') | ||
} catch (e) { | ||
t.is(e.message, 'The first character of a path should be `/` or `*`') | ||
} | ||
}) | ||
test('Handler should be a function', t => { | ||
@@ -45,0 +69,0 @@ t.plan(1) |
@@ -7,2 +7,17 @@ 'use strict' | ||
test('Parametric route, request.url contains dash', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: (req, res) => { | ||
t.fail('Should not be defaultRoute') | ||
} | ||
}) | ||
findMyWay.on('GET', '/a/:param/b', (req, res, params) => { | ||
t.equal(params.param, 'foo-bar') | ||
}) | ||
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/b' }, null) | ||
}) | ||
test('Parametric route with fixed suffix', t => { | ||
@@ -150,2 +165,24 @@ t.plan(1) | ||
test('Multi parametric route with regexp / 2', t => { | ||
t.plan(4) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: (req, res) => { | ||
t.fail('Should not be defaultRoute') | ||
} | ||
}) | ||
findMyWay.on('GET', '/a/:uuid(^[\\d-]{19})-:user(^\\w+)', (req, res, params) => { | ||
t.equal(params.uuid, '1111-2222-3333-4444') | ||
t.equal(params.user, 'foo') | ||
}) | ||
findMyWay.on('GET', '/a/:uuid(^[\\d-]{19})-:user(^\\w+)/account', (req, res, params) => { | ||
t.equal(params.uuid, '1111-2222-3333-4445') | ||
t.equal(params.user, 'bar') | ||
}) | ||
findMyWay.lookup({ method: 'GET', url: '/a/1111-2222-3333-4444-foo' }, null) | ||
findMyWay.lookup({ method: 'GET', url: '/a/1111-2222-3333-4445-bar/account' }, null) | ||
}) | ||
test('Multi parametric route with fixed suffix', t => { | ||
@@ -152,0 +189,0 @@ t.plan(2) |
@@ -128,1 +128,19 @@ 'use strict' | ||
}) | ||
test('safe decodeURIComponent', t => { | ||
t.plan(1) | ||
const findMyWay = FindMyWay({ | ||
defaultRoute: () => { | ||
t.ok('route not matched') | ||
} | ||
}) | ||
findMyWay.on('GET', '/test/:id(^\\d+$)', () => { | ||
t.fail('we should not be here') | ||
}) | ||
t.deepEqual( | ||
findMyWay.find('GET', '/test/hel%"Flo'), | ||
null | ||
) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60743
1764