Skip to content

Commit

Permalink
Merge pull request #30 from delvedor/url-validation
Browse files Browse the repository at this point in the history
 Added url validations
  • Loading branch information
delvedor authored Oct 1, 2017
2 parents cee1a6b + e765c5b commit 5288ecc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'/': 47
':': 58
'?': 63
*/

const assert = require('assert')
Expand All @@ -31,7 +30,7 @@ function Router (opts) {
opts = opts || {}

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
}

Expand All @@ -46,10 +45,15 @@ Router.prototype.on = function (method, path, handler, store) {
return
}

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')

const params = []
var j = 0
Expand Down Expand Up @@ -300,9 +304,6 @@ Router.prototype.find = function (method, path) {
path = path.slice(i)
continue
}

// route not found
if (len !== prefixLen) return null
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ test('Path should be a string', t => {
}
})

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 => {
t.plan(1)
const findMyWay = FindMyWay()
Expand Down
18 changes: 18 additions & 0 deletions test/regex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,21 @@ test('route with an extension regex - no match', t => {

findMyWay.lookup({ method: 'GET', url: '/test/aa.png' }, null)
})

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
)
})

0 comments on commit 5288ecc

Please sign in to comment.