After writing my first sample gRPC application, I was trying to find different ways of testing it.
So with little bit of search I was able to find some ways of testing gRPC service and in this tutorial, I am going to explain those.
In principle, I have found following ways:
Writing your own gRPC Client
Using gRPCurl and gRPCui
Using Swagger ui
First let’s create a sample gRPC in .Net
Open Visual studio and create gRPC service app.
My gRPC server is ready. Let’s test it.
Testing with gRPC Client
As I mentioned in the beginning first way is to write a own gRPC client. This method is well explained in Microsoft documents. so I am not going to repeat. Take a look at the document. https://docs.microsoft.com/en-us/aspnet/core/grpc/httpapi?view=aspnetcore-5.0
Test Using gRPCurl and gRPCui
I found some tools which can be used for testing gRPC.
gRPCurl
gRPCui
How to use these tools. It is nicely explained in the given document.
This was great..
But one problem that I had to installed gRPCui on my pc and being a container enthusiast, I try to avoid installing it on my pc but rather I tried to search if there is a docker image of gRPCui.
gRPCui In Docker
After a little search I found these two images.
https://hub.docker.com/u/fullstorydev
https://hub.docker.com/r/wongnai/grpcui
First one is official image from the grpcui developer and other one is wrapper for grpcui. So the next step is try them.
I have used the second one for my testing.
Look at the docker run cmd for starting a container.
docker run -eGRPCUI_SERVER=172.17.0.1:5000 -p8091:8080 wongnai/grpcui
If you notice that I have to provide connection address of my gRPC Server. and which seems to be address inside the container.
Now if my gRPC server is also running as container then it would be easy as I can ran both of them in same network and create a docker compose file like below.
Docker compose for gRPCui and gRPC Service
docker-compose up -d
Running containers
Let’s run and see.
gRPC demo in Containers
Note: make sure you start the container after your gRPC server is running.
Great..! that worked just fine.
Now if my gRPC server is not in container but running on localhost then how it is going to work.
Remove the above created containers.
docker-compose down
Now let’s create container with following command and test our grpc server.
docker run -eGRPCUI_SERVER=host.docker.internal:5000 -p8091:8080 wongnai/grpcui:master
Note: I had docker running on windows pc , so this worked for me. for Linux it would be different.
gRPC demo in for localhost
So in the end, I was able to test with gRPCui running in container and gRPC service running on localhost.
Using Swagger ui
When I wanted to test my gRPC service, my instinct was to test with swagger like normal webapi but it did not worked, so I did a little search and found that swagger for gRPC doesn’t work with simple swagger integration. This is still in experiment and also require some of the changes in production code.
Summary
Testing gRPC could be challenging specially when you have to write your own client but with gRPCui it is very easy and very quickly you can test your gRPC server in development environment.
Source: Medium - Nitesh Singhal
The Tech Platform
Comments