234 lines
10 KiB
JavaScript
234 lines
10 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 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('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)
|
||
|
})
|
||
|
})
|