55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
get: vi.fn(),
|
|
fetchThumbnail: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('$lib/server/database', () => ({
|
|
getDatabase: vi.fn(async () => ({
|
|
prepare: vi.fn(async () => ({ get: mocks.get })),
|
|
})),
|
|
}));
|
|
|
|
vi.mock('$lib/server/ubisoft-news-client', () => ({
|
|
fetchUbisoftNewsThumbnail: mocks.fetchThumbnail,
|
|
}));
|
|
|
|
import { GET } from './+server';
|
|
|
|
const articleId = '9890c398-0522-4e8c-8fdb-78de9c66deb6';
|
|
const sourceUrl = 'https://staticctf.ubisoft.com/news/cover.jpg';
|
|
|
|
describe('Ubisoft News image proxy', () => {
|
|
beforeEach(() => {
|
|
mocks.get.mockReset().mockResolvedValue({ thumbnail_url: sourceUrl });
|
|
mocks.fetchThumbnail.mockReset().mockResolvedValue({
|
|
body: new Uint8Array([1, 2, 3]),
|
|
contentType: 'image/jpeg',
|
|
});
|
|
});
|
|
|
|
it('serves a same-origin cached image and honors its ETag', async () => {
|
|
const first = await GET({
|
|
params: { id: articleId },
|
|
request: new Request(`http://localhost/images/news/${articleId}`),
|
|
} as never);
|
|
expect(first.status).toBe(200);
|
|
expect(first.headers.get('content-type')).toBe('image/jpeg');
|
|
expect(first.headers.get('cross-origin-resource-policy')).toBe(
|
|
'same-origin',
|
|
);
|
|
const etag = first.headers.get('etag');
|
|
expect(etag).toBeTruthy();
|
|
|
|
const second = await GET({
|
|
params: { id: articleId },
|
|
request: new Request(`http://localhost/images/news/${articleId}`, {
|
|
headers: { 'if-none-match': String(etag) },
|
|
}),
|
|
} as never);
|
|
expect(second.status).toBe(304);
|
|
expect(mocks.fetchThumbnail).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|