WireMock.Net: Using google.protobuf.Empty as response results in a bad gRPC response

When writing a response builder with google.protobuf.Empty the mock gRPC client will return StatusCode=Unimplemented.

Expected behavior:

It should be possible to use googles ‘WellKnown’ types as the protobuf body in the response builder.

Test to reproduce

  1. Proto file greet.proto
syntax = "proto3"

import "google/protobuf/empty.proto";

package organization.greet.api.v1;

message HelloRequest {
  string name = 1;
}

service Greeter {
  rpc SayHello (HelloRequest) returns (google.protobuf.Empty);
}
  1. The test
[Fact]
public async Task SayHello_ShouldReturnEmptyResponse_WhenInvoked()
{
  using var server = WireMockServer.Start(new WireMockServerSettings
  {
    Urls = ["grpcs://localhost:5000"],
    UseHttp2 = true
  });

  var protoFile = await File.ReadAllTextAsync(@"C:\projects\GrpcIntegrationTests\GrpcIntegrationTests\Protos\greet.proto");

  var protoDefinitionid = "GrpcGreet";

  server.AddProtoDefinition(protoDefinitionId, protoFile)
    .Given(Request.Create()
      .UsingPost()
      .WithHttpVersion("2")
      .WithPath("/organization.greet.api.v1.Greeter/SayHello")
      .WithBodyAsProtoBud("organization.greet.api.v1.HelloRequest"))
    .WithProtoDefinition(protoDefinitionId)
    .RespondWith(Response.Create()
      .WithHeader("Content-Type", "application/grpc")
      .WithTrailingHeader("grpc-status", "0")
      .WithBodyAsProtoBuf("google.protobuf.Empty", new {})
      .WithTransformer());

  var channel = GrpcChannel.ForAddress(server.Url!);

  var client = new Greeter.GreeterClient(channel);

  var reply = await client.SayHelloAsync(new HelloRequest { Name = "Test" });

  reply.Should.BeOfType<Empty>();

  server.Stop();
}

I have tried with and without the .WithTransformer(), I have also tried with new Empty() new Empty {} and new() but none of those match.

Other related info

I am able to get the tests to work if the response is a type that I create in my proto, for example lets say that the HelloReply is in the proto file with a message HelloReply { string reply_message = 1; }

.WithBodyAsProtoBuf("organization.greet.api.v1.HelloReply", new { reply_message = "Hello" })

I have also tried creating my own message EmptyResponse { } and this also did not work

.WithBodyAsProtoBuf("organization.greet.api.v1.EmptyResponse", new {})

It only works when the response contains something like

I have also tried creating my own message EmptyResponse { string status = 1; }

.WithBodyAsProtoBuf("organization.greet.api.v1.EmptyResponse", new { status = "accepted" })

Please can someone either point me in the direction of what I am doing wrong to match an empty response, or confirm that this is a bug

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 50

Most upvoted comments

@StefH It works now. Thank you!

@StefH It is working as expected now! Thank you🙏

Yes, I think I made a mistake in that Value or Struct. I’ll take a look.

I will have to provide a minimal reproduction solution when I have time, I’ve tried a few different things and for .Empty I always get the unable to deserialize response, and for duration/timestamp I’m getting the Unimplemented error.

I’ll provide a link to the repository and throw something together by the end of the week

@ArcaneTSGK Can you please test preview 1.5.62-ci-19067 , this version supports:

  • google.protobuf.Empty
  • google.protobuf.Duration
  • google.protobuf.Timestamp
  • google/protobuf/wrappers
  • google/protobuf/any
  • google/protobuf/struct
  • Enum
  • oneof

I will have some time tomorrow to take a look, I’ll let you know my results, thanks @StefH