projects
/
squeep-api-dingus
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
allow additional arguments to be passed to handler functions
[squeep-api-dingus]
/
test
/
lib
/
router.js
diff --git
a/test/lib/router.js
b/test/lib/router.js
index f4c54a997fb26a0adcf944f208dfa1e81cb73fbf..7cc92a04d984fb9cdac009b9ce672e55acb24691 100644
(file)
--- a/
test/lib/router.js
+++ b/
test/lib/router.js
@@
-183,6
+183,10
@@
describe('Router', function () {
describe('on', function () {
let pathsByLengthOrig;
const stubHandler = () => {};
describe('on', function () {
let pathsByLengthOrig;
const stubHandler = () => {};
+ const stubEntry = {
+ handler: stubHandler,
+ handlerArgs: [],
+ };
beforeEach(function () {
pathsByLengthOrig = router.pathsByLength;
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);
it('adds new path', function () {
const urlPath = '/a/:id';
const expected = router._pathDefinitionToPathMatch(urlPath);
- expected[router.METHODS]['GET'] = stub
Handler
;
+ expected[router.METHODS]['GET'] = stub
Entry
;
router.on('GET', urlPath, stubHandler);
assert.deepStrictEqual(router.pathsByLength[3][0], expected);
});
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);
it('adds new method to path', function () {
const urlPath = '/a/:id';
const expected = router._pathDefinitionToPathMatch(urlPath);
- expected[router.METHODS]['GET'] = stub
Handler
;
- expected[router.METHODS]['POST'] = stub
Handler
;
+ expected[router.METHODS]['GET'] = stub
Entry
;
+ expected[router.METHODS]['POST'] = stub
Entry
;
router.on('GET', urlPath, stubHandler);
router.on('POST', urlPath, stubHandler);
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);
it('add some more paths', function () {
let urlPath = '/a/b/c/d';
const expected = router._pathDefinitionToPathMatch(urlPath);
- expected[router.METHODS]['GET'] = stub
Handler
;
+ expected[router.METHODS]['GET'] = stub
Entry
;
router.on('GET', urlPath, stubHandler);
urlPath = '/a/b/x/y';
router.on('GET', urlPath, stubHandler);
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);
it('adds multiple methods', function () {
const urlPath = '/:id';
const expected = router._pathDefinitionToPathMatch(urlPath);
- expected[router.METHODS]['GET'] = stub
Handler
;
- expected[router.METHODS]['HEAD'] = stub
Handler
;
+ expected[router.METHODS]['GET'] = stub
Entry
;
+ expected[router.METHODS]['HEAD'] = stub
Entry
;
router.on(['GET', 'HEAD'], urlPath, stubHandler);
assert.deepStrictEqual(router.pathsByLength[2][0], expected);
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);
it('adds new wildcard path', function () {
const urlPath = '/a/:id';
const expected = router._pathDefinitionToPathMatch(urlPath);
- expected[router.METHODS]['*'] = stub
Handler
;
+ expected[router.METHODS]['*'] = stub
Entry
;
router.on('*', urlPath, stubHandler);
assert.deepStrictEqual(router.pathsByLength[3][0], expected);
});
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\'');
}
});
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 () {
}); // on
describe('lookup', function () {
@@
-274,7
+288,7
@@
describe('Router', function () {
router.on(method, urlPath, stubHandler);
const path = '/abc';
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 () {
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/';
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 () {
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';
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 () {
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';
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(handler, stubHandler);
});
it('calls unsupported method', async function () {
@@
-334,7
+348,7
@@
describe('Router', function () {
assert.strictEqual(e.message, 'NoMethod');
}
});
assert.strictEqual(e.message, 'NoMethod');
}
});
- it('does not lookup non
exista
nt path', async function () {
+ it('does not lookup non
-existe
nt path', async function () {
const path = '/foo/bar';
const method = 'GET';
const path = '/foo/bar';
const method = 'GET';