File
Blob: src/cloudflare/internal/test/workflows/workflows-mock.js
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | const restartBodies = new Map(); |
| 6 | |
| 7 | export default { |
| 8 | async fetch(request, env, ctx) { |
| 9 | const data = await request.json(); |
| 10 | const reqUrl = new URL(request.url); |
| 11 | |
| 12 | if (reqUrl.pathname === '/get' && request.method === 'POST') { |
| 13 | return Response.json( |
| 14 | { |
| 15 | result: { |
| 16 | id: data.id, |
| 17 | }, |
| 18 | }, |
| 19 | { |
| 20 | status: 200, |
| 21 | headers: { |
| 22 | 'content-type': 'application/json', |
| 23 | }, |
| 24 | } |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | if (reqUrl.pathname === '/create' && request.method === 'POST') { |
| 29 | return Response.json( |
| 30 | { |
| 31 | result: { |
| 32 | id: data.id, |
| 33 | }, |
| 34 | }, |
| 35 | { |
| 36 | status: 201, |
| 37 | headers: { |
| 38 | 'content-type': 'application/json', |
| 39 | }, |
| 40 | } |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | if (reqUrl.pathname === '/createBatch' && request.method === 'POST') { |
| 45 | return Response.json( |
| 46 | { |
| 47 | result: data.map((val) => ({ id: val.id })), |
| 48 | }, |
| 49 | { |
| 50 | status: 201, |
| 51 | headers: { |
| 52 | 'content-type': 'application/json', |
| 53 | }, |
| 54 | } |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | if (reqUrl.pathname === '/restart' && request.method === 'POST') { |
| 59 | restartBodies.set(data.id, data); |
| 60 | return Response.json({}, { status: 200 }); |
| 61 | } |
| 62 | |
| 63 | // Test-only: returns the body from the last /restart call for a given id |
| 64 | if (reqUrl.pathname === '/last-restart' && request.method === 'POST') { |
| 65 | return Response.json( |
| 66 | { result: restartBodies.get(data.id) ?? null }, |
| 67 | { status: 200 } |
| 68 | ); |
| 69 | } |
| 70 | }, |
| 71 | }; |