allow additional arguments to be passed to handler functions
[squeep-api-dingus] / test / lib / router.js
index f4c54a997fb26a0adcf944f208dfa1e81cb73fbf..7cc92a04d984fb9cdac009b9ce672e55acb24691 100644 (file)
@@ -183,6 +183,10 @@ describe('Router', function () {
   describe('on', function () {
     let pathsByLengthOrig;
     const stubHandler = () => {};
+    const stubEntry = {
+      handler: stubHandler,
+      handlerArgs: [],
+    };
 
     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);
-      expected[router.METHODS]['GET'] = stubHandler;
+      expected[router.METHODS]['GET'] = stubEntry;
       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);
-      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);
@@ -216,7 +220,7 @@ describe('Router', function () {
     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);
@@ -227,8 +231,8 @@ describe('Router', function () {
     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);
@@ -237,7 +241,7 @@ describe('Router', function () {
     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);
     });
@@ -252,6 +256,16 @@ describe('Router', function () {
         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 () {
@@ -274,7 +288,7 @@ describe('Router', 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 () {
@@ -299,7 +313,7 @@ describe('Router', 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 () {
@@ -308,7 +322,7 @@ describe('Router', 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 () {
@@ -317,7 +331,7 @@ describe('Router', 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 () {
@@ -334,7 +348,7 @@ describe('Router', 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';