(routes: Record<string, Handler>)
| 33 | } |
| 34 | |
| 35 | function startMock(routes: Record<string, Handler>): MockServer { |
| 36 | const requests: MockServer['requests'] = []; |
| 37 | const server = Bun.serve({ |
| 38 | port: 0, |
| 39 | async fetch(req) { |
| 40 | const u = new URL(req.url); |
| 41 | const key = `${req.method} ${u.pathname}`; |
| 42 | // Log method+path only. Handlers that need the body read it themselves; |
| 43 | // Response bodies can only be consumed once. |
| 44 | requests.push({ method: req.method, path: u.pathname }); |
| 45 | const handler = routes[key] || routes[`${req.method} *`]; |
| 46 | if (!handler) { |
| 47 | return new Response( |
| 48 | JSON.stringify({ message: `no mock for ${key}` }), |
| 49 | { status: 404, headers: { 'content-type': 'application/json' } } |
| 50 | ); |
| 51 | } |
| 52 | return handler(req); |
| 53 | }, |
| 54 | }); |
| 55 | const base = `http://localhost:${server.port}`; |
| 56 | return { |
| 57 | url: base, |
| 58 | close: () => server.stop(true), |
| 59 | requests, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | async function runBin( |
| 64 | args: string[], |
no test coverage detected