Skip to content

Commit

Permalink
Restructure nested formatting tests
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Kaminski <itamark@google.com>
  • Loading branch information
itamarkam committed Jan 13, 2021
1 parent 635d6f0 commit 9dd6649
Showing 1 changed file with 127 additions and 27 deletions.
154 changes: 127 additions & 27 deletions test/common/formatter/substitution_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ TEST(SubstitutionFormatterTest, StructFormatterPlainStringTest) {
expected_json_map);
}

TEST(SubstitutionFormatterTest, StructFormatterNestedObject) {
TEST(SubstitutionFormatterTest, StructFormatterTypesTest) {
StreamInfo::MockStreamInfo stream_info;
Http::TestRequestHeaderMapImpl request_header;
Http::TestResponseHeaderMapImpl response_header;
Expand All @@ -1638,31 +1638,35 @@ TEST(SubstitutionFormatterTest, StructFormatterNestedObject) {

ProtobufWkt::Struct key_mapping;
TestUtility::loadFromYaml(R"EOF(
level_one:
level_two:
level_three:
plain_string: plain_string_value
protocol: '%PROTOCOL%'
string_type: plain_string_value
struct_type:
plain_string: plain_string_value
protocol: '%PROTOCOL%'
list_type:
- plain_string_value
- '%PROTOCOL%'
)EOF",
key_mapping);
StructFormatter formatter(key_mapping, false, false);

const ProtobufWkt::Struct expected = TestUtility::jsonToStruct(R"EOF({
"level_one": {
"level_two": {
"level_three": {
"plain_string": "plain_string_value",
"protocol": "HTTP/1.1"
}
}
}
"string_type": "plain_string_value",
"struct_type": {
"plain_string": "plain_string_value",
"protocol": "HTTP/1.1"
},
"list_type": [
"plain_string_value",
"HTTP/1.1"
]
})EOF");
const ProtobufWkt::Struct out_struct =
formatter.format(request_header, response_header, response_trailer, stream_info, body);
EXPECT_TRUE(TestUtility::protoEqual(out_struct, expected));
}

TEST(SubstitutionFormatterTest, StructFormatterList) {
// Test that nested values are formatted properly, including inter-type nesting.
TEST(SubstitutionFormatterTest, StructFormatterNestedObjectsTest) {
StreamInfo::MockStreamInfo stream_info;
Http::TestRequestHeaderMapImpl request_header;
Http::TestResponseHeaderMapImpl response_header;
Expand All @@ -1675,19 +1679,110 @@ TEST(SubstitutionFormatterTest, StructFormatterList) {
EXPECT_CALL(stream_info, protocol()).WillRepeatedly(Return(protocol));

ProtobufWkt::Struct key_mapping;
// For both struct and list, we test 3 nesting levels of all types (string, struct and list).
TestUtility::loadFromYaml(R"EOF(
struct:
struct_string: plain_string_value
struct_protocol: '%PROTOCOL%'
struct_struct:
struct_struct_string: plain_string_value
struct_struct_protocol: '%PROTOCOL%'
struct_struct_struct:
struct_struct_struct_string: plain_string_value
struct_struct_struct_protocol: '%PROTOCOL%'
struct_struct_list:
- struct_struct_list_string
- '%PROTOCOL%'
struct_list:
- struct_list_string
- '%PROTOCOL%'
# struct_list_struct
- struct_list_struct_string: plain_string_value
struct_list_struct_protocol: '%PROTOCOL%'
# struct_list_list
- - struct_list_list_string
- '%PROTOCOL%'
list:
- plain_string: plain_string_value
- protocol: '%PROTOCOL%'
- list_string
- '%PROTOCOL%'
# list_struct
- list_struct_string: plain_string_value
list_struct_protocol: '%PROTOCOL%'
list_struct_struct:
list_struct_struct_string: plain_string_value
list_struct_struct_protocol: '%PROTOCOL%'
list_struct_list:
- list_struct_list_string
- '%PROTOCOL%'
# list_list
- - list_list_string
- '%PROTOCOL%'
# list_list_struct
- list_list_struct_string: plain_string_value
list_list_struct_protocol: '%PROTOCOL%'
# list_list_list
- - list_list_list_string
- '%PROTOCOL%'
)EOF",
key_mapping);
StructFormatter formatter(key_mapping, false, false);

const ProtobufWkt::Struct expected = TestUtility::jsonToStruct(R"EOF({
"struct": {
"struct_string": "plain_string_value",
"struct_protocol": "HTTP/1.1",
"struct_struct": {
"struct_struct_string": "plain_string_value",
"struct_struct_protocol": "HTTP/1.1",
"struct_struct_struct": {
"struct_struct_struct_string": "plain_string_value",
"struct_struct_struct_protocol": "HTTP/1.1",
},
"struct_struct_list": [
"struct_struct_list_string",
"HTTP/1.1",
],
},
"struct_list": [
"struct_list_string",
"HTTP/1.1",
{
"struct_list_struct_string": "plain_string_value",
"struct_list_struct_protocol": "HTTP/1.1",
},
[
"struct_list_list_string",
"HTTP/1.1",
],
],
},
"list": [
{ "plain_string": "plain_string_value" },
{ "protocol": "HTTP/1.1" },
]
"list_string",
"HTTP/1.1",
{
"list_struct_string": "plain_string_value",
"list_struct_protocol": "HTTP/1.1",
"list_struct_struct": {
"list_struct_struct_string": "plain_string_value",
"list_struct_struct_protocol": "HTTP/1.1",
},
"list_struct_list": [
"list_struct_list_string",
"HTTP/1.1",
]
},
[
"list_list_string",
"HTTP/1.1",
{
"list_list_struct_string": "plain_string_value",
"list_list_struct_protocol": "HTTP/1.1",
},
[
"list_list_list_string",
"HTTP/1.1",
],
],
],
})EOF");
const ProtobufWkt::Struct out_struct =
formatter.format(request_header, response_header, response_trailer, stream_info, body);
Expand Down Expand Up @@ -1768,10 +1863,14 @@ TEST(SubstitutionFormatterTest, StructFormatterAlternateHeaderTest) {

ProtobufWkt::Struct key_mapping;
TestUtility::loadFromYaml(R"EOF(
request_present_header_or_request_absent_header: '%REQ(request_present_header?request_absent_header)%'
request_absent_header_or_request_present_header: '%REQ(request_absent_header?request_present_header)%'
response_absent_header_or_response_absent_header: '%RESP(response_absent_header?response_present_header)%'
response_present_header_or_response_absent_header: '%RESP(response_present_header?response_absent_header)%'
request_present_header_or_request_absent_header:
'%REQ(request_present_header?request_absent_header)%'
request_absent_header_or_request_present_header:
'%REQ(request_absent_header?request_present_header)%'
response_absent_header_or_response_absent_header:
'%RESP(response_absent_header?response_present_header)%'
response_present_header_or_response_absent_header:
'%RESP(response_present_header?response_absent_header)%'
)EOF",
key_mapping);
StructFormatter formatter(key_mapping, false, false);
Expand Down Expand Up @@ -1932,7 +2031,7 @@ TEST(SubstitutionFormatterTest, StructFormatterTypedFilterStateTest) {
fields.at("test_obj").struct_value().fields().at("inner_key").string_value());
}

// Test new specifier (PLAIN/TYPED) of FilterState. Ensure that after adding additional specifier,
// Test new specifier (PLAIN/TYPED) of FilterState. Ensure that after adding additiona specifier,
// the FilterState can call the serializeAsProto or serializeAsString methods correctly.
TEST(SubstitutionFormatterTest, FilterStateSpeciferTest) {
Http::TestRequestHeaderMapImpl request_headers;
Expand Down Expand Up @@ -2062,7 +2161,8 @@ TEST(SubstitutionFormatterTest, StructFormatterMultiTokenTest) {

ProtobufWkt::Struct key_mapping;
TestUtility::loadFromYaml(R"EOF(
multi_token_field: '%PROTOCOL% plainstring %REQ(some_request_header)% %RESP(some_response_header)%'
multi_token_field: '%PROTOCOL% plainstring %REQ(some_request_header)%
%RESP(some_response_header)%'
)EOF",
key_mapping);
for (const bool preserve_types : {false, true}) {
Expand Down

0 comments on commit 9dd6649

Please sign in to comment.