Skip to content

Commit

Permalink
Merge pull request #263 from HPInc/feat/empty-body-default-to-empty-o…
Browse files Browse the repository at this point in the history
…bject

feat: empty body default to empty object
  • Loading branch information
Adrià authored Sep 7, 2023
2 parents 56dad50 + 53b168c commit a699add
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/http-server/src/HttpServerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -420,7 +420,7 @@ export abstract class HttpServerModule<
}

if (parameterConfig.source === 'body') {
acc.body = parameterConfig.value;
acc.body = parameterConfig.value || {};
}

if (parameterConfig.source === 'header') {
Expand Down
45 changes: 44 additions & 1 deletion packages/http-server/test/unit/HttpServerModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ describe('HttpServerModule', () => {
methodReflection
})
});
const reqMock = {};
const reqMock = { body: 'invalidType' };
const resMock = {};
const result = await requestHandler(reqMock, resMock);

Expand All @@ -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<HttpServerInterceptor>(ctrlInterceptorPre, { stage: 'preValidation' })
@interceptor<HttpServerInterceptor>(ctrlInterceptorPost, { stage: 'postValidation' })
@route.controller({ basePath: '/api/customers' })
class CustomerController {
@interceptor<HttpServerInterceptor>(methodInterceptorPre, { stage: 'preValidation' })
@interceptor<HttpServerInterceptor>(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 => {
Expand Down

0 comments on commit a699add

Please sign in to comment.