Skip to content

Commit

Permalink
test: add config test for HeaderToMetadata filter (#7506)
Browse files Browse the repository at this point in the history
Adds coverage  for source/extensions/filters/http/header_to_metadata_config.cc

Risk Level: Low
Testing: Included
Docs Changes: N/A
Release Notes: N/A

Signed-off-by: Derek Argueta <dereka@pinterest.com>
  • Loading branch information
derekargueta authored and zuercher committed Jul 10, 2019
1 parent e3973ec commit 79b071c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/extensions/filters/http/header_to_metadata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ envoy_extension_cc_test(
"//test/mocks/server:server_mocks",
],
)

envoy_extension_cc_test(
name = "config_test",
srcs = ["config_test.cc"],
extension_name = "envoy.filters.http.header_to_metadata",
deps = [
"//source/extensions/filters/http/header_to_metadata:config",
"//test/mocks/server:server_mocks",
"//test/test_common:utility_lib",
],
)
74 changes: 74 additions & 0 deletions test/extensions/filters/http/header_to_metadata/config_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <string>

#include "envoy/config/filter/http/header_to_metadata/v2/header_to_metadata.pb.validate.h"

#include "extensions/filters/http/header_to_metadata/config.h"

#include "test/mocks/server/mocks.h"
#include "test/test_common/utility.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace Envoy {
namespace Extensions {
namespace HttpFilters {
namespace HeaderToMetadataFilter {

using HeaderToMetadataProtoConfig = envoy::config::filter::http::header_to_metadata::v2::Config;

TEST(HeaderToMetadataFilterConfigTest, InvalidEmptyHeader) {
const std::string yaml = R"EOF(
request_rules:
- header: ""
)EOF";

HeaderToMetadataProtoConfig proto_config;
EXPECT_THROW(TestUtility::loadFromYamlAndValidate(yaml, proto_config), ProtoValidationException);
}

TEST(HeaderToMetadataFilterConfigTest, InvalidEmptyKey) {
const std::string yaml = R"EOF(
request_rules:
- header: x-version
on_header_present:
metadata_namespace: envoy.lb
key: ""
type: STRING
)EOF";

HeaderToMetadataProtoConfig proto_config;
EXPECT_THROW(TestUtility::loadFromYamlAndValidate(yaml, proto_config), ProtoValidationException);
}

TEST(HeaderToMetadataFilterConfigTest, SimpleConfig) {
const std::string yaml = R"EOF(
request_rules:
- header: x-version
on_header_present:
metadata_namespace: envoy.lb
key: version
type: STRING
on_header_missing:
metadata_namespace: envoy.lb
key: default
value: 'true'
type: STRING
)EOF";

HeaderToMetadataProtoConfig proto_config;
TestUtility::loadFromYamlAndValidate(yaml, proto_config);

testing::NiceMock<Server::Configuration::MockFactoryContext> context;
HeaderToMetadataConfig factory;

Http::FilterFactoryCb cb = factory.createFilterFactoryFromProto(proto_config, "stats", context);
Http::MockFilterChainFactoryCallbacks filter_callbacks;
EXPECT_CALL(filter_callbacks, addStreamFilter(_));
cb(filter_callbacks);
}

} // namespace HeaderToMetadataFilter
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy

0 comments on commit 79b071c

Please sign in to comment.