recorder.js CORS header implementation. Closes #4426

This commit is contained in:
Francis Cao committed 2026-08-04 15:34:36 -07:00
1 parent 80d02254ae
commit 9e0034781a
7 files changed
+192 -20

No files matched your search

@@ -0,0 +1,42 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { parseRequest } from '@/lib/request';
import { getWebsite } from '@/queries/prisma';
import { GET, OPTIONS } from './route';
vi.mock('@/lib/request', () => ({
parseRequest: vi.fn(),
}));
vi.mock('@/queries/prisma', () => ({
getWebsite: vi.fn(),
}));
const parseRequestMock = vi.mocked(parseRequest);
const getWebsiteMock = vi.mocked(getWebsite);
beforeEach(() => {
vi.clearAllMocks();
parseRequestMock.mockResolvedValue({ error: undefined });
});
describe('recorder config route CORS', () => {
test('handles preflight requests', async () => {
const response = OPTIONS();
expect(response.status).toBe(204);
expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
expect(response.headers.get('Access-Control-Allow-Headers')).toContain('x-umami-cache');
});
test('includes CORS headers on config responses', async () => {
getWebsiteMock.mockResolvedValue(null as any);
const response = await GET(new Request('http://localhost/api/websites/test/recorder'), {
params: Promise.resolve({ websiteId: '11111111-1111-4111-8111-111111111111' }),
});
expect(response.status).toBe(200);
expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
expect(response.headers.get('Cache-Control')).toContain('max-age=60');
});
});
@@ -1,7 +1,12 @@
import { corsPreflight, getApiCorsHeaders, withCorsHeaders } from '@/lib/cors';
import { getRecorderConfig } from '@/lib/recorder';
import { parseRequest } from '@/lib/request';
import { getWebsite } from '@/queries/prisma';
export function OPTIONS() {
return corsPreflight();
}
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
@@ -9,13 +14,14 @@ export async function GET(
const { error } = await parseRequest(request, null, { skipAuth: true });
if (error) {
return error();
return withCorsHeaders(error());
}
const { websiteId } = await params;
const website = await getWebsite(websiteId);
const headers = {
...getApiCorsHeaders(),
'Cache-Control': 'public, max-age=60, stale-while-revalidate=300',
};