const assert = require('assert');
const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
const Router = require('../../lib/router');
-const { DingusError } = require('../../lib/errors');
+const PathParameter = require('../../lib/router/path-parameter');
+const { DingusError, RouterNoPathError, RouterNoMethodError } = require('../../lib/errors');
const noExpectedException = 'did not get expected exception';
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
+ it('defines a path with escaped parameter', function () {
+ const p = '/a/\\:b/c';
+ const expected = ['', 'a', ':b', 'c'];
+ expected[Router.kPathMethods] = {};
+ const r = router._pathToRoutePath(p);
+ assert.deepStrictEqual(r, expected);
+ });
+ }); // _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);
});
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);
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);
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);
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);
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);
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 () {
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]);
});
beforeEach(function () {
pathsByLengthOrig = router.pathsByLength;
router.pathsByLength = {
- 2: [ router._pathDefinitionToPathMatch('/:id') ],
+ 2: [ router._pathToRoutePath('/:id') ],
};
});
afterEach(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);
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);
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);
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);
});
const { handler } = router.lookup(method, path, ctx);
assert.strictEqual(handler, stubHandler);
+ assert.strictEqual(ctx.matchedPath, urlPath);
});
it('does not find handler with trailing slash', function () {
router.ignoreTrailingSlash = false;
router.lookup(method, path, ctx);
assert.fail(noExpectedException);
} catch (e) {
- assert(e instanceof DingusError);
- assert.strictEqual(e.message, 'NoPath');
+ assert(e instanceof RouterNoPathError);
}
});
it('finds handler', function () {
router.lookup(method, path, ctx);
assert.fail(noExpectedException);
} catch (e) {
- assert(e instanceof DingusError);
- assert.strictEqual(e.message, 'NoMethod');
+ assert(e instanceof RouterNoMethodError);
}
});
it('does not lookup non-existent path', async function () {
router.lookup(method, path, ctx);
assert.fail(noExpectedException);
} catch (e) {
- assert(e instanceof DingusError);
- assert.strictEqual(e.message, 'NoPath');
+ assert(e instanceof RouterNoPathError);
}
});