A Summary of Live Coding a Simple CLI for gRPC Testing at Go Conference 2023
Hello, I am Faustin of Money Forward. If you visited our sponsor booth at Go Conference 2023, then you may remember that Andy live coded grpcake
, a command-line client for testing gRPC services during development. If you couldn't make it to the session, or if you just want a quick recap, I will tell you about it in this post.
Motivation Behind grpcake
As a CLI tool, you can use grpcake
from the embedded terminal of that IDE you love so much, eliminating the need for those disruptive switches to a different GUI. Written in Golang, it also offers seamless integration with gRPC projects also written in Go. The difference with other gRPC clients written in Golang is its user interface.
The Basics of grpcake
Grpcake
is designed to make the process of testing gRPC services from the command line simple. Instead of having to pass JSON strings, grpcake
reads parameters as regular command line arguments.
Let's consider an example from the gRPC go quickstart, the Greeter
Service:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
For the server to support reflection, we modify the code by importing google.golang.org/grpc/reflection
and adding reflection.Register(s)
in the function main
. After launching the server, we're ready to test the Greeter
service using grpcake
.
grpcake --url localhost:50051 --grpc-method helloworld.Greeter/SayHello name="Go Con 2023"
The response is something like this:
2023/06/19 08:42:26 request json body: {"name":"Go Con 2023"}
2023/06/19 08:42:26 Response: {
"message": "Hello Go Con 2023"
}
Grpcake also supports raw JSON arguments for request parameters, so you can pass in complex JSON objects as parameters by using := instead of =. Consider a hypothetical gRPC service Person, with method information taking a parameter person shaped as:
{
"person": {
"name": string,
"age": number,
"address": {
"city": string,
"prefecture": string,
"zip": number
}
}
}
To test this service, you can run a command like this
grpcake --url SERVER_URL --grpc-method Person/information person.address:={"prefecture":"Kanagawa"} person.age:=2023