273 lines
6.3 KiB
V
273 lines
6.3 KiB
V
import hono
|
|
import os
|
|
|
|
// 简化的测试统计
|
|
struct TestStats {
|
|
mut:
|
|
total_tests int
|
|
passed_tests int
|
|
failed_tests int
|
|
}
|
|
|
|
fn (mut stats TestStats) run_test(test_name string, test_func fn() bool) {
|
|
stats.total_tests++
|
|
print('🧪 ${test_name}... ')
|
|
|
|
if test_func() {
|
|
stats.passed_tests++
|
|
println('✅')
|
|
} else {
|
|
stats.failed_tests++
|
|
println('❌')
|
|
}
|
|
}
|
|
|
|
fn (stats TestStats) print_summary() {
|
|
println('\n=== 测试总结 ===')
|
|
println('总测试数: ${stats.total_tests}')
|
|
println('通过: ${stats.passed_tests}')
|
|
println('失败: ${stats.failed_tests}')
|
|
|
|
if stats.failed_tests == 0 {
|
|
println('🎉 所有测试通过!')
|
|
} else {
|
|
println('⚠️ 有 ${stats.failed_tests} 个测试失败')
|
|
}
|
|
}
|
|
|
|
// 1. 测试配置管理
|
|
fn test_config_management() bool {
|
|
// 测试默认配置
|
|
config := hono.default_config()
|
|
|
|
if config.server.host != '127.0.0.1' {
|
|
return false
|
|
}
|
|
|
|
if config.server.port != 8080 {
|
|
return false
|
|
}
|
|
|
|
// 测试配置验证
|
|
hono.validate_config(config) or {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// 2. 测试日志系统
|
|
fn test_logging_system() bool {
|
|
// 创建测试日志器
|
|
config := hono.LoggerConfig{
|
|
level: hono.LogLevel.debug
|
|
output: hono.LogOutput.console
|
|
enable_colors: false
|
|
}
|
|
|
|
mut logger := hono.new_logger(config)
|
|
|
|
// 测试基本日志方法
|
|
logger.info('测试信息日志')
|
|
logger.warn('测试警告日志')
|
|
logger.error('测试错误日志')
|
|
|
|
// 测试日志级别转换
|
|
if hono.parse_log_level('info') != hono.LogLevel.info {
|
|
return false
|
|
}
|
|
|
|
if hono.log_level_to_string(hono.LogLevel.error) != 'ERROR' {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// 3. 测试安全验证
|
|
fn test_security_validation() bool {
|
|
// 测试危险路径
|
|
dangerous_paths := [
|
|
'../../../etc/passwd',
|
|
'..\\..\\windows\\system32',
|
|
'/etc/passwd',
|
|
'C:\\Windows\\System32'
|
|
]
|
|
|
|
options := hono.PathValidationOptions{}
|
|
|
|
for path in dangerous_paths {
|
|
hono.validate_file_path(path, options) or {
|
|
// 验证失败是期望的结果
|
|
continue
|
|
}
|
|
// 如果没有返回错误,说明危险路径通过了验证,这是不对的
|
|
return false
|
|
}
|
|
|
|
// 测试安全路径
|
|
safe_path := 'documents/file.txt'
|
|
hono.validate_file_path(safe_path, options) or {
|
|
return false // 安全路径应该通过验证
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// 4. 测试错误处理结构
|
|
fn test_error_handling() bool {
|
|
// 测试错误类型
|
|
error_types := [
|
|
hono.ErrorType.bad_request,
|
|
hono.ErrorType.unauthorized,
|
|
hono.ErrorType.forbidden,
|
|
hono.ErrorType.not_found,
|
|
hono.ErrorType.internal_server_error
|
|
]
|
|
|
|
// 验证错误代码
|
|
expected_codes := [400, 401, 403, 404, 500]
|
|
|
|
for i, error_type in error_types {
|
|
if int(error_type) != expected_codes[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// 5. 测试配置文件操作
|
|
fn test_config_file_operations() bool {
|
|
config_path := './test_config.json'
|
|
|
|
// 清理可能存在的测试文件
|
|
if os.exists(config_path) {
|
|
os.rm(config_path) or { return false }
|
|
}
|
|
|
|
// 创建和保存配置
|
|
config := hono.default_config()
|
|
hono.save_config(config, config_path) or {
|
|
return false
|
|
}
|
|
|
|
// 加载配置
|
|
loaded_config := hono.load_config(config_path) or {
|
|
return false
|
|
}
|
|
|
|
// 验证配置内容
|
|
success := loaded_config.server.host == config.server.host &&
|
|
loaded_config.server.port == config.server.port
|
|
|
|
// 清理测试文件
|
|
os.rm(config_path) or {}
|
|
|
|
return success
|
|
}
|
|
|
|
// 6. 测试环境变量配置
|
|
fn test_env_config() bool {
|
|
// 设置环境变量
|
|
os.setenv('HONO_HOST', '0.0.0.0', true)
|
|
os.setenv('HONO_PORT', '9090', true)
|
|
os.setenv('HONO_ENV', 'production', true)
|
|
|
|
config := hono.load_config_from_env()
|
|
|
|
// 验证环境变量配置
|
|
success := config.server.host == '0.0.0.0' &&
|
|
config.server.port == 9090 &&
|
|
config.env == 'production'
|
|
|
|
// 清理环境变量
|
|
os.unsetenv('HONO_HOST')
|
|
os.unsetenv('HONO_PORT')
|
|
os.unsetenv('HONO_ENV')
|
|
|
|
return success
|
|
}
|
|
|
|
// 7. 测试配置摘要
|
|
fn test_config_summary() bool {
|
|
config := hono.default_config()
|
|
summary := hono.get_config_summary(config)
|
|
|
|
// 验证摘要包含关键信息
|
|
return summary.contains('应用配置摘要') &&
|
|
summary.contains('127.0.0.1:8080') &&
|
|
summary.contains('development') &&
|
|
summary.contains('静态文件') &&
|
|
summary.contains('文件上传')
|
|
}
|
|
|
|
// 8. 测试配置合并
|
|
fn test_config_merge() bool {
|
|
base_config := hono.default_config()
|
|
mut override_config := hono.AppConfig{}
|
|
override_config.server.host = '0.0.0.0'
|
|
override_config.server.port = 9000
|
|
override_config.env = 'production'
|
|
|
|
merged_config := hono.merge_config(base_config, override_config)
|
|
|
|
// 验证合并结果
|
|
return merged_config.server.host == '0.0.0.0' &&
|
|
merged_config.server.port == 9000 &&
|
|
merged_config.env == 'production' &&
|
|
merged_config.static.enabled == true // 其他值应该保持默认
|
|
}
|
|
|
|
// 9. 测试上传配置结构
|
|
fn test_upload_config_struct() bool {
|
|
config := hono.ChunkUploadConfig{
|
|
chunk_size: 1024 * 1024
|
|
max_file_size: 100 * 1024 * 1024
|
|
temp_dir: './test_uploads/chunks'
|
|
upload_dir: './test_uploads/files'
|
|
}
|
|
|
|
// 验证配置结构
|
|
return config.chunk_size == 1024 * 1024 &&
|
|
config.max_file_size == 100 * 1024 * 1024 &&
|
|
config.temp_dir == './test_uploads/chunks' &&
|
|
config.upload_dir == './test_uploads/files'
|
|
}
|
|
|
|
// 10. 测试缓存配置
|
|
fn test_cache_config() bool {
|
|
// 测试缓存配置结构
|
|
cache_config := hono.CacheConfig{
|
|
enabled: true
|
|
max_size: 1000
|
|
default_ttl: 300
|
|
cleanup_interval: 60
|
|
}
|
|
|
|
return cache_config.enabled == true &&
|
|
cache_config.max_size == 1000 &&
|
|
cache_config.default_ttl == 300 &&
|
|
cache_config.cleanup_interval == 60
|
|
}
|
|
|
|
fn main() {
|
|
println('🚀 开始V-Hono简化测试套件...\n')
|
|
|
|
mut stats := TestStats{}
|
|
|
|
// 运行所有测试
|
|
stats.run_test('配置管理', test_config_management)
|
|
stats.run_test('日志系统', test_logging_system)
|
|
stats.run_test('安全验证', test_security_validation)
|
|
stats.run_test('错误处理', test_error_handling)
|
|
stats.run_test('配置文件操作', test_config_file_operations)
|
|
stats.run_test('环境变量配置', test_env_config)
|
|
stats.run_test('配置摘要', test_config_summary)
|
|
stats.run_test('配置合并', test_config_merge)
|
|
stats.run_test('上传配置结构', test_upload_config_struct)
|
|
stats.run_test('缓存配置', test_cache_config)
|
|
|
|
// 打印测试总结
|
|
stats.print_summary()
|
|
} |