|
|
@@ -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');
|
|
|
},
|
|
|
});
|