Skip to content

Commit

Permalink
Add test for splitting buffer into two encode data
Browse files Browse the repository at this point in the history
Signed-off-by: Dhi Aurrahman <dio@rockybars.com>
  • Loading branch information
dio committed Jul 12, 2018
1 parent b45854a commit d93f57e
Showing 1 changed file with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"}};

Expand All @@ -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("<h1>Hello, world!</h1>", 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("<h1>Hello, world!</h1>");

auto response_data = Grpc::Common::serializeBody(response);
EXPECT_EQ(40, response_data->length());

// The response data buffer is splitted into two parts.
std::array<char, 20> 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("<h1>Hello, world!</h1>", response_html);
Expand Down

0 comments on commit d93f57e

Please sign in to comment.