import hono import time import strings import net.http // 测试统计 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++ print('🧪 ${test_name}... ') } fn (mut stats TestStats) pass_test() { stats.passed_tests++ println('✅') } fn (mut stats TestStats) fail_test(error string) { stats.failed_tests++ println('❌ ${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}') if stats.total_tests > 0 { 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.ContextLRUCache.new(3) // 创建测试数据 test_match := hono.ContextRouteMatch{ handler: hono.ContextHandler{ path: '/test' } params: {'id': '123'} path: '/test' base_path: '' } // 测试基本操作 cache.put('key1', test_match) cache.put('key2', test_match) cache.put('key3', test_match) // 验证获取 if _ := cache.get('key1') { // 获取成功 } else { stats.fail_test('无法获取缓存值') return } // 测试LRU淘汰 - key1 被访问后移到头部,key2 成为最久未使用 cache.put('key4', test_match) // 应该淘汰 key2 if _ := cache.get('key2') { 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