Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grpc transcoding content type not matching response body #8312

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,17 @@ Http::FilterHeadersStatus JsonTranscoderFilter::encodeHeaders(Http::HeaderMap& h
response_headers_ = &headers;

if (end_stream) {

if (method_->server_streaming()) {
// When there is no body in a streaming response, a empty JSON array is
// returned by default. Set the content type correctly.
headers.insertContentType().value().setReference(Http::Headers::get().ContentTypeValues.Json);
}

// In gRPC wire protocol, headers frame with end_stream is a trailers-only response.
// The return value from encodeTrailers is ignored since it is always continue.
encodeTrailers(headers);

return Http::FilterHeadersStatus::Continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ TEST_P(GrpcJsonTranscoderIntegrationTest, BindingAndBody) {

TEST_P(GrpcJsonTranscoderIntegrationTest, ServerStreamingGet) {
HttpIntegrationTest::initialize();

// 1: Normal streaming get
testTranscoding<bookstore::ListBooksRequest, bookstore::Book>(
Http::TestHeaderMapImpl{
{":method", "GET"}, {":path", "/shelves/1/books"}, {":authority", "host"}},
Expand All @@ -439,6 +441,22 @@ TEST_P(GrpcJsonTranscoderIntegrationTest, ServerStreamingGet) {
Status(), Http::TestHeaderMapImpl{{":status", "200"}, {"content-type", "application/json"}},
R"([{"id":"1","author":"Neal Stephenson","title":"Readme"})"
R"(,{"id":"2","author":"George R.R. Martin","title":"A Game of Thrones"}])");

// 2: Empty response (trailers only) from streaming backend.
// Response type is a valid JSON, so content type should be application/json.
// Regression test for github.com/envoyproxy/envoy#5011
testTranscoding<bookstore::ListBooksRequest, bookstore::Book>(
Http::TestHeaderMapImpl{
{":method", "GET"}, {":path", "/shelves/2/books"}, {":authority", "host"}},
"", {"shelf: 2"}, {}, Status(),
Http::TestHeaderMapImpl{{":status", "200"}, {"content-type", "application/json"}}, "[]");

// 3: Empty response (trailers only) from streaming backend, with a gRPC error.
testTranscoding<bookstore::ListBooksRequest, bookstore::Book>(
Http::TestHeaderMapImpl{
{":method", "GET"}, {":path", "/shelves/37/books"}, {":authority", "host"}},
"", {"shelf: 37"}, {}, Status(Code::NOT_FOUND, "Shelf 37 not found"),
Http::TestHeaderMapImpl{{":status", "200"}, {"content-type", "application/json"}}, "[]");
}

TEST_P(GrpcJsonTranscoderIntegrationTest, StreamingPost) {
Expand Down Expand Up @@ -553,6 +571,7 @@ TEST_P(GrpcJsonTranscoderIntegrationTest, DeepStruct) {

// The valid deep struct is parsed successfully.
// Since we didn't set the response, it return 503.
// Response body is empty (not a valid JSON), so content type should be application/grpc.
testTranscoding<bookstore::EchoStructReqResp, bookstore::EchoStructReqResp>(
Http::TestHeaderMapImpl{
{":method", "POST"}, {":path", "/echoStruct"}, {":authority", "host"}},
Expand Down