describe('on', function () {
let pathsByLengthOrig;
const stubHandler = () => {};
+ const stubEntry = {
+ handler: stubHandler,
+ handlerArgs: [],
+ };
beforeEach(function () {
pathsByLengthOrig = router.pathsByLength;
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);
});
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);
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);
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);
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);
});
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 () {
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 () {
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 () {
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 () {
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 () {
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';