Skip to content

gRPC

Mockifyr serves gRPC from the same stubs you already write. There is no separate gRPC stub format: the service and method become a URL path, the request message is matched as proto3 JSON, and the response message is a jsonBody.

Turning it on

gRPC serving turns on automatically when compiled protobuf descriptor sets are present at <root-dir>/grpc/*.dsc. There is no flag. If the directory holds no .dsc file, nothing is served and the rest of Mockifyr behaves as usual.

Terminal window
protoc --descriptor_set_in= \
--include_imports \
--descriptor_set_out=./mockifyr-root/grpc/orders.dsc \
orders.proto

--root-dir is the same root used for stub files and __files — see persistence.

Writing a gRPC stub

The stub is an ordinary mapping. urlPath is /{package.Service}/{Method}, the body pattern matches the request message as proto3 JSON, and the response is a jsonBody.

{
"request": {
"method": "POST",
"urlPath": "/orders.OrderService/GetOrder",
"bodyPatterns": [ { "equalToJson": { "orderId": "A-1" } } ]
},
"response": {
"status": 200,
"jsonBody": {
"orderId": "A-1",
"state": "SHIPPED",
"totalCents": "19900"
}
}
}

Because it is a normal stub, everything else that applies to stubs applies here: request matching, templating, scenarios, delays and faults.

Codec coverage

The protobuf ↔ JSON codec covers:

FeatureRendered as
proto3 scalarsJSON numbers / booleans
stringJSON string
bytesbase64 string
Nested messagesNested JSON objects
EnumsThe enum value name, not its number
MapsJSON objects
Repeated fieldsJSON arrays (packed and unpacked wire forms)
oneofThe set field only
Well-known wrappers (StringValue, Int32Value, …)Bare scalars, not {"value": …}

Returning an error status

To return a gRPC error instead of a message, set two response headers:

HeaderMeaning
grpc-status-nameThe status code name, e.g. NOT_FOUND
grpc-status-reasonThe detail message
{
"request": {
"method": "POST",
"urlPath": "/orders.OrderService/GetOrder",
"bodyPatterns": [ { "matchesJsonPath": "$.orderId" } ]
},
"response": {
"status": 200,
"headers": {
"grpc-status-name": "NOT_FOUND",
"grpc-status-reason": "no such order"
}
}
}

Streaming

PatternSupported
UnaryYes
Server-streaming, single messageYes
Client-streaming, single messageYes
Multi-message streamsNo
Bidirectional streamingNo

The unsupported cases are not an oversight of scope. The WireMock gRPC extension does not implement them either, so there is no reference implementation to differentially test Mockifyr against — and Mockifyr does not ship behaviour it cannot verify against the oracle.

Not supported

  • Multi-message and bidirectional streaming (above).
  • A gRPC-specific admin reset. Use the ordinary stub admin routes to clear mappings.

Tenants

Tenancy works exactly as elsewhere: the X-Mockifyr-Tenant header selects the tenant, and the gRPC middleware honours it. See multi-tenancy.