// 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 body = new URLSearchParams({ apiKey: 'valid123' }); const req = new Request('http://localhost/', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body, }); const ctx: IContext = { req, params: {}, query: {}, 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 body = new URLSearchParams({ apiKey: 'invalid456' }); const req = new Request('http://localhost/', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body, }); const ctx: IContext = { req, params: {}, query: {}, 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 body = new URLSearchParams({ text: 'nur text ohne apiKey' }); const req = new Request('http://localhost/', { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', }, body, }); const ctx: IContext = { req, params: {}, query: {}, state: {}, }; const response = await ltProxyAuth( ctx, async () => new Response('SHOULD NOT HAPPEN'), ); assertEquals(response.status, 403); assertEquals(await response.text(), 'Forbidden – Invalid API key'); });