#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 简单上传下载测试 """ import requests import sys import io # 设置stdout编码为utf-8 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") BASE_URL = "http://localhost:8000/api/v1" def simple_test(): print("=== 简单上传下载测试 ===") # 使用存在的用户ID (用户ID=3存在) user_id = 3 print(f"使用用户ID: {user_id}") # 直接测试下载现有文件 (文件ID=24, test.txt) file_id = 24 print(f"\n直接下载现有文件 ID: {file_id} (test.txt)...") download_data = { "file_id": file_id, "user_id": user_id } try: download_resp = requests.post(f"{BASE_URL}/files/download", json=download_data) print(f"下载状态码: {download_resp.status_code}") print(f"下载响应头: {dict(download_resp.headers)}") if download_resp.status_code == 200: downloaded_content = download_resp.text print(f"[OK] 下载成功") print(f"下载内容: {downloaded_content}") print(f"内容长度: {len(downloaded_content)}") # 读取原始文件内容进行对比 try: with open(f"uploads/608408b0-02c7-449e-ba58-be43c4360333.txt", 'r', encoding='utf-8') as f: original_content = f.read() print(f"原始文件长度: {len(original_content)}") if original_content == downloaded_content: print("[OK] 内容一致!上传下载功能正常") else: print("[ERROR] 内容不一致") print(f"原始: {repr(original_content[:100])}") print(f"下载: {repr(downloaded_content[:100])}") except Exception as e: print(f"无法读取原始文件进行对比: {e}") else: print(f"[ERROR] 下载失败: {download_resp.text}") except Exception as e: print(f"[ERROR] 下载异常: {e}") import traceback traceback.print_exc() # 现在测试上传新文件 print(f"\n=== 测试上传新文件 ===") test_content = f"这是全新的测试文件内容\nBrand new test file content\n唯一时间戳: {hash('unique_content_' + str(__import__('time').time()))}" files = {"file": (f"unique_test_{hash(test_content) % 10000}.txt", test_content, "text/plain")} data = { "user_id": str(user_id), "description": "新测试文件", "tags": "test,new", "is_public": "false" } try: upload_resp = requests.post(f"{BASE_URL}/files/upload", files=files, data=data) print(f"上传状态码: {upload_resp.status_code}") if upload_resp.status_code == 201: new_file_id = upload_resp.json()["data"]["file"]["id"] print(f"[OK] 新文件上传成功,文件ID: {new_file_id}") # 立即下载新文件 print(f"\n下载新上传的文件 ID: {new_file_id}...") download_data = { "file_id": new_file_id, "user_id": user_id } download_resp = requests.post(f"{BASE_URL}/files/download", json=download_data) if download_resp.status_code == 200: new_downloaded_content = download_resp.text print(f"[OK] 新文件下载成功") print(f"下载内容: {new_downloaded_content}") if test_content == new_downloaded_content: print("[OK] 新文件内容一致!") else: print("[ERROR] 新文件内容不一致") else: print(f"[ERROR] 新文件下载失败: {download_resp.text}") else: print(f"[ERROR] 新文件上传失败: {upload_resp.text}") except Exception as e: print(f"[ERROR] 新文件测试异常: {e}") import traceback traceback.print_exc() if __name__ == "__main__": simple_test()