Repository: envoyproxy/envoy
File: test/extensions/filters/http/on_demand/on_demand_filter_test.cc (C++)

Code before:
// incomplete body
TEST_F(OnDemandFilterTest, OnClusterDiscoveryCompletionClusterFoundRedirectWithIncompleteBody) {
  Http::TestRequestHeaderMapImpl headers;
  Buffer::OwnedImpl buffer;
  // Simulate request with body that hasn't ended yet
  filter_->decodeHeaders(headers, false);
  filter_->decodeData(buffer, false);
  EXPECT_CALL(decoder_callbacks_, continueDecoding());
  EXPECT_CALL(decoder_callbacks_.downstream_callbacks_, clearRouteCache()).Times(0);
  filter_->onClusterDiscoveryCompletion(Upstream::ClusterDiscoveryStatus::Available);
}

TEST(OnDemandConfigTest, Basic) {
  NiceMock<Upstream::MockClusterManager> cm;
  ProtobufMessage::StrictValidationVisitorImpl visitor;
  envoy::extensions::filters::http::on_demand::v3::OnDemand config;

  OnDemandFilterConfig config1(config, cm, visitor);

  config.mutable_odcds();
  OnDemandFilterConfig config2(config, cm, visitor);

  config.mutable_odcds()->set_resources_locator("foo");
  EXPECT_THROW_WITH_MESSAGE(
      { OnDemandFilterConfig config3(config, cm, visitor); }, EnvoyException,
      "foo does not have a xdstp:, http: or file: scheme");
}

} // namespace OnDemand
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy


Review comment (suggestion):
It is unclear what should be done with these tests once the runtime flag is deprecated. I don't think the tests should disappear, so it makes sense to fix them as part of this PR.


Code after:
  EXPECT_CALL(decoder_callbacks_, recreateStream(_)).WillOnce(Return(true));
  filter_->onClusterDiscoveryCompletion(Upstream::ClusterDiscoveryStatus::Available);
}

// tests onClusterDiscoveryCompletion when a cluster is available with a fully read body
TEST_F(OnDemandFilterTest,
       OnClusterDiscoveryCompletionClusterFoundWithFullyReadBodyNoRecreateStream) {
  TestScopedRuntime scoped_runtime;
  scoped_runtime.mergeValues(
      {{"envoy.reloadable_features.on_demand_cluster_no_recreate_stream", "true"}});
  Http::TestRequestHeaderMapImpl headers;
  Buffer::OwnedImpl buffer;
  // Simulate request with body that has been fully read (end_stream = true)
  filter_->decodeHeaders(headers, false);
  filter_->decodeData(buffer, true);
  EXPECT_CALL(decoder_callbacks_, continueDecoding());
  EXPECT_CALL(decoder_callbacks_.downstream_callbacks_, clearRouteCache()).Times(0);
  EXPECT_CALL(decoder_callbacks_, recreateStream(_)).Times(0);
  filter_->onClusterDiscoveryCompletion(Upstream::ClusterDiscoveryStatus::Available);
}

// tests onClusterDiscoveryCompletion when a cluster is available, but recreating a stream failed
TEST_F(OnDemandFilterTest, OnClusterDiscoveryCompletionClusterFoundRecreateStreamFailed) {
  TestScopedRuntime scoped_runtime;
  // This test is irrelevant for the case when there is no recreateStream call.
  scoped_runtime.mergeValues(
      {{"envoy.reloadable_features.on_demand_cluster_no_recreate_stream", "false"}});
  Http::TestRequestHeaderMapImpl headers;
  filter_->decodeHeaders(headers, true);
  EXPECT_CALL(decoder_callbacks_, continueDecoding());
  EXPECT_CALL(decoder_callbacks_.downstream_callbacks_, clearRouteCache()).Times(0);
  EXPECT_CALL(decoder_callbacks_, recreateStream(_)).WillOnce(Return(false));
  filter_->onClusterDiscoveryCompletion(Upstream::ClusterDiscoveryStatus::Available);
}

// tests onClusterDiscoveryCompletion when a cluster is available, but redirect contains an
// incomplete body
TEST_F(OnDemandFilterTest, OnClusterDiscoveryCompletionClusterFoundRedirectWithIncompleteBody) {
  Http::TestRequestHeaderMapImpl headers;
  Buffer::OwnedImpl buffer;
  // Simulate request with body that hasn't ended yet
  filter_->decodeHeaders(headers, false);
  filter_->decodeData(buffer, false);
  EXPECT_CALL(decoder_callbacks_, continueDecoding());
  EXPECT_CALL(decoder_callbacks_.downstream_callbacks_, clearRouteCache()).Times(0);
  filter_->onClusterDiscoveryCompletion(Upstream::ClusterDiscoveryStatus::Available);
}

TEST(OnDemandConfigTest, Basic) {
  NiceMock<Upstream::MockClusterManager> cm;
  ProtobufMessage::StrictValidationVisitorImpl visitor;