|
19 | 19 |
|
20 | 20 | #include "iceberg/catalog/rest/rest_catalog.h" |
21 | 21 |
|
| 22 | +#include <httplib.h> |
| 23 | + |
| 24 | +#include <memory> |
| 25 | +#include <thread> |
| 26 | + |
22 | 27 | #include <gmock/gmock.h> |
23 | 28 | #include <gtest/gtest.h> |
24 | | - |
25 | | -#include "cpr/error.h" |
26 | | -#include "cpr/response.h" |
| 29 | +#include <nlohmann/json.hpp> |
27 | 30 |
|
28 | 31 | namespace iceberg::catalog::rest { |
29 | 32 |
|
30 | | -class RestCatalogTest : public ::testing::Test {}; |
| 33 | +class RestCatalogIntegrationTest : public ::testing::Test { |
| 34 | + protected: |
| 35 | + void SetUp() override { |
| 36 | + server_ = std::make_unique<httplib::Server>(); |
| 37 | + port_ = server_->bind_to_any_port("127.0.0.1"); |
31 | 38 |
|
32 | | -TEST_F(RestCatalogTest, CanCreateRestCatalog) { |
33 | | - RestCatalog catalog("http://localhost:8181"); |
34 | | - SUCCEED(); |
35 | | -} |
| 39 | + server_thread_ = std::thread([this]() { server_->listen_after_bind(); }); |
| 40 | + } |
| 41 | + |
| 42 | + void TearDown() override { |
| 43 | + server_->stop(); |
| 44 | + if (server_thread_.joinable()) { |
| 45 | + server_thread_.join(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + std::unique_ptr<httplib::Server> server_; |
| 50 | + int port_ = -1; |
| 51 | + std::thread server_thread_; |
| 52 | +}; |
| 53 | + |
| 54 | +TEST_F(RestCatalogIntegrationTest, GetConfigSuccessfully) { |
| 55 | + server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) { |
| 56 | + res.status = 200; |
| 57 | + res.set_content(R"({"warehouse": "s3://test-bucket"})", "application/json"); |
| 58 | + }); |
36 | 59 |
|
37 | | -TEST_F(RestCatalogTest, MultipleCatalogInstances) { |
38 | | - RestCatalog catalog1("http://localhost:8181"); |
39 | | - RestCatalog catalog2("http://localhost:8182"); |
40 | | - SUCCEED(); |
| 60 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 61 | + RestCatalog catalog(base_uri); |
| 62 | + cpr::Response response = catalog.GetConfig(); |
| 63 | + |
| 64 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 65 | + ASSERT_EQ(response.status_code, 200); |
| 66 | + |
| 67 | + auto json_body = nlohmann::json::parse(response.text); |
| 68 | + EXPECT_EQ(json_body["warehouse"], "s3://test-bucket"); |
41 | 69 | } |
42 | 70 |
|
43 | | -TEST_F(RestCatalogTest, TestCprIntegration) { |
44 | | - RestCatalog catalog("http://localhost:8181"); |
| 71 | +TEST_F(RestCatalogIntegrationTest, ListNamespacesReturnsMultipleResults) { |
| 72 | + server_->Get("/v1/namespaces", [](const httplib::Request&, httplib::Response& res) { |
| 73 | + res.status = 200; |
| 74 | + res.set_content(R"({ |
| 75 | + "namespaces": [ |
| 76 | + ["accounting", "db"], |
| 77 | + ["production", "db"] |
| 78 | + ] |
| 79 | + })", |
| 80 | + "application/json"); |
| 81 | + }); |
| 82 | + |
| 83 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 84 | + RestCatalog catalog(base_uri); |
| 85 | + cpr::Response response = catalog.ListNamespaces(); |
| 86 | + |
| 87 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 88 | + ASSERT_EQ(response.status_code, 200); |
| 89 | + |
| 90 | + auto json_body = nlohmann::json::parse(response.text); |
| 91 | + ASSERT_TRUE(json_body.contains("namespaces")); |
| 92 | + EXPECT_EQ(json_body["namespaces"].size(), 2); |
| 93 | + EXPECT_THAT(json_body["namespaces"][0][0], "accounting"); |
| 94 | +} |
45 | 95 |
|
46 | | - cpr::Response response = catalog.testCprIntegration(); |
| 96 | +TEST_F(RestCatalogIntegrationTest, HandlesServerError) { |
| 97 | + server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) { |
| 98 | + res.status = 500; |
| 99 | + res.set_content("Internal Server Error", "text/plain"); |
| 100 | + }); |
47 | 101 |
|
48 | | - ASSERT_EQ(response.error.code, cpr::ErrorCode::OK) |
49 | | - << "CPR transport error: " << response.error.message; |
| 102 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 103 | + RestCatalog catalog(base_uri); |
| 104 | + cpr::Response response = catalog.GetConfig(); |
50 | 105 |
|
51 | | - EXPECT_EQ(response.status_code, 200); |
52 | | - EXPECT_THAT(response.text, ::testing::HasSubstr("\"hello\": \"world\"")); |
| 106 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 107 | + ASSERT_EQ(response.status_code, 500); |
| 108 | + ASSERT_EQ(response.text, "Internal Server Error"); |
53 | 109 | } |
54 | 110 |
|
55 | 111 | } // namespace iceberg::catalog::rest |
0 commit comments