import sys import os # 添加项目根目录到Python路径 sys.path.append(os.path.dirname(os.path.abspath(__file__))) def test_file_upload(): """测试文件上传功能""" try: from services.utils.http_client import HTTPClient # 创建HTTP客户端实例 http_client = HTTPClient( base_url="http://localhost:8000", # 替换为实际的API URL api_key="your_api_key" # 替换为实际的API密钥 ) # 测试文件路径 test_file_path = r"D:\project\work\ragflow_plugs\book\不一样的卡梅拉1-我想去看海.pdf" # 打开文件并构建files字典 with open(test_file_path, 'rb') as f: files = {'file': (os.path.basename(test_file_path), f)} print(f"测试文件上传: {test_file_path}") print(f"文件字典: {files}") # 发送POST请求,测试文件上传 response = http_client.post( "/api/v1/test/upload", # 替换为实际的上传端点 files=files ) print(f"上传响应: {response}") print("✓ 文件上传测试通过") return True except Exception as e: print(f"✗ 文件上传测试失败: {str(e)}") import traceback traceback.print_exc() return False def test_post_without_files(): """测试不带文件的POST请求""" try: from services.utils.http_client import HTTPClient # 创建HTTP客户端实例 http_client = HTTPClient( base_url="http://localhost:8000", # 替换为实际的API URL api_key="your_api_key" # 替换为实际的API密钥 ) # 发送普通POST请求 response = http_client.post( "/api/v1/test/post", # 替换为实际的POST端点 json={"key": "value"} ) print(f"普通POST响应: {response}") print("✓ 普通POST请求测试通过") return True except Exception as e: print(f"✗ 普通POST请求测试失败: {str(e)}") import traceback traceback.print_exc() return False def main(): """主测试函数""" print("=== 测试文件上传修复 ===") # 测试文件上传 test_file_upload() # 测试普通POST请求 test_post_without_files() print("\n=== 测试完成 ===") if __name__ == "__main__": main()