Routes, Cases, and Behaviors
A Route is one YAML file with an id, one transport, and a match saying where the
interaction happens. Its Cases say which request shape matched, and each Case owns named
Behaviors: what Decoy does.
Decoy loads every .yaml/.yml file under the routes directory, recursively, so you can group
files in folders. Duplicate Route ids fail startup.
Reuse a mapping with YAML anchors
In a Route file, a Collections file, or decoy.yaml, mark a mapping with &name and
merge it elsewhere in the same file with <<: *name. Here both Behaviors return the same body:
Keys written directly in the mapping take precedence, wherever they appear. To merge several
mappings, use <<: [*first, *second]. When both supply the same key, the first wins. Merging
is shallow, so setting body on created replaces the whole inherited body.
Case match
A Case with no match matches every request to its Route. Otherwise match is a mapping of
fields, and all fields must match:
Path segments and pathParams compare percent-decoded, like query (/users/a%20b gives id: a b; %2F stays inside one segment). The envelope path stays as sent.
body is the parsed JSON when the request's content-type is JSON (application/json,
*+json), otherwise the raw text. Repeated request headers are joined with ", ". An unknown
field, such as a misspelled querry, fails startup, and so does a list or null value in
pathParams, query, headers or cookies, or a pathParams key the Route path doesn't define.
Predicates
When a literal pattern isn't enough, use a JMESPath predicate: a string
that is exactly ${ <expression> }. It can replace the whole match or any single field, and it
always sees the whole request:
The request envelope, what predicates and templates see:
Truthiness follows JMESPath: null, false, "", [], and {} don't match, and anything else
does. Literals inside expressions use backticks (`1000`) or quotes ('GetUser'). Header names
with dashes need quotes: headers."x-tier". Invalid expressions fail startup, naming the
route:case.
Response Behavior
The default kind. All fields are optional:
Delay
Any Behavior, Response or Passthrough, can wait before answering. Set delay to an integer in milliseconds or to a duration with ms or s, such as 500ms, 2s, or 1.5s. Other requests can proceed while one waits.
Templates
Any string in status, headers, or body, at any depth, can use ${ <jmespath> } over the same
request shape as predicates. Keys are never templated.
A templated status must render an integer from 100 to 599; otherwise the request gets a 500
explaining which Behavior failed. Invalid expressions fail startup, naming the route:case:behavior.
Behaviors without ${ are served as written.
Built-in functions, next to JMESPath's own:
HTTP body Pipelines
Set pipeline on the Route to transform the request body before Case matching and the
rendered response body before sending it. For example, the
typed envelope example
uses the same worker for an HTTP POST and a WebSocket message:
The first worker receives parsed JSON for application/json and +json media types,
UTF-8 text for text/*, or the original bytes for binary content or bodies Decoy cannot parse.
In the example the client posts
application/msgpack. The worker decodes those bytes, so the Case can match on
body.data.ids and the template can read the ID. The output worker encodes the rendered
object as MessagePack bytes. Decoy sends those bytes verbatim with the authored content type.
Cases on a Route share its transformed body and see the original request headers. If another
Route is tried, its Pipeline starts from the original request. A terminal input command's stdout
is parsed as JSON for Case matching and templates if possible. Other byte results appear as
null there. A Passthrough Behavior forwards byte results verbatim and serializes other results
from the winning Route's transformed value. If Decoy serializes that value as JSON, it uses
application/json upstream unless the original media type already identifies JSON.
HTTP workers can also read the original method, path,
and headers as ctx.request in both directions. This does not change which headers Passthrough
forwards. Command Transformers still receive only their payload on stdin.
When a Passthrough Route has output steps, Decoy buffers the upstream body and decodes it
according to its content type before running them. This buffer is limited by maxBodyBytes.
A non-identity Content-Encoding gives the first Transformer bytes, including for JSON
media types. When a Passthrough transform changes the wire bytes, Decoy drops the sender's
Content-Encoding, ETag, and digest headers and sends the new body length. If the bytes
are identical, those headers stay in place. Mock responses keep their authored media type and
encoding. Decoy skips output steps for bodyless upstream responses such as HEAD and 204.
An HTTP 206 Partial Content response can only pass through if the output steps leave its bytes
unchanged; otherwise Decoy returns 500 rather than sending an invalid byte range.
Upstream body read failures are reported as Passthrough forwarding failures, not Transformer errors.
An input worker can call miss to skip its Route. A Transformer error returns HTTP 500.
An input error occurs before Decoy contacts an upstream.
Passthrough Behavior
Forwards the matched request (method, path and query, headers, body) to a real upstream instead of mocking it:
The target is the first one set, in this order: the Behavior's target, the Route's top-level
passthrough: { baseUrl }, decoy serve --passthrough-base-url, then the request's own Host. If
forwarding fails, Decoy answers 502 with {"error":"passthrough forwarding failed", ...}, which
is never confused with a mock miss. Without output transforms the upstream's body streams through as it arrives, so SSE,
long-poll and large downloads work. An upstream that doesn't connect within 10s or sends no response
headers for 60s gets 504; a body that stalls for 60s is cut off. A request that comes back to the
same Decoy (e.g. the request's own Host is Decoy) gets 508 instead of looping: each Decoy adds
its id to x-decoy-via. x-mock-session is not forwarded, so a Decoy further down a chain answers
from its default Session. Every repeated response header (such as set-cookie) comes back. A path
with a . or .. segment, even percent-encoded (%2e%2e, ..%2f) or split by \, gets 502
instead of reaching the upstream, so it can't leave the base path.
Misses are fail-closed
A request no active Case matches gets 501 with x-mock-miss: true and a body that says why each
active route entry, checked bottom-to-top, did not match:
reason is one of method, path (with the Route's expected value), condition (the first
failed Case condition: field, expected, and the request's actual, null when absent; field
is match for a whole-Case predicate, and a predicate that failed to evaluate adds its error),
decode (a gRPC decode failure or an HTTP input Transformer that reported
a miss, with the error), transport, missingRoute, missingCase, or missingBehavior. Decoy never forwards a request unless a Passthrough Behavior is selected.