update dependency, fixes for logger update
[websub-hub] / test / src / service.js
index 62c0059553a0cbce037a75909d01902063e9178d..2de3b52c4595772834636f38be58b10fb54f9f6a 100644 (file)
@@ -10,15 +10,17 @@ const stubDb = require('../stub-db');
 const stubLogger = require('../stub-logger');
 const Service = require('../../src/service');
 const Config = require('../../config');
+const { AsyncLocalStorage } = require('node:async_hooks');
 
 
 describe('Service', function () {
-  let service, options;
+  let service, options, asyncLocalStorage;
   let req, res, ctx;
 
   beforeEach(function () {
+    asyncLocalStorage = new AsyncLocalStorage();
     options = new Config('test');
-    service = new Service(stubLogger, stubDb, options);
+    service = new Service(stubLogger, stubDb, options, asyncLocalStorage);
     sinon.stub(service.manager);
     sinon.stub(service.sessionManager);
     sinon.stub(service.authenticator);
@@ -46,6 +48,17 @@ describe('Service', function () {
     assert(service);
   });
 
+  describe('preHandler', function () {
+    it('logs requestId', async () => {
+      sinon.stub(service.__proto__.__proto__, 'preHandler').resolves();
+      await service.asyncLocalStorage.run({}, async () => {
+        await service.preHandler(req, res, ctx);
+        const logObject = service.asyncLocalStorage.getStore();
+        assert('requestId' in logObject);
+      });
+    });
+  }); // preHandler
+
   describe('maybeIngestBody', function () {
     beforeEach(function () {
       sinon.stub(service, 'bodyData');