distiller_url_fetcher_unittest.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2013 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/dom_distiller/core/distiller_url_fetcher.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/task_environment.h"
  10. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  11. #include "services/network/public/mojom/url_response_head.mojom.h"
  12. #include "services/network/test/test_url_loader_factory.h"
  13. #include "services/network/test/test_utils.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "url/gurl.h"
  16. const char kTestPageA[] = "http://www.a.com/";
  17. const char kTestPageAResponse[] = {1, 2, 3, 4, 5, 6, 7};
  18. const char kTestPageB[] = "http://www.b.com/";
  19. const char kTestPageBResponse[] = {'a', 'b', 'c'};
  20. class DistillerURLFetcherTest : public testing::Test {
  21. public:
  22. DistillerURLFetcherTest()
  23. : test_shared_url_loader_factory_(
  24. base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
  25. &test_url_loader_factory_)) {}
  26. void FetcherCallback(const std::string& response) { response_ = response; }
  27. protected:
  28. // testing::Test implementation:
  29. void SetUp() override {
  30. url_fetcher_ = std::make_unique<dom_distiller::DistillerURLFetcher>(
  31. test_shared_url_loader_factory_);
  32. test_url_loader_factory_.AddResponse(
  33. kTestPageA,
  34. std::string(kTestPageAResponse, sizeof(kTestPageAResponse)));
  35. test_url_loader_factory_.AddResponse(
  36. GURL(kTestPageB),
  37. network::CreateURLResponseHead(net::HTTP_INTERNAL_SERVER_ERROR),
  38. std::string(kTestPageBResponse, sizeof(kTestPageBResponse)),
  39. network::URLLoaderCompletionStatus(net::OK));
  40. }
  41. void Fetch(const std::string& url, const std::string& expected_response) {
  42. url_fetcher_->FetchURL(
  43. url, base::BindOnce(&DistillerURLFetcherTest::FetcherCallback,
  44. base::Unretained(this)));
  45. base::RunLoop().RunUntilIdle();
  46. CHECK_EQ(expected_response, response_);
  47. }
  48. base::test::SingleThreadTaskEnvironment task_environment_{
  49. base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
  50. std::unique_ptr<dom_distiller::DistillerURLFetcher> url_fetcher_;
  51. network::TestURLLoaderFactory test_url_loader_factory_;
  52. scoped_refptr<network::SharedURLLoaderFactory>
  53. test_shared_url_loader_factory_;
  54. std::string response_;
  55. };
  56. TEST_F(DistillerURLFetcherTest, PopulateProto) {
  57. Fetch(kTestPageA,
  58. std::string(kTestPageAResponse, sizeof(kTestPageAResponse)));
  59. }
  60. TEST_F(DistillerURLFetcherTest, PopulateProtoFailedFetch) {
  61. // Expect the callback to contain an empty string for this URL.
  62. Fetch(kTestPageB, std::string(std::string(), 0));
  63. }