From aaaee774bf3ac4f72258ae521e87790ede296c58 Mon Sep 17 00:00:00 2001 From: Rei Shimizu Date: Sat, 6 Mar 2021 03:04:41 +0900 Subject: [PATCH] json transcoder: set decoded percent-encoded message as json response (#15276) Signed-off-by: Shikugawa --- .../json_transcoder_filter.cc | 3 ++- .../json_transcoder_filter_test.cc | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc b/source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc index c51f35a9cd54..8f6c6a3eec9c 100644 --- a/source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc +++ b/source/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter.cc @@ -850,7 +850,8 @@ bool JsonTranscoderFilter::maybeConvertGrpcStatus(Grpc::Status::GrpcStatus grpc_ auto grpc_message_header = trailers.GrpcMessage(); if (grpc_message_header) { auto message = grpc_message_header->value().getStringView(); - status_details->set_message(message.data(), message.size()); + auto decoded_message = Http::Utility::PercentEncoding::decode(message); + status_details->set_message(decoded_message.data(), decoded_message.size()); } } diff --git a/test/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter_test.cc b/test/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter_test.cc index bc3ad9d3a5af..44782705d731 100644 --- a/test/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter_test.cc +++ b/test/extensions/filters/http/grpc_json_transcoder/json_transcoder_filter_test.cc @@ -1224,6 +1224,25 @@ TEST_F(GrpcJsonTranscoderFilterConvertGrpcStatusTest, EXPECT_FALSE(response_headers.has("grpc-status-details-bin")); } +TEST_F(GrpcJsonTranscoderFilterConvertGrpcStatusTest, + TranscodingPercentEncodedTextHeadersInTrailerOnlyResponse) { + const std::string expected_response(R"({"code":5,"message":"Resource not found"})"); + EXPECT_CALL(encoder_callbacks_, addEncodedData(_, false)) + .WillOnce(Invoke([&expected_response](Buffer::Instance& data, bool) { + EXPECT_EQ(expected_response, data.toString()); + })); + + Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}, + {"content-type", "application/grpc"}, + {"grpc-status", "5"}, + {"grpc-message", "Resource%20not%20found"}}; + EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.encodeHeaders(response_headers, true)); + EXPECT_EQ("404", response_headers.get_(":status")); + EXPECT_EQ("application/json", response_headers.get_("content-type")); + EXPECT_FALSE(response_headers.has("grpc-status")); + EXPECT_FALSE(response_headers.has("grpc-message")); +} + // Trailer-only response with grpc-status-details-bin header with details. // Also tests that a user-defined type from a proto descriptor in config can be used in details. TEST_F(GrpcJsonTranscoderFilterConvertGrpcStatusTest,