Skip to content

Commit

Permalink
change send single request
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Zhang <danzh@google.com>
  • Loading branch information
danzh1989 committed Mar 22, 2021
1 parent 4c2b490 commit 97292b2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 51 deletions.
2 changes: 1 addition & 1 deletion test/integration/http_integration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ void HttpIntegrationTest::initialize() {
ON_CALL(mock_factory_ctx, api()).WillByDefault(testing::ReturnRef(*api_));

quic_transport_socket_factory_ =
IntegrationUtil::createQuicClientTransportSocketFactory(mock_factory_ctx, san_to_match_);
IntegrationUtil::createQuicUpstreamTransportSocketFactory(mock_factory_ctx, san_to_match_);

config_helper_.addQuicDownstreamTransportSocketConfig(set_reuse_port_);

Expand Down
106 changes: 58 additions & 48 deletions test/integration/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct ConnectionCallbacks : public Network::ConnectionCallbacks {
bool connected_{false};
};

Network::TransportSocketFactoryPtr IntegrationUtil::createQuicClientTransportSocketFactory(
Network::TransportSocketFactoryPtr IntegrationUtil::createQuicUpstreamTransportSocketFactory(
Server::Configuration::TransportSocketFactoryContext& context,
const std::string& san_to_match) {
envoy::extensions::transport_sockets::quic::v3::QuicUpstreamTransport
Expand All @@ -128,6 +128,37 @@ Network::TransportSocketFactoryPtr IntegrationUtil::createQuicClientTransportSoc
return config_factory.createTransportSocketFactory(quic_transport_socket_config, context);
}

BufferingStreamDecoderPtr
sendRequestAndWaitForResponse(Event::Dispatcher& dispatcher, const std::string& method,
const std::string& url, const std::string& body,
const std::string& host, const std::string& content_type,
Http::CodecClientProd& client) {
BufferingStreamDecoderPtr response(new BufferingStreamDecoder([&]() -> void {
client.close();
dispatcher.exit();
}));
Http::RequestEncoder& encoder = client.newStream(*response);
encoder.getStream().addCallbacks(*response);

Http::TestRequestHeaderMapImpl headers;
headers.setMethod(method);
headers.setPath(url);
headers.setHost(host);
headers.setReferenceScheme(Http::Headers::get().SchemeValues.Http);
if (!content_type.empty()) {
headers.setContentType(content_type);
}
const auto status = encoder.encodeHeaders(headers, body.empty());
ASSERT(status.ok());
if (!body.empty()) {
Buffer::OwnedImpl body_buffer(body);
encoder.encodeData(body_buffer, true);
}

dispatcher.run(Event::Dispatcher::RunType::Block);
return response;
}

BufferingStreamDecoderPtr
IntegrationUtil::makeSingleRequest(const Network::Address::InstanceConstSharedPtr& addr,
const std::string& method, const std::string& url,
Expand All @@ -147,64 +178,43 @@ IntegrationUtil::makeSingleRequest(const Network::Address::InstanceConstSharedPt
fmt::format("{}://127.0.0.1:80", (type == Http::CodecClient::Type::HTTP3 ? "udp" : "tcp")),
time_system)};

if (type <= Http::CodecClient::Type::HTTP2) {
Http::CodecClientProd client(
type,
dispatcher->createClientConnection(addr, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr),
host_description, *dispatcher, random);
return sendRequestAndWaitForResponse(*dispatcher, method, url, body, host, content_type,
client);
}

NiceMock<Server::Configuration::MockTransportSocketFactoryContext> mock_factory_ctx;
ON_CALL(mock_factory_ctx, api()).WillByDefault(testing::ReturnRef(api));
Network::TransportSocketFactoryPtr transport_socket_factory =
createQuicClientTransportSocketFactory(mock_factory_ctx, "spiffe://lyft.com/backend-team");
createQuicUpstreamTransportSocketFactory(mock_factory_ctx, "spiffe://lyft.com/backend-team");
std::unique_ptr<Http::PersistentQuicInfo> persistent_info;
Network::ClientConnectionPtr connection;
if (type == Http::CodecClient::Type::HTTP3) {
Http::QuicClientConnectionFactory& connection_factory =
Config::Utility::getAndCheckFactoryByName<Http::QuicClientConnectionFactory>(
Http::QuicCodecNames::get().Quiche);
persistent_info = connection_factory.createNetworkConnectionInfo(
*dispatcher, *transport_socket_factory, mock_stats_store, time_system, addr);

Network::Address::InstanceConstSharedPtr local_address;
if (addr->ip()->version() == Network::Address::IpVersion::v4) {
local_address = Network::Utility::getLocalAddress(Network::Address::IpVersion::v4);
} else {
// Docker only works with loopback v6 address.
local_address = std::make_shared<Network::Address::Ipv6Instance>("::1");
}
connection = connection_factory.createQuicNetworkConnection(*persistent_info, *dispatcher, addr,
local_address);
connection->addConnectionCallbacks(connection_callbacks);
Http::QuicClientConnectionFactory& connection_factory =
Config::Utility::getAndCheckFactoryByName<Http::QuicClientConnectionFactory>(
Http::QuicCodecNames::get().Quiche);
persistent_info = connection_factory.createNetworkConnectionInfo(
*dispatcher, *transport_socket_factory, mock_stats_store, time_system, addr);

Network::Address::InstanceConstSharedPtr local_address;
if (addr->ip()->version() == Network::Address::IpVersion::v4) {
local_address = Network::Utility::getLocalAddress(Network::Address::IpVersion::v4);
} else {
connection =
dispatcher->createClientConnection(addr, Network::Address::InstanceConstSharedPtr(),
Network::Test::createRawBufferSocket(), nullptr);
// Docker only works with loopback v6 address.
local_address = std::make_shared<Network::Address::Ipv6Instance>("::1");
}
Network::ClientConnectionPtr connection = connection_factory.createQuicNetworkConnection(
*persistent_info, *dispatcher, addr, local_address);
connection->addConnectionCallbacks(connection_callbacks);
Http::CodecClientProd client(type, std::move(connection), host_description, *dispatcher, random);
if (type == Http::CodecClient::Type::HTTP3) {
// Quic connection needs to finish handshake.
dispatcher->run(Event::Dispatcher::RunType::Block);
}

BufferingStreamDecoderPtr response(new BufferingStreamDecoder([&]() -> void {
client.close();
dispatcher->exit();
}));
Http::RequestEncoder& encoder = client.newStream(*response);
encoder.getStream().addCallbacks(*response);

Http::TestRequestHeaderMapImpl headers;
headers.setMethod(method);
headers.setPath(url);
headers.setHost(host);
headers.setReferenceScheme(Http::Headers::get().SchemeValues.Http);
if (!content_type.empty()) {
headers.setContentType(content_type);
}
const auto status = encoder.encodeHeaders(headers, body.empty());
ASSERT(status.ok());
if (!body.empty()) {
Buffer::OwnedImpl body_buffer(body);
encoder.encodeData(body_buffer, true);
}

dispatcher->run(Event::Dispatcher::RunType::Block);
return response;
return sendRequestAndWaitForResponse(*dispatcher, method, url, body, host, content_type, client);
}

BufferingStreamDecoderPtr
Expand Down
4 changes: 2 additions & 2 deletions test/integration/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ class IntegrationUtil {
const std::string& content_type = "");

/**
* Create transport socket factory for Quic client transport socket.
* Create transport socket factory for Quic upstream transport socket.
* @param context supplies the port to connect to on localhost.
* @param san_to_match configs |context| to match Subject Alternative Name during certificate
* verification.
* @return TransportSocketFactoryPtr the client transport socket factory.
*/
static Network::TransportSocketFactoryPtr createQuicClientTransportSocketFactory(
static Network::TransportSocketFactoryPtr createQuicUpstreamTransportSocketFactory(
Server::Configuration::TransportSocketFactoryContext& context,
const std::string& san_to_match);
};
Expand Down

0 comments on commit 97292b2

Please sign in to comment.