unauthenticated_http_fetcher_unittest.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2022 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 "ash/quick_pair/repository/unauthenticated_http_fetcher.h"
  5. #include "ash/quick_pair/common/mock_quick_pair_browser_delegate.h"
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/test/task_environment.h"
  8. #include "net/http/http_util.h"
  9. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  10. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  11. #include "services/network/test/test_url_loader_factory.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace {
  15. constexpr char kBody[] = "body";
  16. constexpr char kTestUrl[] = "http://www.test.com/";
  17. } // namespace
  18. namespace ash {
  19. namespace quick_pair {
  20. class UnauthenticatedHttpFetcherTest : public testing::Test {
  21. public:
  22. void SetUp() override {
  23. http_fetcher_ = std::make_unique<UnauthenticatedHttpFetcher>(
  24. TRAFFIC_ANNOTATION_FOR_TESTS);
  25. browser_delegate_ = std::make_unique<MockQuickPairBrowserDelegate>();
  26. ON_CALL(*browser_delegate_, GetURLLoaderFactory())
  27. .WillByDefault(
  28. testing::Return(url_loader_factory_.GetSafeWeakWrapper()));
  29. }
  30. void TearDown() override { url_loader_factory_.ClearResponses(); }
  31. protected:
  32. base::test::TaskEnvironment task_environment_;
  33. std::unique_ptr<UnauthenticatedHttpFetcher> http_fetcher_;
  34. std::unique_ptr<MockQuickPairBrowserDelegate> browser_delegate_;
  35. network::TestURLLoaderFactory url_loader_factory_;
  36. };
  37. TEST_F(UnauthenticatedHttpFetcherTest, ExecuteGetRequest_Success) {
  38. GURL url(kTestUrl);
  39. std::string body(kBody);
  40. auto head = network::mojom::URLResponseHead::New();
  41. head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
  42. net::HttpUtil::AssembleRawHeaders(""));
  43. head->headers->GetMimeType(&head->mime_type);
  44. network::URLLoaderCompletionStatus status(net::Error::OK);
  45. status.decoded_body_length = body.size();
  46. url_loader_factory_.AddResponse(url, std::move(head), body, status);
  47. http_fetcher_->ExecuteGetRequest(
  48. url, base::BindOnce([](std::unique_ptr<std::string> response,
  49. std::unique_ptr<FastPairHttpResult> result) {
  50. ASSERT_EQ(kBody, *response);
  51. ASSERT_TRUE(result->IsSuccess());
  52. }));
  53. task_environment_.RunUntilIdle();
  54. }
  55. TEST_F(UnauthenticatedHttpFetcherTest, ExecuteGetRequest_Failure) {
  56. url_loader_factory_.AddResponse(kTestUrl, "",
  57. net::HTTP_INTERNAL_SERVER_ERROR);
  58. http_fetcher_->ExecuteGetRequest(
  59. GURL(kTestUrl),
  60. base::BindOnce([](std::unique_ptr<std::string> response,
  61. std::unique_ptr<FastPairHttpResult> result) {
  62. ASSERT_EQ(nullptr, response);
  63. ASSERT_FALSE(result->IsSuccess());
  64. ASSERT_EQ(result->http_response_error(),
  65. net::HTTP_INTERNAL_SERVER_ERROR);
  66. }));
  67. task_environment_.RunUntilIdle();
  68. }
  69. TEST_F(UnauthenticatedHttpFetcherTest, ExecuteGetRequest_Failure_NoUrlLoader) {
  70. ON_CALL(*browser_delegate_, GetURLLoaderFactory())
  71. .WillByDefault(testing::Return(nullptr));
  72. http_fetcher_->ExecuteGetRequest(
  73. GURL(kTestUrl),
  74. base::BindOnce([](std::unique_ptr<std::string> response,
  75. std::unique_ptr<FastPairHttpResult> result) {
  76. ASSERT_EQ(nullptr, response);
  77. ASSERT_EQ(nullptr, result);
  78. }));
  79. task_environment_.RunUntilIdle();
  80. }
  81. } // namespace quick_pair
  82. } // namespace ash