pdf_url_loader_request_interceptor_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2021 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 "components/pdf/browser/pdf_url_loader_request_interceptor.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/run_loop.h"
  8. #include "base/test/gmock_callback_support.h"
  9. #include "base/test/mock_callback.h"
  10. #include "components/pdf/browser/fake_pdf_stream_delegate.h"
  11. #include "components/pdf/browser/mock_url_loader_client.h"
  12. #include "components/pdf/browser/pdf_stream_delegate.h"
  13. #include "content/public/browser/url_loader_request_interceptor.h"
  14. #include "content/public/test/test_renderer_host.h"
  15. #include "mojo/public/cpp/bindings/receiver.h"
  16. #include "services/network/public/cpp/resource_request.h"
  17. #include "services/network/public/mojom/fetch_api.mojom-shared.h"
  18. #include "services/network/public/mojom/url_loader.mojom.h"
  19. #include "testing/gmock/include/gmock/gmock.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. #include "url/gurl.h"
  22. namespace pdf {
  23. namespace {
  24. using ::testing::NiceMock;
  25. class PdfURLLoaderRequestInterceptorTest
  26. : public content::RenderViewHostTestHarness {
  27. protected:
  28. PdfURLLoaderRequestInterceptorTest() {
  29. resource_request_.mode = network::mojom::RequestMode::kNavigate;
  30. resource_request_.url = GURL(FakePdfStreamDelegate::kDefaultOriginalUrl);
  31. }
  32. std::unique_ptr<PdfURLLoaderRequestInterceptor> CreateInterceptor() {
  33. return std::make_unique<PdfURLLoaderRequestInterceptor>(
  34. main_rfh()->GetFrameTreeNodeId(), std::move(stream_delegate_));
  35. }
  36. std::unique_ptr<FakePdfStreamDelegate> stream_delegate_ =
  37. std::make_unique<FakePdfStreamDelegate>();
  38. network::ResourceRequest resource_request_;
  39. base::MockCallback<content::URLLoaderRequestInterceptor::LoaderCallback>
  40. loader_callback_;
  41. };
  42. void RunRequestHandler(
  43. content::URLLoaderRequestInterceptor::RequestHandler request_handler) {
  44. base::RunLoop run_loop;
  45. NiceMock<MockURLLoaderClient> mock_client;
  46. EXPECT_CALL(mock_client, OnReceiveResponse).WillOnce([&run_loop]() {
  47. run_loop.Quit();
  48. });
  49. mojo::Receiver<network::mojom::URLLoaderClient> client_receiver(&mock_client);
  50. std::move(request_handler)
  51. .Run({}, {}, client_receiver.BindNewPipeAndPassRemote());
  52. run_loop.Run();
  53. }
  54. } // namespace
  55. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateInterceptor) {
  56. EXPECT_TRUE(PdfURLLoaderRequestInterceptor::MaybeCreateInterceptor(
  57. main_rfh()->GetFrameTreeNodeId(), std::move(stream_delegate_)));
  58. }
  59. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoader) {
  60. EXPECT_CALL(loader_callback_, Run(base::test::IsNotNullCallback()))
  61. .WillOnce(RunRequestHandler);
  62. auto interceptor = CreateInterceptor();
  63. interceptor->MaybeCreateLoader(resource_request_, browser_context(),
  64. loader_callback_.Get());
  65. }
  66. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderNotNavigate) {
  67. EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));
  68. auto interceptor = CreateInterceptor();
  69. resource_request_.mode = network::mojom::RequestMode::kCors;
  70. interceptor->MaybeCreateLoader(resource_request_, browser_context(),
  71. loader_callback_.Get());
  72. }
  73. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderDeleteContents) {
  74. EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));
  75. auto interceptor = CreateInterceptor();
  76. DeleteContents();
  77. interceptor->MaybeCreateLoader(resource_request_, browser_context(),
  78. loader_callback_.Get());
  79. }
  80. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderNoStreamInfo) {
  81. EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));
  82. stream_delegate_->clear_stream_info();
  83. auto interceptor = CreateInterceptor();
  84. interceptor->MaybeCreateLoader(resource_request_, browser_context(),
  85. loader_callback_.Get());
  86. }
  87. TEST_F(PdfURLLoaderRequestInterceptorTest, MaybeCreateLoaderOtherUrl) {
  88. EXPECT_CALL(loader_callback_, Run(base::test::IsNullCallback()));
  89. auto interceptor = CreateInterceptor();
  90. resource_request_.url = GURL("https://example.test");
  91. interceptor->MaybeCreateLoader(resource_request_, browser_context(),
  92. loader_callback_.Get());
  93. }
  94. } // namespace pdf