chromium/content/browser/loader/keep_alive_url_loader_service_unittest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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[] =;

// Mock a receiver URLLoaderClient that may exist in renderer.
class MockReceiverURLLoaderClient : public network::mojom::URLLoaderClient {};

// Fakes a URLLoaderFactory that may exist in renderer, which only delegates to
// `remote_url_loader_factory`.
class FakeRemoteURLLoaderFactory {};

// Fakes a FetchLaterLoaderFactory that may exist in renderer, which only
// delegates to `remote_fetch_later_loader_factory`.
class FakeRemoteFetchLaterLoaderFactory {};

class ConfigurableURLLoaderThrottle final : public blink::URLLoaderThrottle {};

// Returns true if `arg` has a header of the given `name` and `value`.
// `arg` is an `network::mojom::URLResponseHeadPtr`.
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 =
        {}

}  // namespace

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) {}

// This test initially provides an unbind factory to KeepAliveURLLoaderService.
// After that, provides a bound factory via UpdateFactory.
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) {}

// Creates a fetchLater request which is deferred by default. The mojo endpoints
// in renderer then gets disconnected, which should start the fetchLater
// request.
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
       LoadFetchLaterRequestAndLoaderStayAliveAfterRendererIsDisconnected) {}

// Creates a fetchLater request which is deferred by default. The mojo endpoints
// in renderer then gets disconnected, and then the loader gets dropped by
// browser due to exceeding internal timeout.
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
       LoadFetchLaterRequestAndLoaderKilledAfterRendererIsDisconnected) {}

// Notifying KeepAliveURLLoaderService about shutdown should start any pending
// loaders.
TEST_F(FetchLaterKeepAliveURLLoaderServiceTest, Shutdown) {}

TEST_F(FetchLaterKeepAliveURLLoaderServiceTest,
       ForwardRedirectsAndResponseToAttributionRequestHelper) {}

}  // namespace content