test(proxy): add PATCH forwarding and absent Content-Length coverage

Closes the two untested code paths flagged in review:
- PATCH method routes to backend with correct URL
- Requests without Content-Length header pass through (NaN > n = false)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-22 18:12:06 +02:00
committed by marcel
parent 253c539f18
commit e1ae299326

View File

@@ -2,7 +2,7 @@ import { describe, it, expect, vi } from 'vitest';
process.env.API_INTERNAL_URL = 'http://backend:8080';
const { GET, PUT, POST } = await import('./+server');
const { GET, PUT, POST, PATCH } = await import('./+server');
function makeEvent(
path: string,
@@ -69,6 +69,21 @@ describe('catch-all API proxy — body size limit', () => {
expect(mockFetch).not.toHaveBeenCalled();
});
it('forwards request when Content-Length header is absent', async () => {
const mockFetch = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }));
const event = makeEvent('documents', 'POST', mockFetch);
(event.request as Request) = new Request('http://localhost/api/documents', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'hello' })
});
const response = await POST(event as never);
expect(response.status).toBe(200);
expect(mockFetch).toHaveBeenCalled();
});
it('does not reject request with Content-Length exactly at 1 MB', async () => {
const mockFetch = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }));
const event = makeEvent('documents', 'POST', mockFetch);
@@ -153,4 +168,22 @@ describe('catch-all API proxy — forwarding', () => {
expect(response.headers.get('Transfer-Encoding')).toBeNull();
});
it('forwards PATCH request to backend with correct URL', async () => {
const mockFetch = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }));
const event = makeEvent('documents/doc-1', 'PATCH', mockFetch);
(event.request as Request) = new Request('http://localhost/api/documents/doc-1', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'updated' })
});
const response = await PATCH(event as never);
expect(mockFetch).toHaveBeenCalledWith(
'http://backend:8080/api/documents/doc-1',
expect.objectContaining({ method: 'PATCH' })
);
expect(response.status).toBe(200);
});
});