X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=test%2Flib%2Frouter.js;h=d63917087d1490021d5441564669008c60f1676c;hp=7cc92a04d984fb9cdac009b9ce672e55acb24691;hb=9fe1e9b22b8e753c44dec77d1dee3d0061b2e991;hpb=91197ffbd90707333da06a714aaec0b8b8143577 diff --git a/test/lib/router.js b/test/lib/router.js index 7cc92a0..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]); }); @@ -191,7 +192,7 @@ describe('Router', function () { beforeEach(function () { pathsByLengthOrig = router.pathsByLength; router.pathsByLength = { - 2: [ router._pathDefinitionToPathMatch('/:id') ], + 2: [ router._pathToRoutePath('/:id') ], }; }); afterEach(function () { @@ -200,17 +201,17 @@ describe('Router', function () { it('adds new path', function () { const urlPath = '/a/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubEntry; + 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'] = stubEntry; - expected[router.METHODS]['POST'] = stubEntry; + 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); @@ -219,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'] = stubEntry; + 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); @@ -230,9 +231,9 @@ describe('Router', function () { it('adds multiple methods', function () { const urlPath = '/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['GET'] = stubEntry; - expected[router.METHODS]['HEAD'] = stubEntry; + 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); @@ -240,8 +241,8 @@ describe('Router', function () { it('adds new wildcard path', function () { const urlPath = '/a/:id'; - const expected = router._pathDefinitionToPathMatch(urlPath); - expected[router.METHODS]['*'] = stubEntry; + const expected = router._pathToRoutePath(urlPath); + expected[Router.kPathMethods]['*'] = stubEntry; router.on('*', urlPath, stubHandler); assert.deepStrictEqual(router.pathsByLength[3][0], expected); });