| // Copyright 2026 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "src/public/lib/http_client.h" |
| |
| #include <chrono> |
| #include <future> |
| #include <string> |
| #include <utility> |
| |
| #include <gtest/gtest.h> |
| |
| namespace cobalt::lib { |
| namespace { |
| |
| constexpr int64_t kHttpOk = 200; |
| constexpr std::chrono::seconds kRequestTimeout(5); |
| |
| class StubAsyncGetHTTPClient : public HTTPClient { |
| public: |
| explicit StubAsyncGetHTTPClient(std::string get_response) |
| : get_response_(std::move(get_response)) {} |
| |
| std::future<StatusOr<HTTPResponse>> Get( |
| HTTPRequest /*request*/, std::chrono::steady_clock::time_point /*deadline*/) override { |
| std::promise<StatusOr<HTTPResponse>> response_promise; |
| response_promise.set_value(HTTPResponse(get_response_, Status::OkStatus(), kHttpOk)); |
| return response_promise.get_future(); |
| } |
| |
| private: |
| std::string get_response_; |
| }; |
| |
| class StubSyncGetHTTPClient : public HTTPClient { |
| public: |
| explicit StubSyncGetHTTPClient(std::string get_response) |
| : get_response_(std::move(get_response)) {} |
| |
| StatusOr<HTTPResponse> GetSync(HTTPRequest /*request*/, |
| std::chrono::steady_clock::time_point /*deadline*/) override { |
| return HTTPResponse(get_response_, Status::OkStatus(), kHttpOk); |
| } |
| |
| private: |
| std::string get_response_; |
| }; |
| |
| TEST(HTTPClientTest, GetSyncDelegatesToGet) { |
| StubAsyncGetHTTPClient client("public-keys-payload"); |
| |
| StatusOr<HTTPResponse> result = client.GetSync( |
| HTTPRequest("https://example.com/keys"), std::chrono::steady_clock::now() + kRequestTimeout); |
| |
| ASSERT_TRUE(result.ok()); |
| EXPECT_EQ(result->http_code, kHttpOk); |
| EXPECT_EQ(result->response, "public-keys-payload"); |
| } |
| |
| TEST(HTTPClientTest, GetDelegatesToGetSync) { |
| StubSyncGetHTTPClient client("public-keys-payload"); |
| |
| StatusOr<HTTPResponse> result = client |
| .Get(HTTPRequest("https://example.com/keys"), |
| std::chrono::steady_clock::now() + kRequestTimeout) |
| .get(); |
| |
| ASSERT_TRUE(result.ok()); |
| EXPECT_EQ(result->http_code, kHttpOk); |
| EXPECT_EQ(result->response, "public-keys-payload"); |
| } |
| |
| TEST(HTTPClientTest, EmptyHTTPClientPostSyncReturnsEmptyResponse) { |
| EmptyHTTPClient client; |
| |
| StatusOr<HTTPResponse> result = |
| client.PostSync(HTTPRequest("https://example.com/clearcut", "payload"), |
| std::chrono::steady_clock::now() + kRequestTimeout); |
| |
| ASSERT_TRUE(result.ok()); |
| EXPECT_EQ(result->http_code, kHttpOk); |
| EXPECT_TRUE(result->response.empty()); |
| } |
| |
| TEST(HTTPClientTest, EmptyHTTPClientGetSyncReturnsEmptyResponse) { |
| EmptyHTTPClient client; |
| |
| StatusOr<HTTPResponse> result = client.GetSync( |
| HTTPRequest("https://example.com/keys"), std::chrono::steady_clock::now() + kRequestTimeout); |
| |
| ASSERT_TRUE(result.ok()); |
| EXPECT_EQ(result->http_code, kHttpOk); |
| EXPECT_TRUE(result->response.empty()); |
| } |
| |
| } // namespace |
| } // namespace cobalt::lib |