Repository: envoyproxy/envoy
File: test/extensions/transport_sockets/http_11_proxy/connect_test.cc (C++)

Code before:
        Network::Utility::parseInternetAddressAndPortNoThrow(address_string);

    const std::string port = target_port.has_value() ? absl::StrCat(":", *target_port) : "";
    const std::string proxy_info_hostname = absl::StrCat("www.foo.com", port);
    auto host = std::make_shared<NiceMock<Upstream::MockHostDescription>>();
    std::unique_ptr<Network::TransportSocketOptions::Http11ProxyInfo> info;

    if (!no_proxy_protocol) {
      if (use_metadata_proxy_addr) {
        auto metadata = std::make_shared<envoy::config::core::v3::Metadata>();
        const std::string metadata_key =
            Config::MetadataFilters::get().ENVOY_HTTP11_PROXY_TRANSPORT_SOCKET_ADDR;
        envoy::config::core::v3::Address addr_proto;
        addr_proto.mutable_socket_address()->set_address(proxy_info_hostname);
        addr_proto.mutable_socket_address()->set_port_value(1234);
        Protobuf::Any anypb;
        anypb.PackFrom(addr_proto);
        metadata->mutable_typed_filter_metadata()->emplace(std::make_pair(metadata_key, anypb));
        EXPECT_CALL(*host, metadata()).Times(AnyNumber()).WillRepeatedly(Return(metadata));

        if (with_hostname) {
          static const std::string hostname = "test.example.com";
          EXPECT_CALL(*host, hostname()).Times(AnyNumber()).WillRepeatedly(ReturnRef(hostname));

          // Create real address with port 443
          static auto real_address =
              std::make_shared<Network::Address::Ipv4Instance>("192.168.1.1", 443);
          EXPECT_CALL(*host, address()).Times(AnyNumber()).WillRepeatedly(Return(real_address));

          connect_data_ = Buffer::OwnedImpl(kConnectRequestWithHostname);
        } else {
          // Set empty hostname
          static const std::string empty_hostname;
          EXPECT_CALL(*host, hostname())
              .Times(AnyNumber())
              .WillRepeatedly(ReturnRef(empty_hostname));

          // Set up the connect_data buffer to use the host address view with Host header (RFC 9110)
          EXPECT_CALL(*host, address()).Times(AnyNumber()).WillRepeatedly(Return(address));
          connect_data_ =
              Buffer::OwnedImpl{fmt::format("CONNECT {} HTTP/1.1\r\nHost: {}\r\n\r\n",
                                            address->asStringView(), address->asStringView())};
        }
      } else {
        info = std::make_unique<Network::TransportSocketOptions::Http11ProxyInfo>(
            proxy_info_hostname, address);
        setAddress();
      }
    }

    options_ = std::make_shared<const Network::TransportSocketOptionsImpl>(

Review comment (suggestion):
`static` isn't necessary as it will keep the object from being freed for the lifetime of the process. It's a test so it shouldn't really matter, but let's remove it anyway.

Code after:
        Network::Utility::parseInternetAddressAndPortNoThrow(address_string);

    const std::string port = target_port.has_value() ? absl::StrCat(":", *target_port) : "";
    const std::string proxy_info_hostname = absl::StrCat("www.foo.com", port);
    auto host = std::make_shared<NiceMock<Upstream::MockHostDescription>>();
    std::unique_ptr<Network::TransportSocketOptions::Http11ProxyInfo> info;

    if (!no_proxy_protocol) {
      if (use_metadata_proxy_addr) {
        auto metadata = std::make_shared<envoy::config::core::v3::Metadata>();
        const std::string metadata_key =
            Config::MetadataFilters::get().ENVOY_HTTP11_PROXY_TRANSPORT_SOCKET_ADDR;
        envoy::config::core::v3::Address addr_proto;
        addr_proto.mutable_socket_address()->set_address(proxy_info_hostname);
        addr_proto.mutable_socket_address()->set_port_value(1234);
        Protobuf::Any anypb;
        anypb.PackFrom(addr_proto);
        metadata->mutable_typed_filter_metadata()->emplace(std::make_pair(metadata_key, anypb));
        EXPECT_CALL(*host, metadata()).Times(AnyNumber()).WillRepeatedly(Return(metadata));

        if (with_hostname) {
          static const std::string hostname = "test.example.com";
          EXPECT_CALL(*host, hostname()).Times(AnyNumber()).WillRepeatedly(ReturnRef(hostname));

          // Create real address with port 443.
          auto real_address = std::make_shared<Network::Address::Ipv4Instance>("192.168.1.1", 443);
          EXPECT_CALL(*host, address()).Times(AnyNumber()).WillRepeatedly(Return(real_address));

          connect_data_ = Buffer::OwnedImpl(ConnectRequestWithHostname);
        } else {
          // Set empty hostname.
          static const std::string empty_hostname;
          EXPECT_CALL(*host, hostname())
              .Times(AnyNumber())
              .WillRepeatedly(ReturnRef(empty_hostname));

          // Set up the connect_data buffer to use host address view with Host header (RFC 9110).
          EXPECT_CALL(*host, address()).Times(AnyNumber()).WillRepeatedly(Return(address));
          connect_data_ =
              Buffer::OwnedImpl{fmt::format("CONNECT {} HTTP/1.1\r\nHost: {}\r\n\r\n",
                                            address->asStringView(), address->asStringView())};
        }
      } else {
        info = std::make_unique<Network::TransportSocketOptions::Http11ProxyInfo>(
            proxy_info_hostname, address);
        setAddress();
      }
    }

    options_ = std::make_shared<const Network::TransportSocketOptionsImpl>(
        "", std::vector<std::string>{}, std::vector<std::string>{}, std::vector<std::string>{},