|
| 1 | +#include "envoy/extensions/internal_redirect/allow_listed_routes/v3/allow_listed_routes_config.pb.h" |
| 2 | +#include "envoy/registry/registry.h" |
| 3 | +#include "envoy/router/internal_redirect.h" |
| 4 | + |
| 5 | +#include "source/common/stream_info/filter_state_impl.h" |
| 6 | +#include "source/extensions/internal_redirect/allow_listed_routes/allow_listed_routes.h" |
| 7 | +#include "source/extensions/internal_redirect/allow_listed_routes/config.h" |
| 8 | + |
| 9 | +#include "gmock/gmock.h" |
| 10 | +#include "gtest/gtest.h" |
| 11 | + |
| 12 | +using namespace testing; |
| 13 | + |
| 14 | +namespace Envoy { |
| 15 | +namespace Extensions { |
| 16 | +namespace InternalRedirect { |
| 17 | +namespace { |
| 18 | + |
| 19 | +class AllowListedRoutesTest : public testing::Test { |
| 20 | +protected: |
| 21 | + AllowListedRoutesTest() : filter_state_(StreamInfo::FilterState::LifeSpan::FilterChain) { |
| 22 | + factory_ = Registry::FactoryRegistry<Router::InternalRedirectPredicateFactory>::getFactory( |
| 23 | + "envoy.internal_redirect_predicates.allow_listed_routes"); |
| 24 | + config_ = factory_->createEmptyConfigProto(); |
| 25 | + |
| 26 | + envoy::extensions::internal_redirect::allow_listed_routes::v3::AllowListedRoutesConfig* |
| 27 | + typed_config = dynamic_cast<envoy::extensions::internal_redirect::allow_listed_routes::v3:: |
| 28 | + AllowListedRoutesConfig*>(config_.get()); |
| 29 | + typed_config->add_allowed_route_names("route_2"); |
| 30 | + typed_config->add_allowed_route_names("route_3"); |
| 31 | + } |
| 32 | + |
| 33 | + StreamInfo::FilterStateImpl filter_state_; |
| 34 | + Router::InternalRedirectPredicateFactory* factory_; |
| 35 | + ProtobufTypes::MessagePtr config_; |
| 36 | +}; |
| 37 | + |
| 38 | +TEST_F(AllowListedRoutesTest, PredicateTest) { |
| 39 | + auto predicate = factory_->createInternalRedirectPredicate(*config_, "route_0"); |
| 40 | + ASSERT(predicate); |
| 41 | + |
| 42 | + // Test route that is in the allow list (allowed) |
| 43 | + EXPECT_TRUE(predicate->acceptTargetRoute(filter_state_, "route_2", false, false)); |
| 44 | + |
| 45 | + // Test another route that is in the allow list (allowed) |
| 46 | + EXPECT_TRUE(predicate->acceptTargetRoute(filter_state_, "route_3", false, false)); |
| 47 | + |
| 48 | + // Test route that is not in the allow list (not allowed) |
| 49 | + EXPECT_FALSE(predicate->acceptTargetRoute(filter_state_, "route_1", false, false)); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(AllowListedRoutesTest, VerifyPredicateNameFunction) { |
| 53 | + auto predicate = factory_->createInternalRedirectPredicate(*config_, "route_0"); |
| 54 | + ASSERT(predicate); |
| 55 | + EXPECT_EQ("envoy.internal_redirect_predicates.allow_listed_routes", predicate->name()); |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace |
| 59 | +} // namespace InternalRedirect |
| 60 | +} // namespace Extensions |
| 61 | +} // namespace Envoy |
0 commit comments