Skip to content

mock

const mock: (config?) => Policy

Defined in: src/policies/mock.ts:62

Return a static mock response, bypassing the upstream entirely.

Useful for development stubs, testing, and placeholder routes. Runs at priority 999 (always last) and short-circuits — next() is never called, so no upstream request is made. Object bodies are automatically JSON-serialized with content-type: application/json.

MockConfig

Status code, response body, headers, and artificial delay. All fields optional.

Policy

A Policy at priority 999 (replaces the upstream).

import { createGateway } from "@homegrower-club/stoma";
import { mock } from "@homegrower-club/stoma/policies";
createGateway({
routes: [{
path: "/api/stub",
pipeline: {
policies: [
// Return a JSON stub with 200ms simulated latency
mock({
body: { message: "Hello from stub" },
delayMs: 200,
}),
],
upstream: { type: "handler", handler: () => new Response() }, // never reached
},
}],
});
// Simulate a 503 maintenance page
mock({
status: 503,
body: "Service temporarily unavailable",
headers: { "retry-after": "300" },
});