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() {}) }, /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() {}) }) 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', function() {}) }, /existing/) assert.throws(function() { router.addRoute('/:test/::bla', function() {}) }, /existing/) }) 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.path.handlers[0], 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) 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) }) 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) 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) }) }) 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').path assert.strictEqual(result.handlers.length, 1) result.handlers[0]() assert.strictEqual(assertMatched, true) }) t.test('return middlewares in handlers', function() { let assertMatched = false let assertMiddleware = function() { assertMatched = true } let router = new FlaskaRouter() router.addRoute('/test', assertMiddleware, function() { }) let result = router.match('/test').path assert.strictEqual(result.handlers.length, 2) result.handlers[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.strictEqual(result.path.handlers.length, 1) result.path.handlers[0]() 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.strictEqual(result.path.handlers.length, 1) result.path.handlers[0]() 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.path.handlers.length, 1) assert.strictEqual(result.path.handlers[0], assertParamFunc) assert.strictEqual(result.params.bla, '123') result = router.match('/test/123/asdf') assert.strictEqual(result.path.handlers.length, 1) assert.strictEqual(result.path.handlers[0], assertFullFunc) 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.strictEqual(result.path.handlers.length, 1) result.path.handlers[0]() 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').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) assert.ok(router.match('/test/123/yweherher/reher/h34h34/')) assert.strictEqual(router.match('/test/123/yweherher/reher/h34h34/').path.handlers[0], assertFailFunction) assert.ok(router.match('/test/foo/bar')) assert.strictEqual(router.match('/test/foo/bar').path.handlers[0], assertFailFunction) assert.ok(router.match('/')) assert.strictEqual(router.match('/').path.handlers[0], assertFailFunction) assert.ok(router.match('/something/else/goes/here')) assert.strictEqual(router.match('/something/else/goes/here').path.handlers[0], assertFailFunction) router.addRoute('/', assertRootFunction) router.compile() assert.ok(router.match('/')) assert.strictEqual(router.match('/').path.handlers[0], 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) }) })