53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
import httpx
|
|
import asyncio
|
|
import json
|
|
|
|
async def test_upload():
|
|
base_url = "http://localhost:8080"
|
|
|
|
# 先登录获取token
|
|
login_url = f"{base_url}/api/auth/login"
|
|
login_data = {
|
|
"username": "admin",
|
|
"password": "admin123"
|
|
}
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
# 登录
|
|
login_response = await client.post(login_url, json=login_data)
|
|
print(f"Login Status: {login_response.status_code}")
|
|
if login_response.status_code == 200:
|
|
token_data = login_response.json()
|
|
token = token_data.get("token")
|
|
print(f"Got token: {token[:20]}...")
|
|
|
|
# 上传文件
|
|
upload_url = f"{base_url}/api/files/upload"
|
|
|
|
# 创建测试文件
|
|
test_file_path = "/tmp/test_file.txt"
|
|
with open(test_file_path, "w") as f:
|
|
f.write("This is a test file content")
|
|
|
|
# 准备文件和数据
|
|
files = {
|
|
"file": ("test_file.txt", open(test_file_path, "rb"), "multipart/form-data")
|
|
}
|
|
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
# 发送请求
|
|
response = await client.post(upload_url, files=files, headers=headers)
|
|
print(f"\nUpload Status Code: {response.status_code}")
|
|
print(f"Response Headers: {dict(response.headers)}")
|
|
print(f"Response Body: {response.text}")
|
|
|
|
# 清理
|
|
import os
|
|
os.remove(test_file_path)
|
|
else:
|
|
print(f"Login failed: {login_response.text}")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_upload()) |