From 4da8a7693be1a08fad598211d80e1614a98c02f9 Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Fri, 3 Feb 2023 16:45:55 +0200 Subject: [PATCH] wip --- __tests__/basePath.unit.js | 18 ++++++++++++++++++ index.js | 2 ++ 2 files changed, 20 insertions(+) diff --git a/__tests__/basePath.unit.js b/__tests__/basePath.unit.js index 5e8fc5e..7df7e4e 100644 --- a/__tests__/basePath.unit.js +++ b/__tests__/basePath.unit.js @@ -17,9 +17,21 @@ let event = { } } +api.use(function (req, res, next) { + console.log('HELLO FROM MIDDLEWARE') + req.testMiddleware = "123"; + next(); +}); + /******************************************************************************/ /*** DEFINE TEST ROUTES ***/ /******************************************************************************/ +api.get("/", function(req, res) { + res.status(200).json({ + testMiddleware: req.testMiddleware, + }) +}); + api.get('/test', function(req,res) { res.status(200).json({ method: 'get', status: 'ok' }) }) @@ -40,6 +52,12 @@ api.get('/test/test2/test3', function(req,res) { describe('Base Path Tests:', function() { + it('should return testMiddleware: 123 when calling the root path', async function() { + let _event = Object.assign({},event,{ path: '/v1' }) + let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) + expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false }) + }) + it('Simple path with base: /v1/test', async function() { let _event = Object.assign({},event,{ path: '/v1/test' }) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) diff --git a/index.js b/index.js index 86e3439..73811ed 100644 --- a/index.js +++ b/index.js @@ -149,6 +149,7 @@ class API { // Make a local copy of routes let routes = this._routes; + // console.log('routes', JSON.stringify(routes)); // Create a local stack for inheritance let _stack = { '*': [], m: [] }; @@ -477,6 +478,7 @@ class API { this.METHOD('__MW__', route, ...middleware); }); } + } // end use // Finally handler