#!/usr/bin/env python3
"""
快速CORS测试脚本
"""

import requests
from datetime import datetime

def test_cors():
    """测试CORS配置"""
    base_url = "http://company.citupro.com:5500"
    endpoint = "/api/data_parse/get-calendar-info"
    
    print("=== CORS配置测试 ===")
    print(f"测试时间: {datetime.now()}")
    print(f"目标服务器: {base_url}")
    print(f"测试端点: {endpoint}")
    print("=" * 50)
    
    # 测试1: OPTIONS预检请求
    print("1. 测试OPTIONS预检请求...")
    try:
        headers = {
            'Origin': 'http://localhost:5173',
            'Access-Control-Request-Method': 'GET',
            'Access-Control-Request-Headers': 'Content-Type'
        }
        
        response = requests.options(f"{base_url}{endpoint}", headers=headers)
        print(f"   状态码: {response.status_code}")
        
        # 检查CORS头部
        cors_headers = []
        for key, value in response.headers.items():
            if key.lower().startswith('access-control'):
                cors_headers.append(f"{key}: {value}")
        
        if cors_headers:
            print("   CORS头部:")
            for header in cors_headers:
                print(f"     {header}")
        else:
            print("   ❌ 未找到CORS头部")
            
    except Exception as e:
        print(f"   ❌ 预检请求失败: {e}")
    
    print()
    
    # 测试2: 实际GET请求
    print("2. 测试实际GET请求...")
    try:
        today = datetime.now().strftime("%Y-%m-%d")
        headers = {'Origin': 'http://localhost:5173'}
        
        response = requests.get(f"{base_url}{endpoint}?date={today}", headers=headers)
        print(f"   状态码: {response.status_code}")
        
        if response.status_code == 200:
            print("   ✅ 请求成功")
            # 检查响应中的CORS头部
            cors_headers = []
            for key, value in response.headers.items():
                if key.lower().startswith('access-control'):
                    cors_headers.append(f"{key}: {value}")
            
            if cors_headers:
                print("   CORS头部:")
                for header in cors_headers:
                    print(f"     {header}")
            else:
                print("   ⚠️  响应中未找到CORS头部")
        else:
            print(f"   ❌ 请求失败: {response.text}")
            
    except Exception as e:
        print(f"   ❌ GET请求失败: {e}")
    
    print()
    print("=" * 50)
    print("测试完成！")
    print("\n如果看到CORS头部，说明配置正确。")
    print("如果仍有问题，请检查：")
    print("1. Flask应用是否已重启")
    print("2. 防火墙设置")
    print("3. 代理配置")

if __name__ == "__main__":
    test_cors()
