X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=test%2Flib%2Frouter.js;h=7cc92a04d984fb9cdac009b9ce672e55acb24691;hp=f4c54a997fb26a0adcf944f208dfa1e81cb73fbf;hb=1bfd3f26e768c390a6be543281e79b7ea5c4b9c5;hpb=0f19dc825bb6e74106db319b79950ca463ad40f0 diff --git a/test/lib/router.js b/test/lib/router.js index f4c54a9..7cc92a0 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -183,6 +183,10 @@ describe('Router', function () { describe('on', function () { let pathsByLengthOrig; const stubHandler = () => {}; + const stubEntry = { + handler: stubHandler, + handlerArgs: [], + }; beforeEach(function () { pathsByLengthOrig = router.pathsByLength; @@ -197,7 +201,7 @@ describe('Router', function () { it('adds new path', function () { const urlPath = '/a/:id'; const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubHandler; + expected[router.METHODS]['GET'] = stubEntry; router.on('GET', urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[3][0], expected); }); @@ -205,8 +209,8 @@ describe('Router', function () { 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; + expected[router.METHODS]['GET'] = stubEntry; + expected[router.METHODS]['POST'] = stubEntry; router.on('GET', urlPath, stubHandler); router.on('POST', urlPath, stubHandler); @@ -216,7 +220,7 @@ 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; + expected[router.METHODS]['GET'] = stubEntry; router.on('GET', urlPath, stubHandler); urlPath = '/a/b/x/y'; router.on('GET', urlPath, stubHandler); @@ -227,8 +231,8 @@ 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; + expected[router.METHODS]['GET'] = stubEntry; + expected[router.METHODS]['HEAD'] = stubEntry; router.on(['GET', 'HEAD'], urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[2][0], expected); @@ -237,7 +241,7 @@ describe('Router', function () { it('adds new wildcard path', function () { const urlPath = '/a/:id'; const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['*'] = stubHandler; + expected[router.METHODS]['*'] = stubEntry; router.on('*', urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[3][0], expected); }); @@ -252,6 +256,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 +288,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 +313,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 +322,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 +331,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 +348,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';