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