Richard Köhl преди 2 години
родител
ревизия
b49b193650
променени са 2 файла, в които са добавени 34 реда и са изтрити 18 реда
  1. 4 3
      src/interfaces/controllers/controller.class.ts
  2. 30 15
      test/interfaces/controllers/controller.class.test.ts

+ 4 - 3
src/interfaces/controllers/controller.class.ts

@@ -67,6 +67,7 @@ export default class Controller {
       url.pathname,
       response.status,
       output.byteLength,
+      req.referrer,
       userAgent,
       userName,
     );
@@ -112,11 +113,11 @@ export default class Controller {
     path: string,
     status: number,
     length: number,
+    referrer = '-',
     userAgent: string,
-    userName?: string,
+    userName = '-',
   ) {
-    const user = userName ? userName + ' ' : '';
-    return `${method} ${path} ${user}${status} ${length} (${userAgent})`;
+    return `${userName} "${method} ${path}" ${status} ${length} "${referrer}" "${userAgent}"`;
   }
 
   private static isHealthCheck(userAgent: string): boolean {

+ 30 - 15
test/interfaces/controllers/controller.class.test.ts

@@ -19,22 +19,27 @@ Deno.test('Controller class', async () => {
   controller.setHandlers(mockHandlers);
 
   // Test that the handler was stored correctly
-  assert(controller.hasHandler('/', 'GET'));
-  assertEquals(controller.getHandlers(), mockHandlers);
+  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);
+  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);
-  assertEquals(await jsonRes.json(), { 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/', {
@@ -43,9 +48,12 @@ Deno.test('Controller class', async () => {
     },
   });
   const authRes = controller.response(authReq, 'Hello, world!');
-  assertEquals(authRes.status, 200);
+  assertEquals(authRes.status, 200, 'response http status is OK');
   const lastMessage = testLogger.getLastMessage();
-  assert(lastMessage?.startsWith('[INFO] GET / demo '));
+  assert(
+    lastMessage?.startsWith('[INFO] demo "GET /" '),
+    'username is shown in logging message',
+  );
 });
 
 Deno.test('Controller class error handling', async () => {
@@ -64,17 +72,22 @@ Deno.test('Controller class error handling', async () => {
   controller.setHandler('/', 'GET', errorHandler);
 
   // Test that the handler was stored correctly
-  assert(controller.hasHandler('/', 'GET'));
+  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);
+    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');
@@ -87,9 +100,11 @@ Deno.test({
     const userAgent = Deno.env.get('DOCKER_USER_AGENT') ?? '';
     const req = new Request('http://localhost', {
       method: 'GET',
-      headers: new Headers({
-        'User-Agent': userAgent,
-      }),
+      headers: userAgent
+        ? new Headers({
+          'User-Agent': userAgent,
+        })
+        : undefined,
     });
     const handler: ControllerHandler = (req: Request) => {
       return controller.response(req, 'Hello, World!');
@@ -99,11 +114,11 @@ Deno.test({
 
     // Check if the expected debug message was logged
     const debugMessage = testLogger.getMessages().find((message) =>
-      message.startsWith('[DEBUG] ') && message.endsWith(`(${userAgent})`)
+      message.startsWith('[DEBUG] ') && message.endsWith(`"${userAgent}"`)
     );
 
-    assert(debugMessage !== undefined, 'Expected debug message was not logged');
+    assert(debugMessage !== undefined, 'expected debug message was not logged');
 
-    assertEquals(response.status, 200);
+    assertEquals(response.status, 200, 'response http status is OK');
   },
 });