Skip to content

Commit

Permalink
json transcoder: set decoded percent-encoded message as json response (
Browse files Browse the repository at this point in the history
…#15276)

Signed-off-by: Shikugawa <[email protected]>
  • Loading branch information
Shikugawa authored Mar 5, 2021
1 parent a1cff0e commit aaaee77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit aaaee77

Please sign in to comment.