import { assert, assertEquals } from 'test-deps'; import { HTTPStatus } from 'deps'; import Controller, { ControllerHandler, } from 'if/controllers/controller.class.ts'; import { Logger } from 'infra/logger.ts'; const testLogger = Logger.getInstance('test'); Deno.test('Controller class', async () => { const mockHandler = (req: Request) => new Response('Hello, world!'); const mockHandlers = { '/_GET': mockHandler, '/x_POST': mockHandler, }; const controller = new Controller({}, testLogger); controller.setHandlers(mockHandlers); // Test that the handler was stored correctly assert(controller.hasHandler('/', 'GET'), 'specific handler found'); assertEquals(controller.getHandlers(), mockHandlers, 'all handlers found'); // Test the response method const req = new Request('http://localhost/'); const res = controller.response(req, 'Hello, world!'); assertEquals(res.status, 200, 'response http status is OK'); assertEquals( new TextDecoder().decode(new Uint8Array(await res.arrayBuffer())), 'Hello, world!', 'response body is correct', ); // Test the responseJSON method const jsonRes = controller.responseJSON(req, { message: 'Hello, world!' }); assertEquals(jsonRes.status, 200, 'json response http status is OK'); assertEquals( await jsonRes.json(), { message: 'Hello, world!' }, 'response json body is correct', ); // Test basic auth username / password const authReq = new Request('http://localhost/', { headers: { Authorization: 'Basic ZGVtbzpwQDU1dzByZA==', }, }); const authRes = controller.response(authReq, 'Hello, world!'); assertEquals(authRes.status, 200, 'response http status is OK'); const lastMessage = testLogger.getLastMessage(); assert( lastMessage?.startsWith('[INFO] demo "GET /" '), 'username is shown in logging message', ); }); Deno.test('Controller class error handling', async () => { const error = new Error('Test error'); const errorHandler = ( req: Request, error?: string, headers?: Record, ): Response => { return controller.response(req, error ?? 'Unknown error', { status: HTTPStatus.InternalServerError, }); }; const controller = new Controller(); controller.setHandler('/', 'GET', errorHandler); // Test that the handler was stored correctly assert(controller.hasHandler('/', 'GET'), 'handler found'); // Test the error response method const req = new Request('http://localhost/'); const handler = controller.getHandler('/', 'GET'); if (handler) { const res = await handler(req, error.message); assertEquals( res.status, HTTPStatus.InternalServerError, 'response http status is 500', ); assertEquals( new TextDecoder().decode(new Uint8Array(await res.arrayBuffer())), error.message, 'response body is correct', ); } else { assert(false, 'Handler not found'); } }); Deno.test({ name: 'Controller should log debug message for health check user agent', fn: async () => { const userAgent = Deno.env.get('DOCKER_USER_AGENT') ?? ''; const req = new Request('http://localhost', { method: 'GET', headers: userAgent ? new Headers({ 'User-Agent': userAgent, }) : undefined, }); const handler: ControllerHandler = (req: Request) => { return controller.response(req, 'Hello, World!'); }; const controller = new Controller({ '/_GET': handler }, testLogger); const response = await controller.getHandler('/', 'GET')(req); // Check if the expected debug message was logged const debugMessage = testLogger.getMessages().find((message) => message.startsWith('[DEBUG] ') && message.endsWith(`"${userAgent}"`) ); assert(debugMessage !== undefined, 'expected debug message was not logged'); assertEquals(response.status, 200, 'response http status is OK'); }, });