diff --git a/src/__tests__/ltProxyAuth.test.ts b/src/__tests__/ltProxyAuth.test.ts new file mode 100644 index 0000000..d804b6f --- /dev/null +++ b/src/__tests__/ltProxyAuth.test.ts @@ -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); +}); diff --git a/src/__tests__/ltProxyHandler.test.ts b/src/__tests__/ltProxyHandler.test.ts new file mode 100644 index 0000000..27cd254 --- /dev/null +++ b/src/__tests__/ltProxyHandler.test.ts @@ -0,0 +1,44 @@ +// deno-lint-ignore-file require-await +import { ltProxyHandler } from '../ltProxyHandler.ts'; +import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts'; +import type { IContext } from 'http-kernel/Interfaces/mod.ts'; + +Deno.test('ltProxyHandler: proxies request and returns response', async () => { + const expectedResponse = new Response('Mocked LT Response', { + status: 200, + headers: { + 'content-type': 'text/plain', + 'x-mocked': 'true', + }, + }); + + // Backup and mock global fetch + const originalFetch = globalThis.fetch; + globalThis.fetch = async () => expectedResponse; + + const req = new Request('http://localhost/v2/check?language=de-DE', { + method: 'POST', + body: new TextEncoder().encode('text=Hallo+Welt'), + headers: { + 'content-type': 'application/x-www-form-urlencoded', + }, + }); + + const ctx: IContext = { + req, + params: {}, + query: { + language: 'de-DE', + }, + state: {}, + }; + + const response = await ltProxyHandler(ctx); + + assertEquals(response.status, 200); + assertEquals(await response.text(), 'Mocked LT Response'); + assertEquals(response.headers.get('x-mocked'), 'true'); + + // Restore fetch + globalThis.fetch = originalFetch; +});