TUT/tests/unit/test_url_parser.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

89 lines
2.6 KiB
C++

/**
* @file test_url_parser.cpp
* @brief URL 解析器单元测试
*/
#include <gtest/gtest.h>
#include "core/url_parser.hpp"
namespace tut {
namespace test {
class UrlParserTest : public ::testing::Test {
protected:
UrlParser parser_;
};
TEST_F(UrlParserTest, ParseValidHttpUrl) {
auto result = parser_.parse("https://example.com:8080/path?query=1");
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->scheme, "https");
EXPECT_EQ(result->host, "example.com");
EXPECT_EQ(result->port, 8080);
EXPECT_EQ(result->path, "/path");
EXPECT_EQ(result->query, "query=1");
}
TEST_F(UrlParserTest, ParseUrlWithoutPort) {
auto result = parser_.parse("http://example.com/path");
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->port, 80); // 默认 HTTP 端口
}
TEST_F(UrlParserTest, ParseHttpsDefaultPort) {
auto result = parser_.parse("https://example.com/path");
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->port, 443); // 默认 HTTPS 端口
}
TEST_F(UrlParserTest, ParseInvalidUrl) {
auto result = parser_.parse("not a url");
// 这个测试取决于我们如何定义 "无效"
// 当前实现可能仍会尝试解析
}
TEST_F(UrlParserTest, ParseEmptyUrl) {
auto result = parser_.parse("");
EXPECT_FALSE(result.has_value());
}
TEST_F(UrlParserTest, ParseUrlWithFragment) {
auto result = parser_.parse("https://example.com#section");
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->fragment, "section");
}
TEST_F(UrlParserTest, ParseUrlWithUserInfo) {
auto result = parser_.parse("https://user:pass@example.com/path");
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result->userinfo, "user:pass");
EXPECT_EQ(result->host, "example.com");
}
TEST_F(UrlParserTest, ResolveRelativeUrl) {
std::string base = "https://example.com/dir/page.html";
EXPECT_EQ(parser_.resolveRelative(base, "other.html"),
"https://example.com/dir/other.html");
EXPECT_EQ(parser_.resolveRelative(base, "/absolute.html"),
"https://example.com/absolute.html");
EXPECT_EQ(parser_.resolveRelative(base, "//other.com/path"),
"https://other.com/path");
}
TEST_F(UrlParserTest, NormalizeUrl) {
EXPECT_EQ(parser_.normalize("https://example.com/a/../b/./c"),
"https://example.com/b/c");
}
TEST_F(UrlParserTest, EncodeUrl) {
EXPECT_EQ(UrlParser::encode("hello world"), "hello%20world");
EXPECT_EQ(UrlParser::encode("a+b=c"), "a%2Bb%3Dc");
}
TEST_F(UrlParserTest, DecodeUrl) {
EXPECT_EQ(UrlParser::decode("hello%20world"), "hello world");
EXPECT_EQ(UrlParser::decode("a+b"), "a b");
}
} // namespace test
} // namespace tut