#!/usr/bin/env python3 """ 综合测试所有修复 """ import urllib.request import json def test_fixes_comprehensive(): try: print("=== 综合测试API修复 ===") # 1. 测试模型列表 print("\n1. 测试模型列表:") url = 'http://localhost:5000/api/models' with urllib.request.urlopen(url) as response: data = response.read().decode('utf-8') result = json.loads(data) models = result.get('data', []) print(f"模型数量: {len(models)}") if models: print("可用模型:") for model in models: model_type = model.get('model_type', '') product_id = model.get('product_id', '') model_id = model.get('model_id', '') print(f" - {model_type}/{product_id} (ID: {model_id})") # 2. 测试模型详情端点 print("\n2. 测试模型详情端点:") if models: # 测试第一个模型的详情 first_model = models[0] model_type = first_model.get('model_type', '') product_id = first_model.get('product_id', '') details_url = f'http://localhost:5000/api/models/{model_type}/{product_id}/details' print(f"请求: {details_url}") try: req = urllib.request.Request(details_url) # 添加一些常见的请求头来测试CORS req.add_header('Origin', 'http://localhost:5173') req.add_header('Content-Type', 'application/json') with urllib.request.urlopen(req) as response: print(f"状态码: {response.status}") # 检查CORS头 headers = dict(response.headers) cors_headers = {k: v for k, v in headers.items() if 'access-control' in k.lower()} if cors_headers: print("CORS头:") for k, v in cors_headers.items(): print(f" {k}: {v}") details_data = response.read().decode('utf-8') details_result = json.loads(details_data) if details_result.get('status') == 'success': print("详情API: SUCCESS") data_obj = details_result.get('data', {}) model_info = data_obj.get('model_info', {}) training_metrics = data_obj.get('training_metrics', {}) print(f"模型信息字段: {len(model_info)}") print(f"训练指标: {list(training_metrics.keys())}") if training_metrics: print("主要指标:") for metric in ['RMSE', 'MAE', 'R2', 'MAPE']: if metric in training_metrics: print(f" {metric}: {training_metrics[metric]}") else: print(f"详情API错误: {details_result.get('message', '未知')}") except urllib.error.HTTPError as e: print(f"HTTP错误: {e.code}") try: error_data = e.read().decode('utf-8') error_result = json.loads(error_data) print(f"错误详情: {error_result.get('error', '无详情')}") except: print("无法解析错误响应") except Exception as e: print(f"请求异常: {e}") # 3. 测试CORS预检请求 print("\n3. 测试CORS预检请求:") try: req = urllib.request.Request('http://localhost:5000/api/models', method='OPTIONS') req.add_header('Origin', 'http://localhost:5173') req.add_header('Access-Control-Request-Method', 'GET') req.add_header('Access-Control-Request-Headers', 'Content-Type') with urllib.request.urlopen(req) as response: print(f"OPTIONS请求状态: {response.status}") cors_headers = {k: v for k, v in dict(response.headers).items() if 'access-control' in k.lower()} if cors_headers: print("CORS预检响应头:") for k, v in cors_headers.items(): print(f" {k}: {v}") except Exception as e: print(f"OPTIONS请求失败: {e}") print(f"\n=== 修复状态总结 ===") print("1. 模型详情端点: 修复文件名格式匹配问题") print("2. CORS配置: 增强跨域头设置") print("3. 预检请求: 添加OPTIONS方法处理") print("4. 安全头: 添加Cross-Origin策略头") print("\n如果前端仍有CORS错误,请检查:") print("- 前端是否运行在http://localhost:5173") print("- API是否运行在http://localhost:5000") print("- 浏览器开发者工具Network面板的详细错误") except Exception as e: print(f"测试失败: {e}") if __name__ == "__main__": test_fixes_comprehensive()