Skip to content

Commit a699add

Browse files
author
Adrià
authored
Merge pull request #263 from HPInc/feat/empty-body-default-to-empty-object
feat: empty body default to empty object
2 parents 56dad50 + 53b168c commit a699add

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

packages/http-server/src/HttpServerModule.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ export abstract class HttpServerModule<
271271
const contextValue = contextParameterConfig
272272
? contextParameterConfig.value
273273
: await httpServerModule.createContext({
274-
request,
275-
reflection: { controllerReflection, methodReflection }
274+
request,
275+
reflection: { controllerReflection, methodReflection }
276276
});
277277

278278
const interceptorsBag = httpServerModule.prepareInterceptorBag({
@@ -420,7 +420,7 @@ export abstract class HttpServerModule<
420420
}
421421

422422
if (parameterConfig.source === 'body') {
423-
acc.body = parameterConfig.value;
423+
acc.body = parameterConfig.value || {};
424424
}
425425

426426
if (parameterConfig.source === 'header') {

packages/http-server/test/unit/HttpServerModule.test.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ describe('HttpServerModule', () => {
433433
methodReflection
434434
})
435435
});
436-
const reqMock = {};
436+
const reqMock = { body: 'invalidType' };
437437
const resMock = {};
438438
const result = await requestHandler(reqMock, resMock);
439439

@@ -459,6 +459,49 @@ describe('HttpServerModule', () => {
459459
expect(methodInterceptorPost.called).to.be.false;
460460
});
461461

462+
it('should create a request handler that process preValidation interceptors', async () => {
463+
const ctrlInterceptorPre = sinon.stub().callsFake(next => next());
464+
const ctrlInterceptorPost = sinon.stub().callsFake(next => next());
465+
466+
const methodInterceptorPre = sinon.stub().callsFake(next => next());
467+
const methodInterceptorPost = sinon.stub().callsFake(next => next());
468+
469+
@interceptor<HttpServerInterceptor>(ctrlInterceptorPre, { stage: 'preValidation' })
470+
@interceptor<HttpServerInterceptor>(ctrlInterceptorPost, { stage: 'postValidation' })
471+
@route.controller({ basePath: '/api/customers' })
472+
class CustomerController {
473+
@interceptor<HttpServerInterceptor>(methodInterceptorPre, { stage: 'preValidation' })
474+
@interceptor<HttpServerInterceptor>(methodInterceptorPost, { stage: 'postValidation' })
475+
@route.post({ path: '/all' })
476+
find(@route.body({ required: true }) body) {
477+
return { body };
478+
}
479+
}
480+
const dummyHttpServer = new DummyHttpServer();
481+
const controllerReflection = reflect(CustomerController);
482+
const methodReflection = controllerReflection.methods[0];
483+
const requestHandler = await dummyHttpServer.createRequestHandler(new CustomerController(), 'find', {
484+
path: '/all',
485+
verb: 'get',
486+
controllerReflection,
487+
methodReflection,
488+
parametersConfig: dummyHttpServer.createParametersConfigurations({
489+
controllerReflection,
490+
methodReflection
491+
})
492+
});
493+
const reqMock = {};
494+
const resMock = {};
495+
const result = await requestHandler(reqMock, resMock);
496+
497+
expect(result[1]).to.containSubset({ body: undefined });
498+
expect(ctrlInterceptorPre.called).to.be.true;
499+
expect(ctrlInterceptorPost.called).to.be.true;
500+
501+
expect(methodInterceptorPre.called).to.be.true;
502+
expect(methodInterceptorPost.called).to.be.true;
503+
});
504+
462505
it('should create a request handler tha process interceptors in the correct order', async () => {
463506
const callsOrder = [];
464507
const createHandler = name => next => {

0 commit comments

Comments
 (0)