X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Frouter.js;h=d63917087d1490021d5441564669008c60f1676c;hb=32ece4871dbf71d1f43d541ab602b0c8e554c889;hp=f4c54a997fb26a0adcf944f208dfa1e81cb73fbf;hpb=29837f0eeb9fcb4c53426e5bd89e9bdf7e9d961b;p=squeep-api-dingus diff --git a/test/lib/router.js b/test/lib/router.js index f4c54a9..d639170 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -5,6 +5,7 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const Router = require('../../lib/router'); +const PathParameter = require('../../lib/router/path-parameter') const { DingusError } = require('../../lib/errors'); const noExpectedException = 'did not get expected exception'; @@ -22,61 +23,61 @@ describe('Router', function () { router.ignoreTrailingSlash = _its; }); - describe('_pathDefinitionToPathMatch', function () { + describe('_pathToRoutePath', function () { it('defines a simple path', function () { const p = '/a/b/c'; const expected = ['', 'a', 'b', 'c']; - expected[router.METHODS] = {}; - const r = router._pathDefinitionToPathMatch(p); + expected[Router.kPathMethods] = {}; + const r = router._pathToRoutePath(p); assert.deepStrictEqual(r, expected); }); it('defines a path with parameter', function () { const p = '/a/b/:c'; - const expected = ['', 'a', 'b', { [router.PARAM]: 'c' }]; - expected[router.METHODS] = {}; - const r = router._pathDefinitionToPathMatch(p); + const expected = ['', 'a', 'b', new PathParameter('c')]; + expected[Router.kPathMethods] = {}; + const r = router._pathToRoutePath(p); assert.deepStrictEqual(r, expected); }); it('defines a path with trailing slash', function () { router.ignoreTrailingSlash = true; const p = '/a/b/:c/'; - const expected = ['', 'a', 'b', { [router.PARAM]: 'c' }]; - expected[router.METHODS] = {}; - const r = router._pathDefinitionToPathMatch(p); + const expected = ['', 'a', 'b', new PathParameter('c')]; + expected[Router.kPathMethods] = {}; + const r = router._pathToRoutePath(p); assert.deepStrictEqual(r, expected); }); - }); // _pathDefinitionToPathMatch + }); // _pathToRoutePath describe('_pathCompareExact', function () { let fixedPath, checkPath; it('compares static paths which match', function () { - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b/c'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b/c'); const r = Router._pathCompareExact(fixedPath, checkPath); assert.strictEqual(r, true); }); it('compares static paths which do not match', function () { - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b/d'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b/d'); const r = Router._pathCompareExact(fixedPath, checkPath); assert.strictEqual(r, false); }); it('compares unequal static paths', function () { - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b'); const r = Router._pathCompareExact(fixedPath, checkPath); assert.strictEqual(r, false); }); it('compares param paths which match', function () { - fixedPath = router._pathDefinitionToPathMatch('/a/:b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/:b/c'); + fixedPath = router._pathToRoutePath('/a/:b/c'); + checkPath = router._pathToRoutePath('/a/:b/c'); const r = Router._pathCompareExact(fixedPath, checkPath); assert.strictEqual(r, true); }); it('compares param paths which do not match', function () { - fixedPath = router._pathDefinitionToPathMatch('/a/:b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/:g/c'); + fixedPath = router._pathToRoutePath('/a/:b/c'); + checkPath = router._pathToRoutePath('/a/:g/c'); const r = Router._pathCompareExact(fixedPath, checkPath); assert.strictEqual(r, false); }); @@ -88,8 +89,8 @@ describe('Router', function () { it('compares static paths which match', function () { const params = {}; const expectedParams = {}; - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b/c'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b/c'); const r = Router._pathCompareParam(fixedPath, checkPath); assert.strictEqual(r, true); assert.deepStrictEqual(params, expectedParams); @@ -97,8 +98,8 @@ describe('Router', function () { it('compares static paths which do not match', function () { const params = {}; const expectedParams = {}; - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b/d'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b/d'); const r = Router._pathCompareParam(fixedPath, checkPath, params); assert.strictEqual(r, false); assert.deepStrictEqual(params, expectedParams); @@ -106,8 +107,8 @@ describe('Router', function () { it('compares unequal static paths', function () { const params = {}; const expectedParams = {}; - fixedPath = router._pathDefinitionToPathMatch('/a/b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/b'); + fixedPath = router._pathToRoutePath('/a/b/c'); + checkPath = router._pathToRoutePath('/a/b'); const r = Router._pathCompareParam(fixedPath, checkPath, params); assert.strictEqual(r, false); assert.deepStrictEqual(params, expectedParams); @@ -117,8 +118,8 @@ describe('Router', function () { const expectedParams = { b: 'bar', }; - fixedPath = router._pathDefinitionToPathMatch('/a/:b/c'); - checkPath = router._pathDefinitionToPathMatch('/a/bar/c'); + fixedPath = router._pathToRoutePath('/a/:b/c'); + checkPath = router._pathToRoutePath('/a/bar/c'); const r = Router._pathCompareParam(fixedPath, checkPath, params); assert.strictEqual(r, true); assert.deepStrictEqual(params, expectedParams); @@ -129,8 +130,8 @@ describe('Router', function () { b: 'gaz', c: '123', }; - fixedPath = router._pathDefinitionToPathMatch('/a/:b/:c'); - checkPath = router._pathDefinitionToPathMatch('/a/gaz/123'); + fixedPath = router._pathToRoutePath('/a/:b/:c'); + checkPath = router._pathToRoutePath('/a/gaz/123'); const r = Router._pathCompareParam(fixedPath, checkPath, params); assert.strictEqual(r, true); assert.deepStrictEqual(params, expectedParams); @@ -143,8 +144,8 @@ describe('Router', function () { beforeEach(function () { pathsByLengthOrig = router.pathsByLength; router.pathsByLength = { - 2: [ router._pathDefinitionToPathMatch('/:id') ], - 3: [ router._pathDefinitionToPathMatch('/a/b') ], + 2: [ router._pathToRoutePath('/:id') ], + 3: [ router._pathToRoutePath('/a/b') ], }; }); afterEach(function () { @@ -167,7 +168,7 @@ describe('Router', function () { describe('_pathFindExact', function () { it('finds a path', function () { - const pathParts = ['', { [router.PARAM]: 'id' }]; + const pathParts = ['', new PathParameter('id')]; const r = router._pathFindExact(pathParts); assert.strictEqual(r, router.pathsByLength[2][0]); }); @@ -183,11 +184,15 @@ describe('Router', function () { describe('on', function () { let pathsByLengthOrig; const stubHandler = () => {}; + const stubEntry = { + handler: stubHandler, + handlerArgs: [], + }; beforeEach(function () { pathsByLengthOrig = router.pathsByLength; router.pathsByLength = { - 2: [ router._pathDefinitionToPathMatch('/:id') ], + 2: [ router._pathToRoutePath('/:id') ], }; }); afterEach(function () { @@ -196,17 +201,17 @@ describe('Router', function () { it('adds new path', function () { const urlPath = '/a/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubHandler; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['GET'] = stubEntry; router.on('GET', urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[3][0], expected); }); it('adds new method to path', function () { const urlPath = '/a/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubHandler; - expected[router.METHODS]['POST'] = stubHandler; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['GET'] = stubEntry; + expected[Router.kPathMethods]['POST'] = stubEntry; router.on('GET', urlPath, stubHandler); router.on('POST', urlPath, stubHandler); @@ -215,8 +220,8 @@ describe('Router', function () { it('add some more paths', function () { let urlPath = '/a/b/c/d'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubHandler; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['GET'] = stubEntry; router.on('GET', urlPath, stubHandler); urlPath = '/a/b/x/y'; router.on('GET', urlPath, stubHandler); @@ -226,9 +231,9 @@ describe('Router', function () { it('adds multiple methods', function () { const urlPath = '/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubHandler; - expected[router.METHODS]['HEAD'] = stubHandler; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['GET'] = stubEntry; + expected[Router.kPathMethods]['HEAD'] = stubEntry; router.on(['GET', 'HEAD'], urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[2][0], expected); @@ -236,8 +241,8 @@ describe('Router', function () { it('adds new wildcard path', function () { const urlPath = '/a/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['*'] = stubHandler; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['*'] = stubEntry; router.on('*', urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[3][0], expected); }); @@ -252,6 +257,16 @@ describe('Router', function () { assert.strictEqual(e.message, 'invalid method \'FLARP\''); } }); + + it('requires args to be array', function () { + const urlPath = '/a'; + try { + router.on('GET', urlPath, stubHandler, {}); + assert.fail('expected an exception'); + } catch (e) { + assert(e instanceof TypeError); + } + }); }); // on describe('lookup', function () { @@ -274,7 +289,7 @@ describe('Router', function () { router.on(method, urlPath, stubHandler); const path = '/abc'; - const handler = router.lookup(method, path, ctx); + const { handler } = router.lookup(method, path, ctx); assert.strictEqual(handler, stubHandler); }); it('does not find handler with trailing slash', function () { @@ -299,7 +314,7 @@ describe('Router', function () { router.on(method, urlPath, stubHandler); const path = '/abc/'; - const handler = router.lookup(method, path, ctx); + const { handler } = router.lookup(method, path, ctx); assert.strictEqual(handler, stubHandler); }); it('finds handler without context', async function () { @@ -308,7 +323,7 @@ describe('Router', function () { router.on(method, urlPath, stubHandler); const path = '/abc'; - const handler = router.lookup(method, path); + const { handler } = router.lookup(method, path); assert.strictEqual(handler, stubHandler); }); it('finds fallback handler', async function () { @@ -317,7 +332,7 @@ describe('Router', function () { router.on('*', urlPath, stubHandler); const path = '/abc/def'; - const handler = router.lookup(method, path, ctx); + const { handler } = router.lookup(method, path, ctx); assert.strictEqual(handler, stubHandler); }); it('calls unsupported method', async function () { @@ -334,7 +349,7 @@ describe('Router', function () { assert.strictEqual(e.message, 'NoMethod'); } }); - it('does not lookup nonexistant path', async function () { + it('does not lookup non-existent path', async function () { const path = '/foo/bar'; const method = 'GET';