TEST_F(HttpHealthCheckerImplTest, TransportSocketMatchCriteria) {
  const std::string host = "fake_cluster";
  const std::string path = "/healthcheck";
  const std::string yaml = R"EOF(
    timeout: 1s
    interval: 1s
    no_traffic_interval: 1s
    interval_jitter_percent: 40
    unhealthy_threshold: 2
    healthy_threshold: 2
    http_health_check:
      service_name_matcher:
        prefix: locations
      path: /healthcheck
    transport_socket_match_criteria:
      key: value
    )EOF";

  auto default_socket_factory = std::make_unique<Network::MockTransportSocketFactory>();
  // We expect that this default_socket_factory will NOT be used to create a transport socket for
  // the health check connection.
  EXPECT_CALL(*default_socket_factory, createTransportSocket(_)).Times(0);
  EXPECT_CALL(*default_socket_factory, implementsSecureTransport()).WillRepeatedly(Return(true));
  auto transport_socket_match =
      std::make_unique<Upstream::MockTransportSocketMatcher>(std::move(default_socket_factory));

  auto metadata = TestUtility::parseYaml<envoy::config::core::v3::Metadata>(
      R"EOF(
    filter_metadata:
      envoy.transport_socket_match:
        key: value
  )EOF");

  Stats::IsolatedStoreImpl stats_store;
  auto health_transport_socket_stats = TransportSocketMatchStats{
      ALL_TRANSPORT_SOCKET_MATCH_STATS(POOL_COUNTER_PREFIX(stats_store, "test"))};
  auto health_check_only_socket_factory = std::make_unique<Network::MockTransportSocketFactory>();

  // We expect resolve() to be called twice, once for endpoint socket matching (with no metadata in
  // this test) and once for health check socket matching. In the latter we expect metadata that
  // matches the above object.
  EXPECT_CALL(*transport_socket_match, resolve(nullptr));
  EXPECT_CALL(*transport_socket_match, resolve(MetadataEq(metadata)))
      .WillOnce(Return(TransportSocketMatcher::MatchData(
          *health_check_only_socket_factory, health_transport_socket_stats, "health_check_only")));
  // The health_check_only_socket_factory should be used to create a transport socket for the health
  // check connection.
  EXPECT_CALL(*health_check_only_socket_factory, createTransportSocket(_));

  cluster_->info_->transport_socket_matcher_ = std::move(transport_socket_match);

  allocHealthChecker(yaml);

  cluster_->prioritySet().getMockHostSet(0)->hosts_ = {
      makeTestHost(cluster_->info_, "tcp://127.0.0.1:80", simTime())};
  cluster_->info_->stats().upstream_cx_total_.inc();
  expectSessionCreate();
  expectStreamCreate(0);
  EXPECT_CALL(*test_sessions_[0]->timeout_timer_, enableTimer(_, _));
  health_checker_->start();
  EXPECT_EQ(health_transport_socket_stats.total_match_count_.value(), 1);
}
TEST_F(HttpHealthCheckerImplTest, TransportSocketMatchCriteria) {
  const std::string host = "fake_cluster";
  const std::string path = "/healthcheck";
  const std::string yaml = R"EOF(
    timeout: 1s
    interval: 1s
    no_traffic_interval: 1s
    interval_jitter_percent: 40
    unhealthy_threshold: 2
    healthy_threshold: 2
    http_health_check:
      service_name_matcher:
        prefix: locations
      path: /healthcheck
    transport_socket_match_criteria:
      key: value
    )EOF";

  auto default_socket_factory = std::make_unique<Network::MockTransportSocketFactory>();
   
   
  EXPECT_CALL(*default_socket_factory, createTransportSocket(_)).Times(0);
  EXPECT_CALL(*default_socket_factory, implementsSecureTransport()).WillRepeatedly(Return(true));
  auto transport_socket_match =
      std::make_unique<Upstream::MockTransportSocketMatcher>(std::move(default_socket_factory));

  auto metadata = TestUtility::parseYaml<envoy::config::core::v3::Metadata>(
      R"EOF(
    filter_metadata:
      envoy.transport_socket_match:
        key: value
  )EOF");

  Stats::IsolatedStoreImpl stats_store;
  auto health_transport_socket_stats = TransportSocketMatchStats{
      ALL_TRANSPORT_SOCKET_MATCH_STATS(POOL_COUNTER_PREFIX(stats_store, "test"))};
  auto health_check_only_socket_factory = std::make_unique<Network::MockTransportSocketFactory>();

   
   
   
  EXPECT_CALL(*transport_socket_match, resolve(nullptr));
  EXPECT_CALL(*transport_socket_match, resolve(MetadataEq(metadata)))
      .WillOnce(Return(TransportSocketMatcher::MatchData(
          *health_check_only_socket_factory, health_transport_socket_stats, "health_check_only")));
   
   
  EXPECT_CALL(*health_check_only_socket_factory, createTransportSocket(_));

  cluster_->info_->transport_socket_matcher_ = std::move(transport_socket_match);

  allocHealthChecker(yaml);

  cluster_->prioritySet().getMockHostSet(0)->hosts_ = {
      makeTestHost(cluster_->info_, "tcp://127.0.0.1:80", simTime())};
  cluster_->info_->stats().upstream_cx_total_.inc();
  expectSessionCreate();
  expectStreamCreate(0);
  EXPECT_CALL(*test_sessions_[0]->timeout_timer_, enableTimer(_, _));
  health_checker_->start();
  EXPECT_EQ(health_transport_socket_stats.total_match_count_.value(), 1);
}