Repository navigation
Expand file tree
/
Copy pathworker.test.js
More file actions
138 lines (123 loc) · 4.23 KB
/
Copy pathworker.test.js
File metadata and controls
138 lines (123 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { describe, expect, it, vi } from 'vitest';
import worker from './worker.js';
const manifest = {
version: 1,
imageCount: 3,
galleryCount: 2,
galleries: [
{ name: 'anime', label: 'Anime', total: 2, pc: 1, mobile: 1 },
{ name: 'nature', label: '', total: 1, pc: 1, mobile: 0 }
],
images: [
{
key: 'images/anime/pc/pc.webp',
path: '/image/images/anime/pc/pc.webp',
gallery: 'anime',
device: 'pc',
filename: 'pc.webp',
size: 10,
width: 1920,
height: 1080,
type: 'webp',
mtimeMs: 3
},
{
key: 'images/anime/mobile/mobile.webp',
path: '/image/images/anime/mobile/mobile.webp',
gallery: 'anime',
device: 'mobile',
filename: 'mobile.webp',
size: 11,
width: 1080,
height: 1920,
type: 'webp',
mtimeMs: 2
},
{
key: 'images/nature/pc/nature.jpg',
path: '/image/images/nature/pc/nature.jpg',
gallery: 'nature',
device: 'pc',
filename: 'nature.jpg',
size: 12,
width: 1600,
height: 900,
type: 'jpg',
mtimeMs: 1
}
],
generatedAt: '2026-05-27T00:00:00.000Z'
};
function r2Object(body, headers = {}) {
return {
body,
size: typeof body === 'string' ? body.length : undefined,
httpEtag: '"test-etag"',
text: async () => body,
writeHttpMetadata(target) {
for (const [key, value] of Object.entries(headers)) {
target.set(key, value);
}
}
};
}
function createEnv(files = {}) {
const bucket = {
get: vi.fn(async (key) => {
if (key === 'manifest.json') return r2Object(JSON.stringify(manifest), { 'Content-Type': 'application/json' });
if (files[key]) return r2Object(files[key].body, files[key].headers);
return null;
})
};
return {
IMAGE_BUCKET: bucket,
MANIFEST_CACHE_SECONDS: '0',
IMAGE_KEY_PREFIX: 'images'
};
}
async function fetchJson(url, env = createEnv()) {
const response = await worker.fetch(new Request(url), env);
return { response, data: await response.json() };
}
describe('Cloudflare Worker random image API', () => {
it('returns JSON for a selected gallery and device', async () => {
const { response, data } = await fetchJson('https://img.example.com/image/api/random?gallery=anime&device=mobile&type=json');
expect(response.status).toBe(200);
expect(data.url).toBe('https://img.example.com/image/images/anime/mobile/mobile.webp');
expect(data.gallery).toBe('anime');
expect(data.device).toBe('mobile');
expect(data.total).toBe(1);
});
it('keeps type=mobile compatibility as a redirect device shortcut', async () => {
const env = createEnv();
const response = await worker.fetch(new Request('https://img.example.com/image/api/random?gallery=anime&type=mobile'), env);
expect(response.status).toBe(302);
expect(response.headers.get('Location')).toBe('https://img.example.com/image/images/anime/mobile/mobile.webp');
});
it('returns galleries and stats from the manifest', async () => {
const galleries = await fetchJson('https://img.example.com/image/api/galleries');
const stats = await fetchJson('https://img.example.com/image/api/stats');
expect(galleries.data.galleries).toHaveLength(2);
expect(stats.data.imageCount).toBe(3);
expect(stats.data.galleryCount).toBe(2);
});
it('serves image objects from R2 for static image paths', async () => {
const env = createEnv({
'images/anime/pc/pc.webp': {
body: 'image-body',
headers: { 'Content-Type': 'image/webp' }
}
});
const response = await worker.fetch(new Request('https://img.example.com/image/images/anime/pc/pc.webp'), env);
expect(response.status).toBe(200);
expect(response.headers.get('Content-Type')).toBe('image/webp');
expect(response.headers.get('Cache-Control')).toContain('max-age=31536000');
expect(await response.text()).toBe('image-body');
expect(env.IMAGE_BUCKET.get).toHaveBeenCalledWith('images/anime/pc/pc.webp');
});
it('returns clear validation errors', async () => {
const { response, data } = await fetchJson('https://img.example.com/image/api/random?gallery=../bad&type=json');
expect(response.status).toBe(400);
expect(data.error).toBe('非法图库名');
});
});