From 778332553f9f2676df404f4a07cff07ffc174e1c Mon Sep 17 00:00:00 2001 From: adriasala124 Date: Thu, 7 Sep 2023 13:53:25 +0200 Subject: [PATCH 1/2] feat: empty body default to empty object --- packages/http-server/src/HttpServerModule.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/http-server/src/HttpServerModule.ts b/packages/http-server/src/HttpServerModule.ts index 06c51883..7b2c4347 100644 --- a/packages/http-server/src/HttpServerModule.ts +++ b/packages/http-server/src/HttpServerModule.ts @@ -271,8 +271,8 @@ export abstract class HttpServerModule< const contextValue = contextParameterConfig ? contextParameterConfig.value : await httpServerModule.createContext({ - request, - reflection: { controllerReflection, methodReflection } + request, + reflection: { controllerReflection, methodReflection } }); const interceptorsBag = httpServerModule.prepareInterceptorBag({ @@ -420,7 +420,7 @@ export abstract class HttpServerModule< } if (parameterConfig.source === 'body') { - acc.body = parameterConfig.value; + acc.body = parameterConfig.value || {}; } if (parameterConfig.source === 'header') { From 53b168c946fd65d6665c69a69150fb374d6e5dc6 Mon Sep 17 00:00:00 2001 From: adriasala124 Date: Thu, 7 Sep 2023 14:23:05 +0200 Subject: [PATCH 2/2] fix: update tests --- .../test/unit/HttpServerModule.test.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/http-server/test/unit/HttpServerModule.test.ts b/packages/http-server/test/unit/HttpServerModule.test.ts index eb1d1a1d..d2c9c5b4 100644 --- a/packages/http-server/test/unit/HttpServerModule.test.ts +++ b/packages/http-server/test/unit/HttpServerModule.test.ts @@ -433,7 +433,7 @@ describe('HttpServerModule', () => { methodReflection }) }); - const reqMock = {}; + const reqMock = { body: 'invalidType' }; const resMock = {}; const result = await requestHandler(reqMock, resMock); @@ -459,6 +459,49 @@ describe('HttpServerModule', () => { expect(methodInterceptorPost.called).to.be.false; }); + it('should create a request handler that process preValidation interceptors', async () => { + const ctrlInterceptorPre = sinon.stub().callsFake(next => next()); + const ctrlInterceptorPost = sinon.stub().callsFake(next => next()); + + const methodInterceptorPre = sinon.stub().callsFake(next => next()); + const methodInterceptorPost = sinon.stub().callsFake(next => next()); + + @interceptor(ctrlInterceptorPre, { stage: 'preValidation' }) + @interceptor(ctrlInterceptorPost, { stage: 'postValidation' }) + @route.controller({ basePath: '/api/customers' }) + class CustomerController { + @interceptor(methodInterceptorPre, { stage: 'preValidation' }) + @interceptor(methodInterceptorPost, { stage: 'postValidation' }) + @route.post({ path: '/all' }) + find(@route.body({ required: true }) body) { + return { body }; + } + } + const dummyHttpServer = new DummyHttpServer(); + const controllerReflection = reflect(CustomerController); + const methodReflection = controllerReflection.methods[0]; + const requestHandler = await dummyHttpServer.createRequestHandler(new CustomerController(), 'find', { + path: '/all', + verb: 'get', + controllerReflection, + methodReflection, + parametersConfig: dummyHttpServer.createParametersConfigurations({ + controllerReflection, + methodReflection + }) + }); + const reqMock = {}; + const resMock = {}; + const result = await requestHandler(reqMock, resMock); + + expect(result[1]).to.containSubset({ body: undefined }); + expect(ctrlInterceptorPre.called).to.be.true; + expect(ctrlInterceptorPost.called).to.be.true; + + expect(methodInterceptorPre.called).to.be.true; + expect(methodInterceptorPost.called).to.be.true; + }); + it('should create a request handler tha process interceptors in the correct order', async () => { const callsOrder = []; const createHandler = name => next => {