test_file_upload.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import sys
  2. import os
  3. # 添加项目根目录到Python路径
  4. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  5. def test_file_upload():
  6. """测试文件上传功能"""
  7. try:
  8. from services.utils.http_client import HTTPClient
  9. # 创建HTTP客户端实例
  10. http_client = HTTPClient(
  11. base_url="http://localhost:8000", # 替换为实际的API URL
  12. api_key="your_api_key" # 替换为实际的API密钥
  13. )
  14. # 测试文件路径
  15. test_file_path = r"D:\project\work\ragflow_plugs\book\不一样的卡梅拉1-我想去看海.pdf"
  16. # 打开文件并构建files字典
  17. with open(test_file_path, 'rb') as f:
  18. files = {'file': (os.path.basename(test_file_path), f)}
  19. print(f"测试文件上传: {test_file_path}")
  20. print(f"文件字典: {files}")
  21. # 发送POST请求,测试文件上传
  22. response = http_client.post(
  23. "/api/v1/test/upload", # 替换为实际的上传端点
  24. files=files
  25. )
  26. print(f"上传响应: {response}")
  27. print("✓ 文件上传测试通过")
  28. return True
  29. except Exception as e:
  30. print(f"✗ 文件上传测试失败: {str(e)}")
  31. import traceback
  32. traceback.print_exc()
  33. return False
  34. def test_post_without_files():
  35. """测试不带文件的POST请求"""
  36. try:
  37. from services.utils.http_client import HTTPClient
  38. # 创建HTTP客户端实例
  39. http_client = HTTPClient(
  40. base_url="http://localhost:8000", # 替换为实际的API URL
  41. api_key="your_api_key" # 替换为实际的API密钥
  42. )
  43. # 发送普通POST请求
  44. response = http_client.post(
  45. "/api/v1/test/post", # 替换为实际的POST端点
  46. json={"key": "value"}
  47. )
  48. print(f"普通POST响应: {response}")
  49. print("✓ 普通POST请求测试通过")
  50. return True
  51. except Exception as e:
  52. print(f"✗ 普通POST请求测试失败: {str(e)}")
  53. import traceback
  54. traceback.print_exc()
  55. return False
  56. def main():
  57. """主测试函数"""
  58. print("=== 测试文件上传修复 ===")
  59. # 测试文件上传
  60. test_file_upload()
  61. # 测试普通POST请求
  62. test_post_without_files()
  63. print("\n=== 测试完成 ===")
  64. if __name__ == "__main__":
  65. main()