Skip to content

Commit cbdf737

Browse files
feat(core): auto run get middleware for head requests
1 parent 4593f58 commit cbdf737

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

integration/hello-world/e2e/middleware-fastify.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,5 +765,32 @@ describe('Middleware (FastifyAdapter)', () => {
765765
await app.close();
766766
});
767767
});
768+
769+
describe('HEAD auto-forwarding to GET', () => {
770+
beforeEach(async () => {
771+
app = (
772+
await Test.createTestingModule({
773+
imports: [TestModule],
774+
}).compile()
775+
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
776+
777+
await app.init();
778+
});
779+
780+
it(`GET forRoutes(HEAD /abc/def)`, () => {
781+
return app
782+
.inject({
783+
method: 'HEAD',
784+
url: '/abc/def',
785+
})
786+
.then(({ payload }) =>
787+
expect(payload).to.be.eql(MIDDLEWARE_RETURN_VALUE),
788+
);
789+
});
790+
791+
afterEach(async () => {
792+
await app.close();
793+
});
794+
});
768795
});
769796
});

packages/core/middleware/middleware-module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,12 @@ export class MiddlewareModule<
335335
res: TResponse,
336336
next: () => void,
337337
) => {
338-
if (applicationRef.getRequestMethod?.(req) === requestMethod) {
338+
const actualRequestMethod = applicationRef.getRequestMethod?.(req);
339+
if (
340+
actualRequestMethod === requestMethod ||
341+
(actualRequestMethod === RequestMethod[RequestMethod.HEAD] &&
342+
requestMethod === RequestMethod[RequestMethod.GET])
343+
) {
339344
return proxy(req, res, next);
340345
}
341346
return next();

0 commit comments

Comments
 (0)