// Copyright 2024 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "services/network/public/cpp/url_loader_factory_builder.h" #include "base/test/task_environment.h" #include "mojo/public/cpp/bindings/receiver_set.h" #include "services/network/public/cpp/resource_request.h" #include "services/network/test/test_network_context.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" namespace network { namespace { ElementsAre; // This file explains the usage and behavior of `URLLoaderFactoryBuilder` and // related classes, in the form of working unit tests. class URLLoaderFactoryBuilderTest : public testing::Test { … }; class LoggingURLLoaderFactory final : public SharedURLLoaderFactory { … }; class LoggingNetworkContext final : public TestNetworkContext { … }; // ================================================================ // Basic URLLoaderFactoryBuilder usage with `Finish()`. TEST_F(URLLoaderFactoryBuilderTest, Basic) { … } TEST_F(URLLoaderFactoryBuilderTest, Empty) { … } // ================================================================ // Basic URLLoaderFactoryBuilder usage with `Finish()` with `PendingReceiver`. TEST_F(URLLoaderFactoryBuilderTest, BasicWithPendingReceiver) { … } TEST_F(URLLoaderFactoryBuilderTest, EmptyWithPendingReceiver) { … } // ================================================================ // Passing `URLLoaderFactoryBuilder&` to allow adding interceptors only. // A common scenario is that interceptors are added somewhere behind an // interface like `ContentBrowserClient::WillCreateURLLoaderFactory` while the // caller have the full control over creating the resulting `URLLoaderFactory` // except for adding interceptors in the middle. In such scenarios, we can // pass `URLLoaderFactoryBuilder&` that only allows appending interceptors // (and doesn't allow finishing the builder nor destructing the builder // object) to the intercepting interface. void URLLoaderFactoryBuilderTest::AddInterceptors( URLLoaderFactoryBuilder& factory_builder, int num_interceptors) { … } TEST_F(URLLoaderFactoryBuilderTest, AddInterceptorsByReference) { … } // The caller code remains the same even when no interceptors are added. TEST_F(URLLoaderFactoryBuilderTest, AddNoInterceptorsByReference) { … } // ================================================================ // More URLLoaderFactory-ish types support. // In the tests above, all interceptors, terminals and resulting endpoints // (returned by `Finish()`) are `SharedURLLoaderFactory`, but we can use other // similare interfaces. TEST_F(URLLoaderFactoryBuilderTest, URLLoaderFactoryFromTerminalPendingRemote) { … } TEST_F(URLLoaderFactoryBuilderTest, URLLoaderFactoryFromTerminalNetworkContext) { … } // Return PendingRemote instead of URLLoaderFactory. // See or add the `URLLoaderFactoryBuilder::WrapAs()` for actual implementation. TEST_F(URLLoaderFactoryBuilderTest, PendingRemoteFromTerminalPendingRemote) { … } TEST_F(URLLoaderFactoryBuilderTest, PendingRemoteFromTerminalNetworkContext) { … } } // namespace } // namespace network