import hono import os import time import strings // 测试统计 struct TestStats { mut: total_tests int passed_tests int failed_tests int start_time time.Time } fn (mut stats TestStats) start_test(test_name string) { stats.total_tests++ println('🧪 运行测试: ${test_name}') } fn (mut stats TestStats) pass_test(test_name string) { stats.passed_tests++ println('✅ 测试通过: ${test_name}') } fn (mut stats TestStats) fail_test(test_name string, error string) { stats.failed_tests++ println('❌ 测试失败: ${test_name} - ${error}') } fn (stats TestStats) print_summary() { duration := time.since(stats.start_time) println('\n=== 测试总结 ===') println('总测试数: ${stats.total_tests}') println('通过: ${stats.passed_tests}') println('失败: ${stats.failed_tests}') println('成功率: ${(stats.passed_tests * 100 / stats.total_tests)}%') println('耗时: ${duration.milliseconds()}ms') if stats.failed_tests == 0 { println('🎉 所有测试通过!') } else { println('⚠️ 有 ${stats.failed_tests} 个测试失败') } } // 1. 测试缓存系统 fn test_cache_system(mut stats TestStats) { stats.start_test('缓存系统') // 创建缓存 mut cache := hono.new_context_lru_cache(3) // 测试基本操作 cache.put('key1', 'value1') cache.put('key2', 'value2') cache.put('key3', 'value3') // 验证获取 if val := cache.get('key1') { if val != 'value1' { stats.fail_test('缓存系统', '获取值不匹配') return } } else { stats.fail_test('缓存系统', '无法获取缓存值') return } // 测试LRU淘汰 cache.put('key4', 'value4') // 应该淘汰key2 if cache.get('key2') or { '' } != '' { stats.fail_test('缓存系统', 'LRU淘汰机制失效') return } // 测试健康检查 if !cache.is_healthy() { stats.fail_test('缓存系统', '缓存健康检查失败') return } stats.pass_test('缓存系统') } // 2. 测试安全验证 fn test_security_validation(mut stats TestStats) { stats.start_test('安全验证') // 测试路径验证 dangerous_paths := [ '../../../etc/passwd', '..\\..\\windows\\system32', '/etc/passwd', 'C:\\Windows\\System32', 'file