feat: 修改 400问题

This commit is contained in:
2026-09-28 22:39:54 +08:00
parent 7f1503294f
commit 0fa7b6bb8f
4 changed files with 57 additions and 3 deletions
+19
View File
@@ -8,6 +8,8 @@ import zipfile
from pathlib import Path
from unittest.mock import AsyncMock, patch
from fastapi import HTTPException
SERVER_DIR = Path(__file__).resolve().parents[1]
if str(SERVER_DIR) not in sys.path:
sys.path.insert(0, str(SERVER_DIR))
@@ -16,6 +18,23 @@ from api.export import ExportImageItem, ExportImagesRequest, export_image, expor
class ExportImageTests(unittest.IsolatedAsyncioTestCase):
async def test_extensionless_image_with_generic_mime_can_be_downloaded(self) -> None:
image = ExportImageItem(url="https://example.com/assets/opaque-key", key="upload")
png = b"\x89PNG\r\n\x1a\n" + b"image bytes"
with patch("api.export._download", new=AsyncMock(return_value=(png, "application/octet-stream"))):
response = await export_image(image)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.media_type, "image/png")
self.assertIn('filename="upload.png"', response.headers["content-disposition"])
async def test_extensionless_non_image_is_rejected(self) -> None:
image = ExportImageItem(url="https://example.com/assets/opaque-key")
with patch("api.export._download", new=AsyncMock(return_value=(b"not an image", "application/octet-stream"))):
with self.assertRaises(HTTPException) as raised:
await export_image(image)
self.assertEqual(raised.exception.status_code, 400)
async def test_chinese_filename_uses_rfc5987_header(self) -> None:
image = ExportImageItem(
url="https://example.com/image.png",