transitional_url_loader_factory_owner_unittest.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/transitional_url_loader_factory_owner.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/message_loop/message_pump_type.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/bind.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/threading/thread.h"
  12. #include "net/test/embedded_test_server/default_handlers.h"
  13. #include "net/test/embedded_test_server/embedded_test_server.h"
  14. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  15. #include "net/url_request/url_request_test_util.h"
  16. #include "services/network/public/cpp/resource_request.h"
  17. #include "services/network/public/cpp/shared_url_loader_factory.h"
  18. #include "services/network/public/cpp/simple_url_loader.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace network {
  21. namespace {
  22. class TransitionalURLLoaderFactoryOwnerTest : public ::testing::Test {
  23. public:
  24. TransitionalURLLoaderFactoryOwnerTest()
  25. : task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
  26. void SetUp() override {
  27. net::test_server::RegisterDefaultHandlers(&test_server_);
  28. ASSERT_TRUE(test_server_.Start());
  29. }
  30. void TestOnTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  31. base::OnceClosure flush_thread) {
  32. auto url_request_context_getter =
  33. base::MakeRefCounted<net::TestURLRequestContextGetter>(task_runner);
  34. auto owner = std::make_unique<TransitionalURLLoaderFactoryOwner>(
  35. url_request_context_getter);
  36. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
  37. owner->GetURLLoaderFactory();
  38. ASSERT_TRUE(url_loader_factory != nullptr);
  39. // Try fetching something --- see if |url_loader_factory| is usable.
  40. auto request = std::make_unique<ResourceRequest>();
  41. request->url = test_server_.GetURL("/cachetime");
  42. std::unique_ptr<SimpleURLLoader> loader = SimpleURLLoader::Create(
  43. std::move(request), TRAFFIC_ANNOTATION_FOR_TESTS);
  44. base::RunLoop run_loop;
  45. loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
  46. url_loader_factory.get(),
  47. base::BindLambdaForTesting([&](std::unique_ptr<std::string> body) {
  48. ASSERT_TRUE(body);
  49. EXPECT_NE(std::string::npos, body->find("<title>Cache:")) << *body;
  50. run_loop.Quit();
  51. }));
  52. run_loop.Run();
  53. // Clean stuff up, should be clean on lsan.
  54. owner = nullptr;
  55. std::move(flush_thread).Run();
  56. }
  57. protected:
  58. base::test::TaskEnvironment task_environment_;
  59. net::EmbeddedTestServer test_server_;
  60. };
  61. TEST_F(TransitionalURLLoaderFactoryOwnerTest, CrossThread) {
  62. base::Thread io_thread("IO");
  63. base::Thread::Options options;
  64. options.message_pump_type = base::MessagePumpType::IO;
  65. ASSERT_TRUE(io_thread.StartWithOptions(std::move(options)));
  66. TestOnTaskRunner(io_thread.task_runner(), base::BindLambdaForTesting([&]() {
  67. io_thread.FlushForTesting();
  68. }));
  69. }
  70. TEST_F(TransitionalURLLoaderFactoryOwnerTest, SameThread) {
  71. TestOnTaskRunner(
  72. task_environment_.GetMainThreadTaskRunner(),
  73. base::BindLambdaForTesting([&]() { task_environment_.RunUntilIdle(); }));
  74. }
  75. } // namespace
  76. } // namespace network