#include "content/browser/loader/keep_alive_url_loader_service.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "components/attribution_reporting/constants.h"
#include "content/browser/attribution_reporting/attribution_data_host_manager_impl.h"
#include "content/browser/attribution_reporting/test/mock_attribution_manager.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/test_utils.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/functions.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/network/public/cpp/content_security_policy/content_security_policy.h"
#include "services/network/public/cpp/parsed_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/attribution.mojom.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "services/network/public/mojom/early_hints.mojom.h"
#include "services/network/public/mojom/ip_address_space.mojom.h"
#include "services/network/public/mojom/referrer_policy.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "third_party/blink/public/mojom/loader/fetch_later.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
kAttributionReportingRegisterSourceHeader;
kAttributionReportingRegisterTriggerHeader;
_;
Eq;
IsEmpty;
SizeIs;
WithArg;
constexpr char kTestRequestUrl[] = …;
constexpr char kTestResponseHeaderName[] = …;
constexpr char kTestResponseHeaderValue[] = …;
constexpr char kTestRedirectRequestUrl[] = …;
constexpr char kTestUnSafeRedirectRequestUrl[] = …;
constexpr char kTestViolatingCSPRedirectRequestUrl[] = …;
class MockReceiverURLLoaderClient : public network::mojom::URLLoaderClient { … };
class FakeRemoteURLLoaderFactory { … };
class FakeRemoteFetchLaterLoaderFactory { … };
class ConfigurableURLLoaderThrottle final : public blink::URLLoaderThrottle { … };
MATCHER_P2(ResponseHasHeader,
name,
value,
base::StringPrintf("Response has %sheader[%s=%s]",
negation ? "no " : "",
name,
value)) { … }
network::ResourceRequest CreateFetchLaterResourceRequest(const GURL& url) { … }
network::ResourceRequest CreateResourceRequest(
const GURL& url,
bool keepalive = true,
bool is_trusted = false,
std::optional<network::mojom::RedirectMode> redirect_mode = std::nullopt) { … }
network::mojom::URLResponseHeadPtr CreateResponseHead(
const std::vector<std::pair<std::string, std::string>>& extra_headers =
{ … }
net::RedirectInfo CreateRedirectInfo(const GURL& new_url) { … }
network::mojom::EarlyHintsPtr CreateEarlyHints(
const GURL& url,
const std::vector<std::pair<std::string, std::string>>& extra_headers =
{ … }
}
class KeepAliveURLLoaderServiceTestBase : public RenderViewHostTestHarness { … };
class KeepAliveURLLoaderServiceTest : public KeepAliveURLLoaderServiceTestBase { … };
TEST_F(KeepAliveURLLoaderServiceTest,
LoadKeepAliveRequestWithInvalidFeatureAndTerminate) { … }
TEST_F(KeepAliveURLLoaderServiceTest, LoadFetchLaterRequestAndTerminate) { … }
TEST_F(KeepAliveURLLoaderServiceTest, LoadNonKeepaliveRequestAndTerminate) { … }
TEST_F(KeepAliveURLLoaderServiceTest, LoadTrustedRequestAndTerminate) { … }
TEST_F(KeepAliveURLLoaderServiceTest, LoadRequestAfterPageIsUnloaded) { … }
TEST_F(KeepAliveURLLoaderServiceTest, LoadRequestAfterUpdateFactory) { … }
TEST_F(KeepAliveURLLoaderServiceTest, ForwardOnReceiveResponse) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
ForwardRedirectsAndResponseToAttributionRequestHelper) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveResponseAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest, DoNotForwardOnReceiveRedirect) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveRedirectAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveRedirectToUnSafeTargetAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveRedirectWithErrorRedirectModeAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveRedirectViolatingCSPAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest, ForwardOnReceiveEarlyHints) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnReceiveEarlyHintsAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest, ForwardOnUploadProgress) { … }
TEST_F(KeepAliveURLLoaderServiceTest, ForwardOnTransferSizeUpdated) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
OnTransferSizeUpdatedAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest, ForwardOnComplete) { … }
TEST_F(KeepAliveURLLoaderServiceTest, OnCompleteAfterRendererIsDisconnected) { … }
TEST_F(KeepAliveURLLoaderServiceTest, RendererDisconnectedBeforeOnComplete) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
RendererConnectedAndThrottleCancelLoaderBeforeStartRequest) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
RendererDisconnectedAndThrottleCancelLoaderBeforeStartRedirect) { … }
TEST_F(KeepAliveURLLoaderServiceTest,
RendererDisconnectedAndThrottleDeferLoaderBeforeStartRedirect) { … }
class FetchLaterKeepAliveURLLoaderServiceTest
: public KeepAliveURLLoaderServiceTestBase { … };
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
LoadFetchLaterRequestWithInvalidFeatureAndTerminate) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
LoadNonFetchLaterRequestAndTerminate) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
LoadFetchLaterRequestAndDeferred) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
LoadFetchLaterRequestAndLoaderStayAliveAfterRendererIsDisconnected) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
LoadFetchLaterRequestAndLoaderKilledAfterRendererIsDisconnected) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest, Shutdown) { … }
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
ForwardRedirectsAndResponseToAttributionRequestHelper) { … }
}