diff --git a/source/common/grpc/rpc_channel_impl.cc b/source/common/grpc/rpc_channel_impl.cc index bfe059a18a1e..c53e5ea61047 100644 --- a/source/common/grpc/rpc_channel_impl.cc +++ b/source/common/grpc/rpc_channel_impl.cc @@ -54,7 +54,7 @@ void RpcChannelImpl::onSuccess(Http::MessagePtr&& http_response) { // A gRPC response contains a 5 byte header. Currently we only support unary responses so we // ignore the header. @see serializeBody(). - if (!http_response->body() || !(http_response->body()->length() > 5)) { + if (!http_response->body() || (http_response->body()->length() < 5)) { throw Exception(Optional(), "bad serialized body"); } diff --git a/test/common/grpc/rpc_channel_impl_test.cc b/test/common/grpc/rpc_channel_impl_test.cc index 4de0d07a9b6f..5e7dd426bf2c 100644 --- a/test/common/grpc/rpc_channel_impl_test.cc +++ b/test/common/grpc/rpc_channel_impl_test.cc @@ -212,6 +212,28 @@ TEST_F(GrpcRequestImplTest, ShortBodyInResponse) { http_callbacks_->onSuccess(std::move(response_http_message)); } +TEST_F(GrpcRequestImplTest, EmptyBodyInResponse) { + expectNormalRequest(); + + helloworld::HelloRequest request; + request.set_name("a name"); + helloworld::HelloReply response; + EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_)); + service_.SayHello(nullptr, &request, &response, nullptr); + + Http::MessagePtr response_http_message(new Http::ResponseMessageImpl( + Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{":status", "200"}}})); + helloworld::HelloReply empty_response; + response_http_message->body() = Common::serializeBody(empty_response); + EXPECT_EQ(response_http_message->body()->length(), 5); + response_http_message->trailers( + Http::HeaderMapPtr{new Http::TestHeaderMapImpl{{"grpc-status", "0"}}}); + + EXPECT_CALL(grpc_callbacks_, onSuccess()); + http_callbacks_->onSuccess(std::move(response_http_message)); + EXPECT_EQ(response.SerializeAsString(), empty_response.SerializeAsString()); +} + TEST_F(GrpcRequestImplTest, BadMessageInResponse) { expectNormalRequest();