- Switch API key validation to use POST body parameters - Add support for `application/x-www-form-urlencoded` content type - Store parsed body in context state for further use - Reject unsupported content types with 415 status
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
// 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');
|
|
});
|