#!/usr/bin/env python3 """ 测试API连接 """ import requests import json def test_backend_api(): """测试后端API""" base_url = "http://localhost:8000" try: # 测试健康检查 print("测试健康检查...") response = requests.get(f"{base_url}/health") print(f"健康检查状态: {response.status_code}") print(f"响应: {response.json()}") # 测试获取项目列表 print("\n测试获取项目列表...") response = requests.get(f"{base_url}/api/projects") print(f"项目列表状态: {response.status_code}") if response.status_code == 200: print(f"项目数量: {len(response.json())}") else: print(f"错误: {response.text}") # 测试创建项目 print("\n测试创建项目...") project_data = { "name": "测试项目", "description": "这是一个测试项目", "language": "python", "project_path": "C:\\Users\\31576\\Desktop\\新建文件夹\\sample_project" } response = requests.post( f"{base_url}/api/projects", json=project_data, headers={"Content-Type": "application/json"} ) print(f"创建项目状态: {response.status_code}") if response.status_code == 201: print("项目创建成功!") print(f"响应: {response.json()}") else: print(f"创建失败: {response.text}") except requests.exceptions.ConnectionError: print("无法连接到后端服务,请确保后端正在运行") except Exception as e: print(f"测试失败: {e}") if __name__ == "__main__": test_backend_api()