Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions apps/backend/src/__tests__/app.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
5 changes: 5 additions & 0 deletions apps/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down