X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Frouter.js;h=488d5ceec03ceb5f006230999ce335cf8e2d111c;hb=8e981f499075ec8bbd9229076d4448e6212ca87a;hp=d63917087d1490021d5441564669008c60f1676c;hpb=f944684c37532e67a7d28e34909178435cba03a5;p=squeep-api-dingus diff --git a/test/lib/router.js b/test/lib/router.js index d639170..488d5ce 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -5,8 +5,8 @@ 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 PathParameter = require('../../lib/router/path-parameter'); +const { DingusError, RouterNoPathError, RouterNoMethodError } = require('../../lib/errors'); const noExpectedException = 'did not get expected exception'; @@ -46,6 +46,13 @@ describe('Router', function () { const r = router._pathToRoutePath(p); assert.deepStrictEqual(r, expected); }); + 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 () { @@ -253,7 +260,7 @@ describe('Router', function () { router.on('FLARP', urlPath, stubHandler); assert.fail('expected an exception'); } catch (e) { - assert.strictEqual(e.name, 'DingusError'); + assert(e instanceof DingusError); assert.strictEqual(e.message, 'invalid method \'FLARP\''); } }); @@ -291,6 +298,7 @@ describe('Router', function () { 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; @@ -303,8 +311,7 @@ describe('Router', function () { 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 () { @@ -345,8 +352,7 @@ describe('Router', 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 () { @@ -357,8 +363,7 @@ describe('Router', function () { router.lookup(method, path, ctx); assert.fail(noExpectedException); } catch (e) { - assert(e instanceof DingusError); - assert.strictEqual(e.message, 'NoPath'); + assert(e instanceof RouterNoPathError); } });