File
Blob: fuzzilli/queue-mock.js
| 1 | function shouldChaos() { |
| 2 | return Math.random() < 0.14; // 14% chaos for Queue |
| 3 | } |
| 4 | |
| 5 | function getChaoticQueueResponse() { |
| 6 | const chaos = Math.random(); |
| 7 | if (chaos < 0.2) { |
| 8 | // Queue is full |
| 9 | return new Response('Queue capacity exceeded', { status: 507 }); |
| 10 | } else if (chaos < 0.4) { |
| 11 | // Message too large |
| 12 | return new Response('Message size limit exceeded', { status: 413 }); |
| 13 | } else if (chaos < 0.6) { |
| 14 | // Rate limit hit |
| 15 | return new Response('Too many messages per second', { status: 429 }); |
| 16 | } else if (chaos < 0.8) { |
| 17 | // Temporary network issue |
| 18 | return new Response('Network timeout', { status: 504 }); |
| 19 | } else { |
| 20 | // Partial success (some messages accepted, some rejected) |
| 21 | return new Response( |
| 22 | JSON.stringify({ |
| 23 | success: false, |
| 24 | failed_messages: Math.floor(Math.random() * 3) + 1, |
| 25 | }), |
| 26 | { |
| 27 | status: 207, // Multi-status |
| 28 | headers: { 'Content-Type': 'application/json' }, |
| 29 | } |
| 30 | ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | export default { |
| 35 | async fetch(request, env, ctx) { |
| 36 | // Random chaos injection |
| 37 | if (shouldChaos()) { |
| 38 | return getChaoticQueueResponse(); |
| 39 | } |
| 40 | |
| 41 | const { pathname } = new URL(request.url); |
| 42 | |
| 43 | if (request.method === 'POST' && pathname === '/message') { |
| 44 | // Single message |
| 45 | const format = request.headers.get('X-Msg-Fmt') || 'v8'; |
| 46 | console.log('Queue message received with format:', format); |
| 47 | |
| 48 | // Sometimes reject based on format |
| 49 | if (Math.random() < 0.03 && format === 'json') { |
| 50 | return new Response('JSON format not supported', { status: 400 }); |
| 51 | } |
| 52 | |
| 53 | return new Response('', { status: 200 }); |
| 54 | } else if (request.method === 'POST' && pathname === '/batch') { |
| 55 | // Batch messages |
| 56 | const body = await request.json(); |
| 57 | const messageCount = body.messages?.length || 0; |
| 58 | console.log('Queue batch received:', messageCount, 'messages'); |
| 59 | |
| 60 | // Sometimes simulate partial batch acceptance |
| 61 | if (Math.random() < 0.08 && messageCount > 1) { |
| 62 | return new Response( |
| 63 | JSON.stringify({ |
| 64 | accepted: Math.floor(messageCount * 0.7), |
| 65 | rejected: Math.ceil(messageCount * 0.3), |
| 66 | errors: ['Message 2 invalid format', 'Message 4 too large'], |
| 67 | }), |
| 68 | { |
| 69 | status: 207, |
| 70 | headers: { 'Content-Type': 'application/json' }, |
| 71 | } |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | return new Response('', { status: 200 }); |
| 76 | } |
| 77 | |
| 78 | return new Response('Queue mock ready', { status: 200 }); |
| 79 | }, |
| 80 | }; |