TEST_P(DownstreamProtocolIntegrationTest, MaxRequestsPerConnectionReached) {
  config_helper_.setDownstreamMaxRequestsPerConnection(2);
  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));

  // Sending first request and waiting to complete the response.
  auto encoder_decoder = codec_client_->startRequest(default_request_headers_);
  request_encoder_ = &encoder_decoder.first;
  auto response = std::move(encoder_decoder.second);
  codec_client_->sendData(*request_encoder_, 1, true);
  waitForNextUpstreamRequest();
  upstream_request_->encodeHeaders(default_response_headers_, true);

  ASSERT_TRUE(response->waitForEndStream());
  EXPECT_TRUE(upstream_request_->complete());
  EXPECT_TRUE(response->complete());
  EXPECT_EQ(test_server_->counter("http.config_test.downstream_cx_max_requests_reached")->value(),
            0);

  // Sending second request and waiting to complete the response.
  auto encoder_decoder_2 = codec_client_->startRequest(default_request_headers_);
  request_encoder_ = &encoder_decoder_2.first;
  auto response_2 = std::move(encoder_decoder_2.second);
  codec_client_->sendData(*request_encoder_, 1, true);
  waitForNextUpstreamRequest();
  upstream_request_->encodeHeaders(default_response_headers_, true);

  ASSERT_TRUE(response_2->waitForEndStream());
  EXPECT_TRUE(upstream_request_->complete());
  EXPECT_TRUE(response_2->complete());
  EXPECT_EQ(test_server_->counter("http.config_test.downstream_cx_max_requests_reached")->value(),
            1);

  if (downstream_protocol_ == Http::CodecType::HTTP1) {
    EXPECT_EQ(nullptr, response->headers().Connection());
    EXPECT_EQ("close", response_2->headers().getConnectionValue());
  } else {
    EXPECT_TRUE(codec_client_->sawGoAway());
  }
  ASSERT_TRUE(codec_client_->waitForDisconnect());
}
TEST_P(DownstreamProtocolIntegrationTest, MaxRequestsPerConnectionReached) {
  config_helper_.setDownstreamMaxRequestsPerConnection(2);
  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));

   
  auto encoder_decoder = codec_client_->startRequest(default_request_headers_);
  request_encoder_ = &encoder_decoder.first;
  auto response = std::move(encoder_decoder.second);
  codec_client_->sendData(*request_encoder_, 1, true);
  waitForNextUpstreamRequest();
  upstream_request_->encodeHeaders(default_response_headers_, true);

  ASSERT_TRUE(response->waitForEndStream());
  EXPECT_TRUE(upstream_request_->complete());
  EXPECT_TRUE(response->complete());
  EXPECT_EQ(test_server_->counter("http.config_test.downstream_cx_max_requests_reached")->value(),
            0);

   
  auto encoder_decoder_2 = codec_client_->startRequest(default_request_headers_);
  request_encoder_ = &encoder_decoder_2.first;
  auto response_2 = std::move(encoder_decoder_2.second);
  codec_client_->sendData(*request_encoder_, 1, true);
  waitForNextUpstreamRequest();
  upstream_request_->encodeHeaders(default_response_headers_, true);

  ASSERT_TRUE(response_2->waitForEndStream());
  EXPECT_TRUE(upstream_request_->complete());
  EXPECT_TRUE(response_2->complete());
  EXPECT_EQ(test_server_->counter("http.config_test.downstream_cx_max_requests_reached")->value(),
            1);

  if (downstream_protocol_ == Http::CodecType::HTTP1) {
    EXPECT_EQ(nullptr, response->headers().Connection());
    EXPECT_EQ("close", response_2->headers().getConnectionValue());
  } else {
    EXPECT_TRUE(codec_client_->sawGoAway());
  }
  ASSERT_TRUE(codec_client_->waitForDisconnect());
}