- Add tests to validate ltProxyAuth behavior for valid, invalid, and missing API keys - Add tests to verify ltProxyHandler correctly proxies requests and returns responses
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
// 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;
|
|
});
|