From d93f57e0a9d365582d1cde1d256c528af59a21c0 Mon Sep 17 00:00:00 2001 From: Dhi Aurrahman Date: Fri, 13 Jul 2018 02:11:24 +0700 Subject: [PATCH] Add test for splitting buffer into two encode data Signed-off-by: Dhi Aurrahman --- .../json_transcoder_filter_test.cc | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) 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 61cfb9e4de51..eb183b8254e2 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 @@ -561,10 +561,6 @@ TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryWithHttpBodyAsOutput) { EXPECT_EQ("/bookstore.Bookstore/GetIndex", request_headers.get_(":path")); EXPECT_EQ("trailers", request_headers.get_("te")); - Http::TestHeaderMapImpl continue_headers{{":status", "000"}}; - EXPECT_EQ(Http::FilterHeadersStatus::Continue, - filter_.encode100ContinueHeaders(continue_headers)); - Http::TestHeaderMapImpl response_headers{{"content-type", "application/grpc"}, {":status", "200"}}; @@ -581,7 +577,54 @@ TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryWithHttpBodyAsOutput) { EXPECT_EQ(Http::FilterDataStatus::StopIterationAndBuffer, filter_.encodeData(*response_data, false)); - std::string response_html = response_data->toString(); + const std::string response_html = response_data->toString(); + + EXPECT_EQ("text/html", response_headers.get_("content-type")); + EXPECT_EQ("

Hello, world!

", response_html); + + Http::TestHeaderMapImpl response_trailers{{"grpc-status", "0"}, {"grpc-message", ""}}; + + EXPECT_EQ(Http::FilterTrailersStatus::Continue, filter_.decodeTrailers(response_trailers)); +} + +TEST_F(GrpcJsonTranscoderFilterTest, TranscodingUnaryWithHttpBodyAsOutputAndSplitTwoEncodeData) { + Http::TestHeaderMapImpl request_headers{{":method", "GET"}, {":path", "/index"}}; + + EXPECT_CALL(decoder_callbacks_, clearRouteCache()); + + EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_.decodeHeaders(request_headers, false)); + EXPECT_EQ("application/grpc", request_headers.get_("content-type")); + EXPECT_EQ("/index", request_headers.get_("x-envoy-original-path")); + EXPECT_EQ("/bookstore.Bookstore/GetIndex", request_headers.get_(":path")); + EXPECT_EQ("trailers", request_headers.get_("te")); + + Http::TestHeaderMapImpl response_headers{{"content-type", "application/grpc"}, + {":status", "200"}}; + + EXPECT_EQ(Http::FilterHeadersStatus::StopIteration, + filter_.encodeHeaders(response_headers, false)); + EXPECT_EQ("application/json", response_headers.get_("content-type")); + + google::api::HttpBody response; + response.set_content_type("text/html"); + response.set_data("

Hello, world!

"); + + auto response_data = Grpc::Common::serializeBody(response); + EXPECT_EQ(40, response_data->length()); + + // The response data buffer is splitted into two parts. + std::array out; + Buffer::OwnedImpl part1; + response_data->copyOut(0, 20, out.data()); + part1.add(std::string(out.begin(), out.end())); + EXPECT_EQ(Http::FilterDataStatus::StopIterationAndBuffer, filter_.encodeData(part1, false)); + + Buffer::OwnedImpl part2; + response_data->copyOut(20, 20, out.data()); + part2.add(std::string(out.begin(), out.end())); + EXPECT_EQ(Http::FilterDataStatus::StopIterationAndBuffer, filter_.encodeData(part2, false)); + + const std::string response_html = part2.toString(); EXPECT_EQ("text/html", response_headers.get_("content-type")); EXPECT_EQ("

Hello, world!

", response_html);