|
1 | 1 | require "json" |
2 | 2 |
|
| 3 | +$mock = { |
| 4 | + routes: {}, |
| 5 | + requests: [] # array of hashes |
| 6 | +} |
| 7 | + |
| 8 | +def json(status, obj, headers: {}) |
| 9 | + [status, { "content-type" => "application/json" }.merge(headers), [JSON.dump(obj)]] |
| 10 | +end |
| 11 | + |
| 12 | +def read_body(env) |
| 13 | + io = env["rack.input"] |
| 14 | + return "" unless io |
| 15 | + body = io.read |
| 16 | + io.rewind if io.respond_to?(:rewind) |
| 17 | + body |
| 18 | +end |
| 19 | + |
| 20 | +def record_request!(env) |
| 21 | + method = env["REQUEST_METHOD"] |
| 22 | + path = env["PATH_INFO"] |
| 23 | + query = env["QUERY_STRING"] |
| 24 | + body = read_body(env) |
| 25 | + |
| 26 | + # Record a safe subset of headers (HTTP_... keys + content-type/length) |
| 27 | + headers = env.each_with_object({}) do |(k, v), h| |
| 28 | + next unless k.start_with?("HTTP_") || k == "CONTENT_TYPE" || k == "CONTENT_LENGTH" |
| 29 | + h[k] = v |
| 30 | + end |
| 31 | + |
| 32 | + $mock[:requests] << { |
| 33 | + method: method, |
| 34 | + path: path, |
| 35 | + query: query, |
| 36 | + headers: headers, |
| 37 | + body: body |
| 38 | + } |
| 39 | +end |
| 40 | + |
3 | 41 | run lambda { |env| |
4 | | - path = env["PATH_INFO"] |
| 42 | + method = env["REQUEST_METHOD"] |
| 43 | + path = env["PATH_INFO"] |
| 44 | + |
| 45 | + # ---- Control API ---- |
| 46 | + if method == "POST" && path == "/__control__/reset" |
| 47 | + $mock[:routes].clear |
| 48 | + $mock[:requests].clear |
| 49 | + return json(200, { ok: true }) |
| 50 | + end |
| 51 | + |
| 52 | + if method == "POST" && path == "/__control__/set" |
| 53 | + payload = JSON.parse(read_body(env)) |
| 54 | + m = payload.fetch("method") |
| 55 | + p = payload.fetch("path") |
5 | 56 |
|
6 | | - case path |
7 | | - when "/ping" |
8 | | - [200, { "Content-Type" => "application/json" }, [JSON.dump({ message: "pong" })]] |
9 | | - when "/status/500" |
10 | | - [500, { "Content-Type" => "application/json" }, [JSON.dump({ error: "boom" })]] |
11 | | - else |
12 | | - [404, { "Content-Type" => "application/json" }, [JSON.dump({ error: "not found" })]] |
| 57 | + $mock[:routes][[m, p]] = { |
| 58 | + status: payload.fetch("status"), |
| 59 | + headers: payload["headers"] || {}, |
| 60 | + body: payload["body"] || "" |
| 61 | + } |
| 62 | + |
| 63 | + return json(200, { ok: true }) |
| 64 | + end |
| 65 | + |
| 66 | + if method == "GET" && path == "/__control__/requests" |
| 67 | + return json(200, { requests: $mock[:requests] }) |
13 | 68 | end |
| 69 | + |
| 70 | + if method == "POST" && path == "/__control__/requests/clear" |
| 71 | + $mock[:requests].clear |
| 72 | + return json(200, { ok: true }) |
| 73 | + end |
| 74 | + |
| 75 | + # ---- Record every non-control request ---- |
| 76 | + record_request!(env) |
| 77 | + |
| 78 | + # ---- Serve mocked responses ---- |
| 79 | + route = $mock[:routes][[method, path]] |
| 80 | + return json(404, { error: "not mocked", method: method, path: path }) unless route |
| 81 | + |
| 82 | + status = route[:status] |
| 83 | + headers = route[:headers] || {} |
| 84 | + body = route[:body] |
| 85 | + |
| 86 | + body_str = |
| 87 | + case body |
| 88 | + when String then body |
| 89 | + else JSON.dump(body) |
| 90 | + end |
| 91 | + |
| 92 | + headers = { "content-type" => "application/json" }.merge(headers) if body.is_a?(Hash) || body.is_a?(Array) |
| 93 | + |
| 94 | + [status, headers, [body_str]] |
14 | 95 | } |
0 commit comments