gRPC
Decoy answers unary and server-streaming gRPC calls on its normal port. gRPC over HTTP/2 cleartext (h2c), plain HTTP, and the Control API all share it. Point your gRPC client at Decoy instead of the real service; no generated code or server stubs are needed. Client and bidi streaming are not supported yet. For gRPC over TLS, turn on TLS; the same port then speaks gRPC over HTTPS.
Proto roots
Decoy reads your .proto files at startup. List their import roots in decoy.yaml:
Paths resolve relative to decoy.yaml. A missing root or a proto that doesn't compile stops startup
with the file and the reason.
If a root holds more than you need, such as a monorepo proto/ folder with services that don't
compile together, list the entry files in files. Decoy compiles only those, and resolves their
imports through roots:
Each pattern must match at least one .proto under a root.
gRPC Routes
Collections, Sessions, and the Control API work exactly as for HTTP; addresses are still
route:case:behavior.
The envelope
Cases and templates see one call as:
- By default,
messageis the decoded request as protobuf JSON: camelCase field names, enums as names, 64-bit integers as strings,bytesas base64, and default values included (id: '',state: STATE_UNSPECIFIED). metadatais the request headers with lowercased names.
A Case match takes message (a deep partial match, like HTTP body) and metadata (like
headers, '*' for any value), or ${ } predicates over the whole envelope.
Behaviors
Every string can be a ${ } template, including status.
Server streaming
A server-streaming method takes one request and answers with a stream of messages, each sent
delay after the one before it:
- Each
streamitem carries its data inpayload. - With
status, the call ends after the list:OK, or an error with itsmessagetext andmetadata, as for unary calls. - Without
status, the call stays open after the list. Each Control APIsendto the Route from the call's Session arrives as another response message, until the client cancels. Asendthat comes while the list is still playing arrives after it.{}opens the stream with nothing scripted. Control APIwaitwithconnected: trueresolves once such a stream is open. messageoutside the list is only the text of an errorstatus, andmetadataneeds astatusto be sent with.streamon a unary method fails at startup.delayon the Behavior itself waits before the first message.- The call's request event is logged once, when the stream opens.
Checked at startup
- The service and method exist in the loaded protos.
- Literal response
messageandstream[].payloadvalues fit the method's output type: no unknown fields, valid enum names, scalar types and ranges. A message with no${ }is also parsed as protobuf JSON. - Literal
match.messagevalues fit the input type in the envelope's form above.created_at_msorcreatedAtMs: 5would never match a decoded request, so they fail: use JSON names, and write 64-bit values as strings (createdAtMs: '5'). Well-known types must be written as the request renders them, too: anInt64Valueas'5', aTimestampin UTC as'2024-01-01T00:00:00Z'. The error shows the form to use. - Metadata names are lowercase header names and not
grpc-*. - Literal metadata values are printable ASCII (
0x20–0x7E). A rendered value outside that range is dropped from the trailers with a warning.
For a named Pipeline, Transformers can change the shape of message before matching or
protobuf encoding. Decoy still validates the method and Behavior structure at startup.
A first output worker can check authored literals. The protobuf step checks actual
values against the descriptor when the Pipeline runs.
Templated values are checked when they render. A render or encode failure answers INTERNAL with
the Behavior address. A stream renders its whole list when the call comes in, so a failure there
answers INTERNAL before any message. A send message that doesn't fit the output type is
rejected with 400 and the stream stays open.
Misses and errors
Nothing is forwarded to a real service: gRPC Routes have no Passthrough.
Sessions
Send x-mock-session as call metadata. It's an ordinary gRPC header, so any client can set it:
Then switch that Session's Collection or Behaviors with the Control API over plain HTTP on the same port.
Pipelines
A gRPC Route decodes calls with the default Pipeline [grpc-frame, protobuf]. An explicit
list replaces it. Decoy decodes in list order and encodes in reverse order. Each step can appear once, grpc-frame must come first
(gRPC bodies are always framed); protobuf, if present, follows it.
Without protobuf, the Route sees and answers raw bytes: message is a base64 string.
To add a Transformer, select a named Pipeline with pipeline: approval-ids on the Route.
For example, save this worker next to decoy.yaml. It normalizes IDs before matching and adds a prefix to each reply:
Each direction runs in the order written. Input starts with the framed request bytes.
Here grpc-frame removes the frame, protobuf decodes the request using the method's
input type, and the Case sees the worker's result as message. Output starts with the
rendered reply. The worker changes it, protobuf encodes it using the method's output
type, and grpc-frame adds the frame the client expects. The same output steps apply to
each server-streamed reply and to Control API pushes. A nonempty output list must return wire bytes.
An omitted or empty list uses the default gRPC codec and its startup protobuf validation for that
direction. Nonempty lists run in the authored order, including custom steps between built-ins.
Decoy rejects incompatible adjacent built-ins at startup; a custom step can change the value type. An input command at the end of
the list must emit JSON for Case matching. A worker can return a JSON value or bytes,
which Cases see as a base64 string. A worker can call miss to skip its Route. If no
other Case matches, the client gets UNIMPLEMENTED. A named input step that fails
ends the call with INVALID_ARGUMENT. An authored reply that fails to encode gets INTERNAL.
See examples/grpc-node for a runnable Node client.