test(auth): add unit tests for ltProxyAuth and ltProxyHandler

- Add tests to validate ltProxyAuth behavior for valid, invalid, and missing API keys
- Add tests to verify ltProxyHandler correctly proxies requests and returns responses
This commit is contained in:
2025-05-11 01:10:39 +02:00
parent bcb22f983e
commit 52ce172ef5
2 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
// deno-lint-ignore-file require-await
import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts';
import type { IContext } from 'http-kernel/Interfaces/mod.ts';
import { ltProxyAuth } from '../ltProxyAuth.ts';
Deno.test('ltProxyAuth: accepts valid API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/?apiKey=valid123');
const ctx: IContext = {
req,
params: {},
query: { apiKey: 'valid123' },
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('OK', { status: 200 }),
);
assertEquals(response.status, 200);
assertEquals(await response.text(), 'OK');
});
Deno.test('ltProxyAuth: rejects invalid API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/?apiKey=invalid456');
const ctx: IContext = {
req,
params: {},
query: { apiKey: 'invalid456' },
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('SHOULD NOT HAPPEN'),
);
assertEquals(response.status, 403);
assertEquals(await response.text(), 'Forbidden – Invalid API key');
});
Deno.test('ltProxyAuth: rejects missing API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/');
const ctx: IContext = {
req,
params: {},
query: {},
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('SHOULD NOT HAPPEN'),
);
assertEquals(response.status, 403);
});