307 lines
13 KiB
JavaScript
307 lines
13 KiB
JavaScript
import { Eltro as t, assert } from 'eltro'
|
|
import { FlaskaRouter } from '../flaska.mjs'
|
|
|
|
t.describe('#addRoute()', function() {
|
|
t.test('fail if trying to add non-root path', function() {
|
|
let router = new FlaskaRouter()
|
|
assert.throws(function() { router.addRoute('', function() { }) }, /forward slash/)
|
|
assert.throws(function() { router.addRoute('test') }, /forward slash/)
|
|
assert.throws(function() { router.addRoute(':test') }, /forward slash/)
|
|
assert.throws(function() { router.addRoute('test/test2') }, /forward slash/)
|
|
})
|
|
|
|
t.test('fail if missing handler', function() {
|
|
let router = new FlaskaRouter()
|
|
assert.throws(function() { router.addRoute('/') }, /handler/)
|
|
assert.throws(function() { router.addRoute('/test') }, /handler/)
|
|
assert.throws(function() { router.addRoute('/:test') }, /handler/)
|
|
})
|
|
|
|
t.test('fail if missing handler but has middleware', function() {
|
|
let router = new FlaskaRouter()
|
|
assert.throws(function() { router.addRoute('/', [function() {}]) }, /handler/)
|
|
assert.throws(function() { router.addRoute('/test', [function() {}]) }, /handler/)
|
|
assert.throws(function() { router.addRoute('/:test', [function() {}]) }, /handler/)
|
|
})
|
|
|
|
t.test('fail if adding non-name route', function() {
|
|
let router = new FlaskaRouter()
|
|
assert.throws(function() { router.addRoute('//', function() {}) }, /path/)
|
|
assert.throws(function() { router.addRoute('/test//test2', function() {}) }, /path/)
|
|
assert.throws(function() { router.addRoute('/test/:test//bla', function() {}) }, /path/)
|
|
})
|
|
|
|
t.test('fail if adding non-name param route', function() {
|
|
let router = new FlaskaRouter()
|
|
assert.throws(function() { router.addRoute('/:/', function() {}) }, /path/)
|
|
assert.throws(function() { router.addRoute('/test/:/test2', function() {}) }, /path/)
|
|
assert.throws(function() { router.addRoute('/test/:test/:/bla', function() {}) }, /path/)
|
|
})
|
|
|
|
t.test('fail if adding different named param to existing route', function() {
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/:test', function() {})
|
|
router.addRoute('/:test/asdf/:bla', function() {})
|
|
router.addRoute('/:test/bla', function() {})
|
|
router.addRoute('/bla/bla', function() {})
|
|
router.addRoute('/bla/bla/bla', function() {})
|
|
assert.throws(function() { router.addRoute('/:asdf/', function() {}) }, /param/)
|
|
assert.throws(function() { router.addRoute('/:test/asdf/:foobar', function() {}) }, /param/)
|
|
})
|
|
|
|
t.test('fail if adding multiple fullparam', function() {
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/:test', function() {})
|
|
router.addRoute('/:test/asdf/::bla', function() {})
|
|
router.addRoute('/:test/bla', function() {})
|
|
router.addRoute('/:test/::bla', function() {})
|
|
router.addRoute('/bla/bla/bla', function() {})
|
|
assert.throws(function() { router.addRoute('/:test/asdf/::bla/fail', function() {}) }, /full.+param/)
|
|
assert.throws(function() { router.addRoute('/:test/::bla/test', function() {}) }, /full.+param/)
|
|
assert.throws(function() { router.addRoute('/:test/:bla', function() {}) }, /full.+param/)
|
|
assert.throws(function() { router.addRoute('/::test', function() {}) }, /partial.+param/)
|
|
})
|
|
|
|
t.test('add route correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a/b/c', assertHandler)
|
|
let result = router.match('/a/b/c')
|
|
assert.strictEqual(result.handler, assertHandler)
|
|
})
|
|
|
|
t.test('add param route correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a/:b/c', assertHandler)
|
|
assert.ok(router.root.children.get('a'))
|
|
assert.ok(router.root.children.get('a').children.get('__param'))
|
|
assert.strictEqual(router.root.children.get('a').children.get('__param').paramName, 'b')
|
|
assert.ok(router.root.children.get('a').children.get('__param').children.get('c'))
|
|
assert.strictEqual(router.root.children.get('a').children.get('__param').children.get('c').handler, assertHandler)
|
|
})
|
|
|
|
t.test('add full param route correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a/::b', assertHandler)
|
|
assert.ok(router.root.children.get('a'))
|
|
assert.ok(router.root.children.get('a').children.get('__fullparam'))
|
|
assert.strictEqual(router.root.children.get('a').children.get('__fullparam').fullparamName, 'b')
|
|
assert.strictEqual(router.root.children.get('a').children.get('__fullparam').handler, assertHandler)
|
|
})
|
|
|
|
t.test('add param route correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a/:b/c', assertHandler)
|
|
assert.ok(router.root.children.get('a'))
|
|
assert.ok(router.root.children.get('a').children.get('__param'))
|
|
assert.strictEqual(router.root.children.get('a').children.get('__param').paramName, 'b')
|
|
assert.ok(router.root.children.get('a').children.get('__param').children.get('c'))
|
|
assert.strictEqual(router.root.children.get('a').children.get('__param').children.get('c').handler, assertHandler)
|
|
})
|
|
|
|
t.test('support single middlewares correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let assertMiddleware = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a', assertMiddleware, assertHandler)
|
|
assert.ok(router.root.children.get('a'))
|
|
assert.strictEqual(router.root.children.get('a').handler, assertHandler)
|
|
assert.strictEqual(router.root.children.get('a').middlewares.length, 1)
|
|
assert.strictEqual(router.root.children.get('a').middlewares[0], assertMiddleware)
|
|
})
|
|
|
|
t.test('support multi middlewares correctly', function() {
|
|
let assertHandler = function() { return 1 }
|
|
let assertMiddleware = function() { return 1 }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/a', [assertMiddleware], assertHandler)
|
|
router.addRoute('/b', [assertMiddleware, assertMiddleware], assertHandler)
|
|
assert.ok(router.root.children.get('a'))
|
|
assert.strictEqual(router.root.children.get('a').handler, assertHandler)
|
|
assert.strictEqual(router.root.children.get('a').middlewares.length, 1)
|
|
assert.strictEqual(router.root.children.get('a').middlewares[0], assertMiddleware)
|
|
assert.ok(router.root.children.get('b'))
|
|
assert.strictEqual(router.root.children.get('b').handler, assertHandler)
|
|
assert.strictEqual(router.root.children.get('b').middlewares.length, 2)
|
|
assert.strictEqual(router.root.children.get('b').middlewares[0], assertMiddleware)
|
|
assert.strictEqual(router.root.children.get('b').middlewares[1], assertMiddleware)
|
|
})
|
|
})
|
|
|
|
t.describe('#match()', function() {
|
|
t.test('match basic paths', function() {
|
|
let assertMatched = false
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test', function() { assertMatched = true })
|
|
let result = router.match('/test')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
// Test with extra slash at the end
|
|
assertMatched = false
|
|
result = router.match('/test/')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
})
|
|
|
|
|
|
t.test('return middlewares', function() {
|
|
let assertMatched = false
|
|
let assertMiddleware = function() { assertMatched = true }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test', assertMiddleware, function() { })
|
|
let result = router.match('/test')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 1)
|
|
result.middlewares[0]()
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
// Test with extra slash at the end
|
|
assertMatched = false
|
|
result = router.match('/test/')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 1)
|
|
result.middlewares[0]()
|
|
assert.strictEqual(assertMatched, true)
|
|
})
|
|
|
|
t.test('match variable paths', function() {
|
|
const assertParameter = 'bla'
|
|
let assertMatched = false
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/:id', function() { assertMatched = true })
|
|
let result = router.match('/test/' + assertParameter)
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
|
|
// Test with extra slash at the end
|
|
assertMatched = false
|
|
result = router.match('/test/' + assertParameter + '/')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
})
|
|
|
|
t.test('match full path variable paths', function() {
|
|
const assertParameter = 'bla/bla/bla'
|
|
let assertMatched = false
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/::id', function() { assertMatched = true })
|
|
let result = router.match('/test/' + assertParameter)
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
|
|
// Test with extra slash at the end
|
|
assertMatched = false
|
|
result = router.match('/test/' + assertParameter + '/')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
})
|
|
|
|
t.test('match full path root path properly', function() {
|
|
const assertParamFunc = function() { }
|
|
const assertFullFunc = function() { }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/:bla', assertParamFunc)
|
|
router.addRoute('/::id', assertFullFunc)
|
|
let result = router.match('/test/123')
|
|
assert.strictEqual(result.handler, assertParamFunc)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
assert.strictEqual(result.params.bla, '123')
|
|
|
|
result = router.match('/test/123/asdf')
|
|
assert.strictEqual(result.handler, assertFullFunc)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
assert.strictEqual(result.params.id, 'test/123/asdf')
|
|
assert.notOk(result.params.bla)
|
|
})
|
|
|
|
t.test('match paths properly', function() {
|
|
let assertMatched = true
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/:id', function() { assertMatched = false })
|
|
router.addRoute('/test/:id/test1', function() { })
|
|
let result = router.match('/test/asdf/test1')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, 'asdf')
|
|
|
|
// Test with extra slash at the end
|
|
result = router.match('/test/asdf/test1/')
|
|
assert.ok(result.handler)
|
|
assert.ok(result.middlewares)
|
|
assert.strictEqual(result.middlewares.length, 0)
|
|
result.handler()
|
|
assert.strictEqual(assertMatched, true)
|
|
assert.strictEqual(result.params.id, 'asdf')
|
|
})
|
|
|
|
t.test('more comprehensive testing', function() {
|
|
const assertFunction = function() { }
|
|
const assertRootFunction = function() { }
|
|
const assertFailFunction = function() { }
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/:id', assertFunction)
|
|
router.addRoute('/test/test/::path', assertFunction)
|
|
router.addRoute('/foo/::path', assertFunction)
|
|
router.addRoute('/::path', assertFailFunction)
|
|
|
|
assert.strictEqual(router.match('/test/123').handler, assertFunction)
|
|
assert.strictEqual(router.match('/test/asdfasdg').handler, assertFunction)
|
|
assert.strictEqual(router.match('/test/test/sdafsda').handler, assertFunction)
|
|
assert.strictEqual(router.match('/test/test/sdafsda/gdfshe4/43y34/wtaw').handler, assertFunction)
|
|
assert.strictEqual(router.match('/foo/123').handler, assertFunction)
|
|
assert.strictEqual(router.match('/foo/bar/baz/test').handler, assertFunction)
|
|
assert.ok(router.match('/test/123/yweherher/reher/h34h34/'))
|
|
assert.strictEqual(router.match('/test/123/yweherher/reher/h34h34/').handler, assertFailFunction)
|
|
assert.ok(router.match('/test/foo/bar'))
|
|
assert.strictEqual(router.match('/test/foo/bar').handler, assertFailFunction)
|
|
assert.ok(router.match('/'))
|
|
assert.strictEqual(router.match('/').handler, assertFailFunction)
|
|
assert.ok(router.match('/something/else/goes/here'))
|
|
assert.strictEqual(router.match('/something/else/goes/here').handler, assertFailFunction)
|
|
|
|
router.addRoute('/', assertRootFunction)
|
|
assert.ok(router.match('/'))
|
|
assert.strictEqual(router.match('/').handler, assertRootFunction)
|
|
})
|
|
|
|
t.test('return null when no match is found', function() {
|
|
let router = new FlaskaRouter()
|
|
router.addRoute('/test/:id', function() { })
|
|
router.addRoute('/test/:id/test1', function() { })
|
|
let result = router.match('/test/asdf/test2')
|
|
assert.notOk(result)
|
|
})
|
|
})
|