Skip to content
File

Blob: fuzzilli/kv-mock.js

javascript70 lines
1// Chaos helpers
2function shouldChaos() {
3 return Math.random() < 0.12; // 12% chaos
4}
5 
6function getChaoticResponse() {
7 const chaos = Math.random();
8 if (chaos < 0.3) {
9 // Rate limited
10 return new Response('Rate limited', { status: 429 });
11 } else if (chaos < 0.5) {
12 // Service unavailable
13 return new Response('Service temporarily unavailable', { status: 503 });
14 } else if (chaos < 0.7) {
15 // Malformed JSON
16 return new Response('{"broken": json}', {
17 status: 200,
18 headers: { 'Content-Type': 'application/json' },
19 });
20 } else if (chaos < 0.9) {
21 // Timeout simulation (just return very slow)
22 return new Response('Request timeout', { status: 408 });
23 } else {
24 // Completely wrong content type
25 return new Response('<html>This is not JSON</html>', {
26 status: 200,
27 headers: { 'Content-Type': 'text/html' },
28 });
29 }
30}
31 
32export default {
33 async fetch(request, env, ctx) {
34 // Random chaos injection
35 if (shouldChaos()) {
36 return getChaoticResponse();
37 }
38 
39 const { pathname } = new URL(request.url);
40 const method = request.method;
41 
42 if (method === 'GET' && pathname.startsWith('/values/')) {
43 // GET /values/{key}
44 const key = pathname.slice(8);
45 if (key === 'test-key') {
46 return new Response('test-value', {
47 headers: { 'Content-Type': 'text/plain' },
48 });
49 }
50 return new Response(null, { status: 404 });
51 } else if (method === 'PUT' && pathname.startsWith('/values/')) {
52 // PUT /values/{key}
53 return new Response(null, { status: 200 });
54 } else if (method === 'GET' && pathname === '/keys') {
55 // LIST operation
56 return new Response(
57 JSON.stringify({
58 keys: [{ name: 'test-key' }],
59 list_complete: true,
60 }),
61 {
62 headers: { 'Content-Type': 'application/json' },
63 }
64 );
65 }
66 
67 return new Response('KV mock ready', { status: 200 });
68 },
69};