TUT/tests/unit/test_http_client.cpp
m1ngsama 6408f0e95c feat: Complete FTXUI refactoring with clean architecture
Major architectural refactoring from ncurses to FTXUI framework with
professional engineering structure.

Project Structure:
- src/core/: Browser engine, URL parser, HTTP client
- src/ui/: FTXUI components (main window, address bar, content view, panels)
- src/renderer/: HTML renderer, text formatter, style parser
- src/utils/: Logger, config manager, theme manager
- tests/unit/: Unit tests for core components
- tests/integration/: Integration tests
- assets/: Default configs, themes, keybindings

New Features:
- btop-style four-panel layout with rounded borders
- TOML-based configuration system
- Multiple color themes (default, nord, gruvbox, solarized)
- Comprehensive logging system
- Modular architecture with clear separation of concerns

Build System:
- Updated CMakeLists.txt for modular build
- Prefer system packages (Homebrew) over FetchContent
- Google Test integration for testing
- Version info generation via cmake/version.hpp.in

Configuration:
- Default config.toml with browser settings
- Four built-in themes
- Default keybindings configuration
- Config stored in ~/.config/tut/

Removed:
- Legacy v1 source files (ncurses-based)
- Old render/ directory
- Duplicate and obsolete test files
- Old documentation files

Binary: ~827KB (well under 5MB goal)
Dependencies: FTXUI, cpp-httplib, toml11, gumbo-parser, OpenSSL
2025-12-29 22:07:39 +08:00

79 lines
2.1 KiB
C++

/**
* @file test_http_client.cpp
* @brief HTTP 客户端单元测试
*/
#include <gtest/gtest.h>
#include "core/http_client.hpp"
namespace tut {
namespace test {
class HttpClientTest : public ::testing::Test {
protected:
HttpClient client_;
};
TEST_F(HttpClientTest, GetRequest) {
auto response = client_.get("https://httpbin.org/get");
EXPECT_TRUE(response.isSuccess());
EXPECT_EQ(response.status_code, 200);
EXPECT_FALSE(response.body.empty());
}
TEST_F(HttpClientTest, PostRequest) {
auto response = client_.post(
"https://httpbin.org/post",
"key=value",
"application/x-www-form-urlencoded"
);
EXPECT_TRUE(response.isSuccess());
EXPECT_EQ(response.status_code, 200);
}
TEST_F(HttpClientTest, HeadRequest) {
auto response = client_.head("https://httpbin.org/get");
EXPECT_TRUE(response.isSuccess());
EXPECT_TRUE(response.body.empty()); // HEAD 请求没有 body
}
TEST_F(HttpClientTest, InvalidUrl) {
auto response = client_.get("https://invalid.invalid.invalid/");
EXPECT_TRUE(response.isError());
}
TEST_F(HttpClientTest, TimeoutConfig) {
HttpConfig config;
config.timeout_seconds = 5;
client_.setConfig(config);
EXPECT_EQ(client_.getConfig().timeout_seconds, 5);
}
TEST_F(HttpClientTest, CookieManagement) {
client_.setCookie("example.com", "session", "abc123");
auto cookie = client_.getCookie("example.com", "session");
ASSERT_TRUE(cookie.has_value());
EXPECT_EQ(*cookie, "abc123");
client_.clearCookies();
cookie = client_.getCookie("example.com", "session");
EXPECT_FALSE(cookie.has_value());
}
TEST_F(HttpClientTest, Redirect) {
// httpbin.org/redirect/n redirects n times then returns 200
auto response = client_.get("https://httpbin.org/redirect/1");
EXPECT_TRUE(response.isSuccess());
EXPECT_EQ(response.status_code, 200);
}
TEST_F(HttpClientTest, NotFound) {
auto response = client_.get("https://httpbin.org/status/404");
EXPECT_FALSE(response.isSuccess());
EXPECT_EQ(response.status_code, 404);
}
} // namespace test
} // namespace tut