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

grpc_json_transcoder: Switch tests to compare JSON objects #16085

Merged
merged 1 commit into from
Apr 20, 2021
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 @@ -163,8 +163,15 @@ class GrpcJsonTranscoderIntegrationTest
return Http::HeaderMap::Iterate::Continue;
});
if (!expected_response_body.empty()) {
if (full_response) {
EXPECT_EQ(expected_response_body, response->body());
const bool isJsonResponse = response->headers().getContentTypeValue() == "application/json";
if (full_response && isJsonResponse) {
const bool isStreamingResponse = response->body()[0] == '[';
EXPECT_TRUE(TestUtility::jsonStringEqual(response->body(), expected_response_body,
isStreamingResponse))
<< "Response mismatch. \nGot : " << response->body()
<< "\nWant: " << expected_response_body;
} else if (full_response) {
EXPECT_EQ(response->body(), expected_response_body);
} else {
EXPECT_TRUE(absl::StartsWith(response->body(), expected_response_body));
}
Expand Down Expand Up @@ -1191,7 +1198,7 @@ TEST_P(GrpcJsonTranscoderIntegrationTest, ServerStreamingGetExceedsBufferLimit)
Status(),
Http::TestResponseHeaderMapImpl{{":status", "200"}, {"content-type", "application/json"}},
// Incomplete response, not valid JSON.
R"([{"id":"1","author":"Neal Stephenson","title":"Readme"})", true, false, "", true,
R"([{"id":"1","author":"Neal Stephenson","title":"Readme"})", false, false, "", true,
/*expect_response_complete=*/false);
}

Expand Down
19 changes: 17 additions & 2 deletions test/test_common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,16 @@ class TestUtility {
*
* @param lhs JSON string on LHS.
* @param rhs JSON string on RHS.
* @param support_root_array Whether to support parsing JSON arrays.
* @return bool indicating whether the JSON strings are equal.
*/
static bool jsonStringEqual(const std::string& lhs, const std::string& rhs) {
return protoEqual(jsonToStruct(lhs), jsonToStruct(rhs));
static bool jsonStringEqual(const std::string& lhs, const std::string& rhs,
bool support_root_array = false) {
if (!support_root_array) {
return protoEqual(jsonToStruct(lhs), jsonToStruct(rhs));
}

return protoEqual(jsonArrayToStruct(lhs), jsonArrayToStruct(rhs));
}

/**
Expand Down Expand Up @@ -640,6 +646,15 @@ class TestUtility {
return message;
}

static ProtobufWkt::Struct jsonArrayToStruct(const std::string& json) {
// Hacky: add a surrounding root message, allowing JSON to be parsed into a struct.
std::string root_message = absl::StrCat("{ \"testOnlyArrayRoot\": ", json, "}");

ProtobufWkt::Struct message;
MessageUtil::loadFromJson(root_message, message);
return message;
}

/**
* Extract the Protobuf binary format of a google.protobuf.Message as a string.
* @param message message of type type.googleapis.com/google.protobuf.Message.
Expand Down
12 changes: 12 additions & 0 deletions test/test_common/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,16 @@ TEST(BuffersEqual, NonAligned) {
EXPECT_TRUE(TestUtility::buffersEqual(buffer1, buffer2));
}

TEST(JsonEquals, RootMessage) {
EXPECT_TRUE(TestUtility::jsonStringEqual(R"({ "a" : "b" })", R"({ "a" : "b" })"));
EXPECT_TRUE(TestUtility::jsonStringEqual(R"({ "a" : "b" })", R"({"a":"b"})"));
}

TEST(JsonEquals, RootArray) {
EXPECT_TRUE(TestUtility::jsonStringEqual(R"([{ "a" : "b" }, { "c" : "d" }])",
R"([{ "a" : "b" }, { "c" : "d" }])", true));
EXPECT_TRUE(TestUtility::jsonStringEqual(R"([{ "a" : "b" }, { "c" : "d" }])",
R"([{"a":"b"},{"c":"d"}])", true));
}

} // namespace Envoy