diff --git a/apps/backend/src/__tests__/app.test.ts b/apps/backend/src/__tests__/app.test.ts new file mode 100644 index 0000000..4103660 --- /dev/null +++ b/apps/backend/src/__tests__/app.test.ts @@ -0,0 +1,38 @@ +import { describe, it, expect, vi } from 'vitest'; +import { buildApp } from '../app.js'; + +describe('Request logging middleware', () => { + it('logs method and url for each incoming request', async () => { + const app = await buildApp(); + const logSpy = vi.spyOn(app.log, 'info'); + + await app.inject({ method: 'GET', url: '/health' }); + + expect(logSpy).toHaveBeenCalledWith( + expect.objectContaining({ + method: 'GET', + url: '/health', + }), + 'incoming request', + ); + + await app.close(); + }); + + it('logs POST requests with correct method', async () => { + const app = await buildApp(); + const logSpy = vi.spyOn(app.log, 'info'); + + await app.inject({ method: 'POST', url: '/api/u/testuser' }); + + expect(logSpy).toHaveBeenCalledWith( + expect.objectContaining({ + method: 'POST', + url: '/api/u/testuser', + }), + 'incoming request', + ); + + await app.close(); + }); +}); diff --git a/apps/backend/src/app.ts b/apps/backend/src/app.ts index dc023a2..d9924f2 100644 --- a/apps/backend/src/app.ts +++ b/apps/backend/src/app.ts @@ -57,6 +57,11 @@ export async function buildApp() { await app.register(prismaPlugin); await app.register(redisPlugin); + // ─── Request Logging ─── + app.addHook('onRequest', async (request) => { + request.log.info({ method: request.method, url: request.url }, 'incoming request'); + }); + // ─── Auth Decorator ─── app.decorate('authenticate', async function (request: any, reply: any) { try {