Skip to main content

gRPC Service Testing

Testing gRPC services typically requires complex tooling, manual compilation of Protocol Buffers (protobufs), and heavy client libraries.

Rumour changes the game by integrating natively with gRPC. It compiles your .proto files in memory on the fly and allows you to test RPC endpoints using simple, readable JSON representations of your protobuf messages.


The Scenario​

We will construct a test that connects to a gRPC EchoService, passes authorization metadata (the gRPC equivalent of HTTP headers), sends a payload, and asserts that the underlying grpc_status is successful.

1. Actionable Setup​

Let's build this from scratch. First, we need a live environment to test against. We will use the official rumour-mock-server repository.

Start the Mock Server​

In a new terminal window, boot up the multi-protocol mock server (which spins up a gRPC EchoService on port 50051):

cd rumour-mock-server/mock-server
npm install
npm start

Scaffold the Workspace​

In your main terminal, initialize a fresh testing workspace and scaffold a new collection:

mkdir grpc_tutorial
cd grpc_tutorial
rumour init
rumour new echo_tests
mkdir protos

Open the newly generated workspace.env.toml and define the target endpoint:

[variables]
grpc_url = "grpc://localhost:50051"

Ensure you have your .proto file available locally. For this example, create ./protos/echo.proto:

syntax = "proto3";
package echo;

service EchoService {
rpc Echo (EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
string message = 1;
int32 value = 2;
}

message EchoResponse {
string echo = 1;
int32 received_value = 2;
}

Now, define your gRPC server target in workspace.env.toml:

[variables]
grpc_url = "grpc://localhost:50051"
grpc_token = "super-secret-token"

Step 1: Testing the RPC Method​

Create your request file: test_grpc_echo.toml.

Notice the protocol = "grpc" directive. This tells Rumour to bypass the HTTP/1.1 engine entirely and establish an HTTP/2 multiplexed channel to invoke the remote procedure call.

name = "grpc_echo_with_metadata"
protocol = "grpc"

[request]
# Note the grpc:// protocol scheme in the URL
url = "{{grpc_url}}"

[grpc]
proto_file = "./protos/echo.proto"
service = "EchoService"
method = "Echo"
deadline_ms = 2000

# Metadata works exactly like HTTP headers
[grpc.metadata]
authorization = "Bearer {{grpc_token}}"
x-trace-id = "rumour-grpc-test"

# The JSON payload will automatically be validated against EchoRequest and binary-encoded!
[grpc.request_json]
message = "Hello from Rumour"
value = 100

[assert]
# 0 indicates OK in the gRPC specification
grpc_status = 0

# You can assert against the deserialized JSON response just like a REST API!
[assert.json]
"echo" = { contains = "Hello from Rumour" }
"received_value" = { equal = 100 }

Let's break down what just happened:​

  1. On-the-fly Compilation: Rumour reads ./protos/echo.proto, finds the EchoService, and locates the Echo method.
  2. Payload Serialization: It takes the JSON object from [grpc.request_json], validates it against the EchoRequest message schema, and packs it into binary Protocol Buffers.
  3. Execution: It opens an HTTP/2 stream, sends the metadata, sends the binary payload, and waits for a response (up to the deadline_ms timeout).
  4. Deserialization & Validation: Upon receiving the binary EchoResponse, Rumour converts it back into JSON so your standard [assert.json] logic can validate the fields!

gRPC Status Codes​

Unlike REST which relies heavily on HTTP 2xx/4xx/5xx status codes, gRPC embeds its own application-level status code in the response trailers (e.g., 0 for OK, 5 for NOT_FOUND, 16 for UNAUTHENTICATED).

If you don't define a grpc_status assertion, Rumour will default to expecting 0 (OK). If you want to test a failure scenario, you must explicitly assert it:

[assert]
# We expect the server to return 16 (UNAUTHENTICATED) if we pass a bad token
grpc_status = 16

The Output Trace​

When you run this file (rumour run test_grpc_echo.toml -v), Rumour provides a clean output abstracting away the binary complexity:

POST grpc://localhost:50051/echo.EchoService/Echo
URL: grpc://localhost:50051
Metadata: authorization: Bearer super-secret-token
Payload: {"message":"Hello from Rumour","value":100}
✓ SUCCESS: test_grpc_echo.toml (18ms)
tip

Always ensure your grpc.service string precisely matches the service name defined in the package of your .proto file (e.g. echo.EchoService vs just EchoService depending on the protobuf loader structure).