2021-07-05 17:54:00 +00:00
|
|
|
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/)
|
|
|
|
})
|
2024-11-02 23:40:54 +00:00
|
|
|
|
2021-07-05 17:54:00 +00:00
|
|
|
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() {})
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.throws(function() { router.addRoute('/:asdf', function() {}) }, /existing/)
|
|
|
|
assert.throws(function() { router.addRoute('/:test/asdf/:foobar', function() {}) }, /existing/)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('fail if adding anything after a fullparam', function() {
|
|
|
|
let router = new FlaskaRouter()
|
|
|
|
assert.throws(function() { router.addRoute('/::bla/:bla', function() {}) }, /after/)
|
|
|
|
assert.throws(function() { router.addRoute('/:test/::bla/test', function() {}) }, /after/)
|
|
|
|
assert.throws(function() { router.addRoute('/::test/bla', function() {}) }, /after/)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.test('should work with param and full param side by side', function() {
|
|
|
|
let router = new FlaskaRouter()
|
|
|
|
router.addRoute('/:bla', function() {})
|
|
|
|
router.addRoute('/::bla', function() {})
|
2021-07-05 17:54:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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() {})
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.throws(function() { router.addRoute('/:test/asdf/::bla', function() {}) }, /existing/)
|
|
|
|
assert.throws(function() { router.addRoute('/:test/::bla', function() {}) }, /existing/)
|
2021-07-05 17:54:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers[0], assertHandler)
|
2021-07-05 17:54:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2024-11-02 23:40:54 +00:00
|
|
|
let result = router.match('/a')
|
|
|
|
assert.strictEqual(result.path.handlers.length, 2)
|
|
|
|
assert.strictEqual(result.path.handlers[0], assertMiddleware)
|
|
|
|
assert.strictEqual(result.path.handlers[1], assertHandler)
|
2021-07-05 17:54:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2024-11-02 23:40:54 +00:00
|
|
|
let resultA = router.match('/a')
|
|
|
|
assert.strictEqual(resultA.path.handlers.length, 2)
|
|
|
|
assert.strictEqual(resultA.path.handlers[0], assertMiddleware)
|
|
|
|
assert.strictEqual(resultA.path.handlers[1], assertHandler)
|
|
|
|
let resultB = router.match('/b')
|
|
|
|
assert.strictEqual(resultB.path.handlers.length, 3)
|
|
|
|
assert.strictEqual(resultB.path.handlers[0], assertMiddleware)
|
|
|
|
assert.strictEqual(resultB.path.handlers[1], assertMiddleware)
|
|
|
|
assert.strictEqual(resultB.path.handlers[2], assertHandler)
|
2021-07-05 17:54:00 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.describe('#match()', function() {
|
|
|
|
t.test('match basic paths', function() {
|
|
|
|
let assertMatched = false
|
|
|
|
let router = new FlaskaRouter()
|
|
|
|
router.addRoute('/test', function() { assertMatched = true })
|
2024-11-02 23:40:54 +00:00
|
|
|
let result = router.match('/test').path
|
|
|
|
assert.strictEqual(result.handlers.length, 1)
|
|
|
|
result.handlers[0]()
|
2021-07-05 17:54:00 +00:00
|
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
})
|
|
|
|
|
2024-11-02 23:40:54 +00:00
|
|
|
t.test('return middlewares in handlers', function() {
|
2021-07-05 17:54:00 +00:00
|
|
|
let assertMatched = false
|
|
|
|
let assertMiddleware = function() { assertMatched = true }
|
|
|
|
let router = new FlaskaRouter()
|
|
|
|
router.addRoute('/test', assertMiddleware, function() { })
|
2024-11-02 23:40:54 +00:00
|
|
|
let result = router.match('/test').path
|
|
|
|
assert.strictEqual(result.handlers.length, 2)
|
|
|
|
result.handlers[0]()
|
2021-07-05 17:54:00 +00:00
|
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
})
|
2024-11-02 23:40:54 +00:00
|
|
|
|
2021-07-05 17:54:00 +00:00
|
|
|
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)
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers.length, 1)
|
|
|
|
result.path.handlers[0]()
|
2021-07-05 17:54:00 +00:00
|
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
|
|
})
|
2024-11-02 23:40:54 +00:00
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
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)
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers.length, 1)
|
|
|
|
result.path.handlers[0]()
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
assert.strictEqual(result.params.id, assertParameter)
|
|
|
|
})
|
2024-11-02 23:40:54 +00:00
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
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')
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers.length, 1)
|
|
|
|
assert.strictEqual(result.path.handlers[0], assertParamFunc)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.strictEqual(result.params.bla, '123')
|
|
|
|
|
|
|
|
result = router.match('/test/123/asdf')
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers.length, 1)
|
|
|
|
assert.strictEqual(result.path.handlers[0], assertFullFunc)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.strictEqual(result.params.id, 'test/123/asdf')
|
|
|
|
assert.notOk(result.params.bla)
|
|
|
|
})
|
2021-07-05 17:54:00 +00:00
|
|
|
|
|
|
|
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')
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(result.path.handlers.length, 1)
|
|
|
|
result.path.handlers[0]()
|
2021-07-05 17:54:00 +00:00
|
|
|
assert.strictEqual(assertMatched, true)
|
|
|
|
assert.strictEqual(result.params.id, 'asdf')
|
|
|
|
})
|
|
|
|
|
2021-10-09 00:12:56 +00:00
|
|
|
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)
|
|
|
|
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/test/123').path.handlers[0], assertFunction)
|
|
|
|
assert.strictEqual(router.match('/test/asdfasdg').path.handlers[0], assertFunction)
|
|
|
|
assert.strictEqual(router.match('/test/test/sdafsda').path.handlers[0], assertFunction)
|
|
|
|
assert.strictEqual(router.match('/test/test/sdafsda/gdfshe4/43y34/wtaw').path.handlers[0], assertFunction)
|
|
|
|
assert.strictEqual(router.match('/foo/123').path.handlers[0], assertFunction)
|
|
|
|
assert.strictEqual(router.match('/foo/bar/baz/test').path.handlers[0], assertFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.ok(router.match('/test/123/yweherher/reher/h34h34/'))
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/test/123/yweherher/reher/h34h34/').path.handlers[0], assertFailFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.ok(router.match('/test/foo/bar'))
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/test/foo/bar').path.handlers[0], assertFailFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.ok(router.match('/'))
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/').path.handlers[0], assertFailFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.ok(router.match('/something/else/goes/here'))
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/something/else/goes/here').path.handlers[0], assertFailFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
|
|
|
|
router.addRoute('/', assertRootFunction)
|
2024-11-02 23:40:54 +00:00
|
|
|
router.compile()
|
2021-10-09 00:12:56 +00:00
|
|
|
assert.ok(router.match('/'))
|
2024-11-02 23:40:54 +00:00
|
|
|
assert.strictEqual(router.match('/').path.handlers[0], assertRootFunction)
|
2021-10-09 00:12:56 +00:00
|
|
|
})
|
|
|
|
|
2021-07-05 17:54:00 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|