test_goal_creation.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试目标创建接口的脚本
  5. """
  6. import requests
  7. import json
  8. from datetime import datetime
  9. def test_create_goal():
  10. """测试创建目标接口"""
  11. url = "http://localhost:8080/api/goals"
  12. # 测试数据
  13. test_data = {
  14. "userId": 1,
  15. "subject": "数学",
  16. "goalType": "练习题",
  17. "goalContent": "二次函数",
  18. "difficulty": "中等",
  19. "totalQuantity": 5,
  20. "estimatedTime": 30,
  21. "startTime": datetime.now().isoformat()
  22. }
  23. headers = {
  24. "Content-Type": "application/json"
  25. }
  26. print(f"发送请求到: {url}")
  27. print(f"请求数据: {json.dumps(test_data, indent=2, ensure_ascii=False)}")
  28. print("-" * 50)
  29. try:
  30. response = requests.post(url, json=test_data, headers=headers, timeout=30)
  31. print(f"响应状态码: {response.status_code}")
  32. print(f"响应头: {dict(response.headers)}")
  33. print(f"响应内容: {response.text}")
  34. if response.status_code == 200:
  35. result = response.json()
  36. print("\n✅ 请求成功!")
  37. print(f"返回数据: {json.dumps(result, indent=2, ensure_ascii=False)}")
  38. else:
  39. print(f"\n❌ 请求失败! 状态码: {response.status_code}")
  40. except requests.exceptions.RequestException as e:
  41. print(f"\n❌ 请求异常: {e}")
  42. except Exception as e:
  43. print(f"\n❌ 其他异常: {e}")
  44. if __name__ == "__main__":
  45. test_create_goal()